blob: c095c82eea8fb2ceef4c1fc8cd8e5977e463eceb [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01002#include "pycore_pystate.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004#include "structmember.h"
5
Neal Norwitz5d0ad502005-12-19 04:27:42 +00006/* error strings used for warnings */
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02007#define GLOBAL_PARAM \
8"name '%U' is parameter and global"
9
10#define NONLOCAL_PARAM \
11"name '%U' is parameter and nonlocal"
12
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070014"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000015
Jeremy Hylton81e95022007-02-27 06:50:52 +000016#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070017"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000018
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000019#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070020"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000021
Jeremy Hylton81e95022007-02-27 06:50:52 +000022#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070023"name '%U' is used prior to nonlocal declaration"
24
25#define GLOBAL_ANNOT \
26"annotated name '%U' can't be global"
27
28#define NONLOCAL_ANNOT \
29"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000030
Neal Norwitz5d0ad502005-12-19 04:27:42 +000031#define IMPORT_STAR_WARNING "import * only allowed at module level"
32
Neal Norwitz090b3dd2006-02-28 22:36:46 +000033static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000034ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000035 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000036{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020038 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 k = PyLong_FromVoidPtr(key);
41 if (k == NULL)
42 goto fail;
43 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020044 if (ste == NULL) {
45 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020047 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020049 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020052 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 ste->ste_symbols = NULL;
55 ste->ste_varnames = NULL;
56 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000057
Benjamin Petersond9c87022012-10-31 20:26:20 -040058 ste->ste_directives = NULL;
59
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 ste->ste_nested = 0;
62 ste->ste_free = 0;
63 ste->ste_varargs = 0;
64 ste->ste_varkeywords = 0;
65 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000066 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000068 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 if (st->st_cur != NULL &&
71 (st->st_cur->ste_nested ||
72 st->st_cur->ste_type == FunctionBlock))
73 ste->ste_nested = 1;
74 ste->ste_child_free = 0;
75 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070076 ste->ste_coroutine = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050078 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000079
Victor Stinner9a4fb662013-07-11 22:49:00 +020080 ste->ste_symbols = PyDict_New();
81 ste->ste_varnames = PyList_New(0);
82 ste->ste_children = PyList_New(0);
83 if (ste->ste_symbols == NULL
84 || ste->ste_varnames == NULL
85 || ste->ste_children == NULL)
86 goto fail;
87
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
89 goto fail;
90
91 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 Py_XDECREF(ste);
94 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000095}
96
97static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
101 ste->ste_name,
102 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000103}
104
105static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 ste->ste_table = NULL;
109 Py_XDECREF(ste->ste_id);
110 Py_XDECREF(ste->ste_name);
111 Py_XDECREF(ste->ste_symbols);
112 Py_XDECREF(ste->ste_varnames);
113 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400114 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000116}
117
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119
Guido van Rossum6f799372001-09-20 20:46:19 +0000120static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 {"id", T_OBJECT, OFF(ste_id), READONLY},
122 {"name", T_OBJECT, OFF(ste_name), READONLY},
123 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
124 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
125 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 {"nested", T_INT, OFF(ste_nested), READONLY},
127 {"type", T_INT, OFF(ste_type), READONLY},
128 {"lineno", T_INT, OFF(ste_lineno), READONLY},
129 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130};
131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000132PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 PyVarObject_HEAD_INIT(&PyType_Type, 0)
134 "symtable entry",
135 sizeof(PySTEntryObject),
136 0,
137 (destructor)ste_dealloc, /* tp_dealloc */
138 0, /* tp_print */
139 0, /* tp_getattr */
140 0, /* tp_setattr */
141 0, /* tp_reserved */
142 (reprfunc)ste_repr, /* tp_repr */
143 0, /* tp_as_number */
144 0, /* tp_as_sequence */
145 0, /* tp_as_mapping */
146 0, /* tp_hash */
147 0, /* tp_call */
148 0, /* tp_str */
149 PyObject_GenericGetAttr, /* tp_getattro */
150 0, /* tp_setattro */
151 0, /* tp_as_buffer */
152 Py_TPFLAGS_DEFAULT, /* tp_flags */
153 0, /* tp_doc */
154 0, /* tp_traverse */
155 0, /* tp_clear */
156 0, /* tp_richcompare */
157 0, /* tp_weaklistoffset */
158 0, /* tp_iter */
159 0, /* tp_iternext */
160 0, /* tp_methods */
161 ste_memberlist, /* tp_members */
162 0, /* tp_getset */
163 0, /* tp_base */
164 0, /* tp_dict */
165 0, /* tp_descr_get */
166 0, /* tp_descr_set */
167 0, /* tp_dictoffset */
168 0, /* tp_init */
169 0, /* tp_alloc */
170 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000171};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000172
173static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000175 _Py_block_ty block, void *ast, int lineno,
176 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177static int symtable_exit_block(struct symtable *st, void *ast);
178static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
179static int symtable_visit_expr(struct symtable *st, expr_ty s);
180static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000181static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
182static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000183static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184static int symtable_visit_arguments(struct symtable *st, arguments_ty);
185static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
186static int symtable_visit_alias(struct symtable *st, alias_ty);
187static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
188static int symtable_visit_keyword(struct symtable *st, keyword_ty);
189static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000190static int symtable_visit_params(struct symtable *st, asdl_seq *args);
191static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192static int symtable_implicit_arg(struct symtable *st, int pos);
Yury Selivanov75445082015-05-11 22:57:16 -0400193static int symtable_visit_annotations(struct symtable *st, stmt_ty s, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500194static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196
Nick Coghlan650f0d02007-04-15 12:05:43 +0000197static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500199 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
201#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
204#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000205"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206
207static struct symtable *
208symtable_new(void)
209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
213 if (st == NULL)
214 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 st->st_filename = NULL;
217 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 if ((st->st_stack = PyList_New(0)) == NULL)
220 goto fail;
221 if ((st->st_blocks = PyDict_New()) == NULL)
222 goto fail;
223 st->st_cur = NULL;
224 st->st_private = NULL;
225 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 PySymtable_Free(st);
228 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229}
230
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000231/* When compiling the use of C stack is probably going to be a lot
232 lighter than when executing Python code but still can overflow
233 and causing a Python crash if not checked (e.g. eval("()"*300000)).
234 Using the current recursion limit for the compiler seems too
235 restrictive (it caused at least one test to fail) so a factor is
236 used to allow deeper recursion when compiling an expression.
237
238 Using a scaling factor means this should automatically adjust when
239 the recursion limit is adjusted for small or large C stack allocations.
240*/
241#define COMPILER_STACK_FRAME_SCALE 3
242
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200244PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000246 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 asdl_seq *seq;
248 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000249 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400250 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200253 return NULL;
254 if (filename == NULL) {
255 PySymtable_Free(st);
256 return NULL;
257 }
258 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 st->st_filename = filename;
260 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000261
262 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100263 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000264 if (!tstate) {
265 PySymtable_Free(st);
266 return NULL;
267 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400268 /* Be careful here to prevent overflow. */
269 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
270 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
271 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
272 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* Make the initial symbol information gathering pass */
275 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000276 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 PySymtable_Free(st);
278 return NULL;
279 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 switch (mod->kind) {
283 case Module_kind:
284 seq = mod->v.Module.body;
285 for (i = 0; i < asdl_seq_LEN(seq); i++)
286 if (!symtable_visit_stmt(st,
287 (stmt_ty)asdl_seq_GET(seq, i)))
288 goto error;
289 break;
290 case Expression_kind:
291 if (!symtable_visit_expr(st, mod->v.Expression.body))
292 goto error;
293 break;
294 case Interactive_kind:
295 seq = mod->v.Interactive.body;
296 for (i = 0; i < asdl_seq_LEN(seq); i++)
297 if (!symtable_visit_stmt(st,
298 (stmt_ty)asdl_seq_GET(seq, i)))
299 goto error;
300 break;
301 case Suite_kind:
302 PyErr_SetString(PyExc_RuntimeError,
303 "this compiler does not handle Suites");
304 goto error;
305 }
306 if (!symtable_exit_block(st, (void *)mod)) {
307 PySymtable_Free(st);
308 return NULL;
309 }
310 /* Make the second symbol analysis pass */
311 if (symtable_analyze(st))
312 return st;
313 PySymtable_Free(st);
314 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 (void) symtable_exit_block(st, (void *)mod);
317 PySymtable_Free(st);
318 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319}
320
Victor Stinner14e461d2013-08-26 22:28:21 +0200321struct symtable *
322PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
323{
324 PyObject *filename;
325 struct symtable *st;
326 filename = PyUnicode_DecodeFSDefault(filename_str);
327 if (filename == NULL)
328 return NULL;
329 st = PySymtable_BuildObject(mod, filename, future);
330 Py_DECREF(filename);
331 return st;
332}
333
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334void
335PySymtable_Free(struct symtable *st)
336{
Victor Stinner14e461d2013-08-26 22:28:21 +0200337 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 Py_XDECREF(st->st_blocks);
339 Py_XDECREF(st->st_stack);
340 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341}
342
343PySTEntryObject *
344PySymtable_Lookup(struct symtable *st, void *key)
345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 k = PyLong_FromVoidPtr(key);
349 if (k == NULL)
350 return NULL;
351 v = PyDict_GetItem(st->st_blocks, k);
352 if (v) {
353 assert(PySTEntry_Check(v));
354 Py_INCREF(v);
355 }
356 else {
357 PyErr_SetString(PyExc_KeyError,
358 "unknown symbol table entry");
359 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 Py_DECREF(k);
362 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363}
364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366PyST_GetScope(PySTEntryObject *ste, PyObject *name)
367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
369 if (!v)
370 return 0;
371 assert(PyLong_Check(v));
372 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373}
374
Benjamin Petersond9c87022012-10-31 20:26:20 -0400375static int
376error_at_directive(PySTEntryObject *ste, PyObject *name)
377{
378 Py_ssize_t i;
379 PyObject *data;
380 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600381 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400382 data = PyList_GET_ITEM(ste->ste_directives, i);
383 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600384 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
385 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
386 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
387 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
Ammar Askar025eb982018-09-24 17:12:49 -0400388 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600389
390 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700391 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400392 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600393 PyErr_SetString(PyExc_RuntimeError,
394 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400395 return 0;
396}
397
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398
399/* Analyze raw symbol information to determine scope of each name.
400
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000401 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407 explicit global is declared with the global statement. An implicit
408 global is a free variable for which the compiler has found no binding
409 in an enclosing function scope. The implicit global is either a global
410 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
411 to handle these names to implement slightly odd semantics. In such a
412 block, the name is treated as global until it is assigned to; then it
413 is treated as a local.
414
415 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000416 The first pass collects raw facts from the AST via the symtable_visit_*
417 functions: the name is a parameter here, the name is used but not defined
418 here, etc. The second pass analyzes these facts during a pass over the
419 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420
421 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000423 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000424 Names which are explicitly declared nonlocal must exist in this set of
425 visible names - if they do not, a syntax error is raised. After doing
426 the local analysis, it analyzes each of its child blocks using an
427 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428
Nick Coghlan650f0d02007-04-15 12:05:43 +0000429 The children update the free variable set. If a local variable is added to
430 the free variable set by the child, the variable is marked as a cell. The
431 function object being defined must provide runtime storage for the variable
432 that may outlive the function's frame. Cell variables are removed from the
433 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000434
Nick Coghlan650f0d02007-04-15 12:05:43 +0000435 During analysis, the names are:
436 symbols: dict mapping from symbol names to flag values (including offset scope values)
437 scopes: dict mapping from symbol names to scope values (no offset)
438 local: set of all symbol names local to the current scope
439 bound: set of all symbol names local to a containing function scope
440 free: set of all symbol names referenced but not bound in child scopes
441 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442*/
443
444#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyObject *o = PyLong_FromLong(I); \
446 if (!o) \
447 return 0; \
448 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
449 Py_DECREF(o); \
450 return 0; \
451 } \
452 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453}
454
455/* Decide on scope of name, given flags.
456
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000457 The namespace dictionaries may be modified to record information
458 about the new name. For example, a new global will add an entry to
459 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460*/
461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000463analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 PyObject *bound, PyObject *local, PyObject *free,
465 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 if (flags & DEF_NONLOCAL) {
469 PyErr_Format(PyExc_SyntaxError,
470 "name '%U' is nonlocal and global",
471 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400472 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 }
474 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
475 if (PySet_Add(global, name) < 0)
476 return 0;
477 if (bound && (PySet_Discard(bound, name) < 0))
478 return 0;
479 return 1;
480 }
481 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (!bound) {
483 PyErr_Format(PyExc_SyntaxError,
484 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400485 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 }
487 if (!PySet_Contains(bound, name)) {
488 PyErr_Format(PyExc_SyntaxError,
489 "no binding for nonlocal '%U' found",
490 name);
491
Benjamin Petersond9c87022012-10-31 20:26:20 -0400492 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 }
494 SET_SCOPE(scopes, name, FREE);
495 ste->ste_free = 1;
496 return PySet_Add(free, name) >= 0;
497 }
498 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000499 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (PySet_Add(local, name) < 0)
501 return 0;
502 if (PySet_Discard(global, name) < 0)
503 return 0;
504 return 1;
505 }
506 /* If an enclosing block has a binding for this name, it
507 is a free variable rather than a global variable.
508 Note that having a non-NULL bound implies that the block
509 is nested.
510 */
511 if (bound && PySet_Contains(bound, name)) {
512 SET_SCOPE(scopes, name, FREE);
513 ste->ste_free = 1;
514 return PySet_Add(free, name) >= 0;
515 }
516 /* If a parent has a global statement, then call it global
517 explicit? It could also be global implicit.
518 */
519 if (global && PySet_Contains(global, name)) {
520 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
521 return 1;
522 }
523 if (ste->ste_nested)
524 ste->ste_free = 1;
525 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
526 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527}
528
529#undef SET_SCOPE
530
531/* If a name is defined in free and also in locals, then this block
532 provides the binding for the free variable. The name should be
533 marked CELL in this block and removed from the free list.
534
535 Note that the current block's free variables are included in free.
536 That's safe because no name can be free and local in the same scope.
537*/
538
539static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500540analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 PyObject *name, *v, *v_cell;
543 int success = 0;
544 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 v_cell = PyLong_FromLong(CELL);
547 if (!v_cell)
548 return 0;
549 while (PyDict_Next(scopes, &pos, &name, &v)) {
550 long scope;
551 assert(PyLong_Check(v));
552 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000553 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 continue;
555 if (!PySet_Contains(free, name))
556 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* Replace LOCAL with CELL for this name, and remove
558 from free. It is safe to replace the value of name
559 in the dict, because it will not cause a resize.
560 */
561 if (PyDict_SetItem(scopes, name, v_cell) < 0)
562 goto error;
563 if (PySet_Discard(free, name) < 0)
564 goto error;
565 }
566 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 Py_DECREF(v_cell);
569 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570}
571
Benjamin Peterson312595c2013-05-15 15:26:42 -0500572static int
573drop_class_free(PySTEntryObject *ste, PyObject *free)
574{
575 int res;
576 if (!GET_IDENTIFIER(__class__))
577 return 0;
578 res = PySet_Discard(free, __class__);
579 if (res < 0)
580 return 0;
581 if (res)
582 ste->ste_needs_class_closure = 1;
583 return 1;
584}
585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586/* Enter the final scope information into the ste_symbols dict.
587 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588 * All arguments are dicts. Modifies symbols, others are read-only.
589*/
590static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 PyObject *name = NULL, *itr = NULL;
595 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
596 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* Update scope information for all symbols in this scope */
599 while (PyDict_Next(symbols, &pos, &name, &v)) {
600 long scope, flags;
601 assert(PyLong_Check(v));
602 flags = PyLong_AS_LONG(v);
603 v_scope = PyDict_GetItem(scopes, name);
604 assert(v_scope && PyLong_Check(v_scope));
605 scope = PyLong_AS_LONG(v_scope);
606 flags |= (scope << SCOPE_OFFSET);
607 v_new = PyLong_FromLong(flags);
608 if (!v_new)
609 return 0;
610 if (PyDict_SetItem(symbols, name, v_new) < 0) {
611 Py_DECREF(v_new);
612 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 Py_DECREF(v_new);
615 }
616
617 /* Record not yet resolved free variables from children (if any) */
618 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
619 if (!v_free)
620 return 0;
621
622 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600623 if (itr == NULL) {
624 Py_DECREF(v_free);
625 return 0;
626 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627
628 while ((name = PyIter_Next(itr))) {
629 v = PyDict_GetItem(symbols, name);
630
631 /* Handle symbol that already exists in this scope */
632 if (v) {
633 /* Handle a free variable in a method of
634 the class that has the same name as a local
635 or global in the class scope.
636 */
637 if (classflag &&
638 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
639 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
640 v_new = PyLong_FromLong(flags);
641 if (!v_new) {
642 goto error;
643 }
644 if (PyDict_SetItem(symbols, name, v_new) < 0) {
645 Py_DECREF(v_new);
646 goto error;
647 }
648 Py_DECREF(v_new);
649 }
650 /* It's a cell, or already free in this scope */
651 Py_DECREF(name);
652 continue;
653 }
654 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200655 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 Py_DECREF(name);
657 continue; /* it's a global */
658 }
659 /* Propagate new free symbol up the lexical stack */
660 if (PyDict_SetItem(symbols, name, v_free) < 0) {
661 goto error;
662 }
663 Py_DECREF(name);
664 }
665 Py_DECREF(itr);
666 Py_DECREF(v_free);
667 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000668error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 Py_XDECREF(v_free);
670 Py_XDECREF(itr);
671 Py_XDECREF(name);
672 return 0;
673}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674
675/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000676
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 Arguments:
678 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000679 bound -- set of variables bound in enclosing scopes (input). bound
680 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681 free -- set of free variables in enclosed scopes (output)
682 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000683
684 The implementation uses two mutually recursive functions,
685 analyze_block() and analyze_child_block(). analyze_block() is
686 responsible for analyzing the individual names defined in a block.
687 analyze_child_block() prepares temporary namespace dictionaries
688 used to evaluated nested blocks.
689
690 The two functions exist because a child block should see the name
691 bindings of its enclosing blocks, but those bindings should not
692 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693*/
694
695static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
697 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000698
699static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
701 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
704 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
705 PyObject *temp;
706 int i, success = 0;
707 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 local = PySet_New(NULL); /* collect new names bound in block */
710 if (!local)
711 goto error;
712 scopes = PyDict_New(); /* collect scopes defined for each name */
713 if (!scopes)
714 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 /* Allocate new global and bound variable dictionaries. These
717 dictionaries hold the names visible in nested blocks. For
718 ClassBlocks, the bound and global names are initialized
719 before analyzing names, because class bindings aren't
720 visible in methods. For other blocks, they are initialized
721 after names are analyzed.
722 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 /* TODO(jhylton): Package these dicts in a struct so that we
725 can write reasonable helper functions?
726 */
727 newglobal = PySet_New(NULL);
728 if (!newglobal)
729 goto error;
730 newfree = PySet_New(NULL);
731 if (!newfree)
732 goto error;
733 newbound = PySet_New(NULL);
734 if (!newbound)
735 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 /* Class namespace has no effect on names visible in
738 nested functions, so populate the global and bound
739 sets to be passed to child blocks before analyzing
740 this one.
741 */
742 if (ste->ste_type == ClassBlock) {
743 /* Pass down known globals */
744 temp = PyNumber_InPlaceOr(newglobal, global);
745 if (!temp)
746 goto error;
747 Py_DECREF(temp);
748 /* Pass down previously bound symbols */
749 if (bound) {
750 temp = PyNumber_InPlaceOr(newbound, bound);
751 if (!temp)
752 goto error;
753 Py_DECREF(temp);
754 }
755 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
758 long flags = PyLong_AS_LONG(v);
759 if (!analyze_name(ste, scopes, name, flags,
760 bound, local, free, global))
761 goto error;
762 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 /* Populate global and bound sets to be passed to children. */
765 if (ste->ste_type != ClassBlock) {
766 /* Add function locals to bound set */
767 if (ste->ste_type == FunctionBlock) {
768 temp = PyNumber_InPlaceOr(newbound, local);
769 if (!temp)
770 goto error;
771 Py_DECREF(temp);
772 }
773 /* Pass down previously bound symbols */
774 if (bound) {
775 temp = PyNumber_InPlaceOr(newbound, bound);
776 if (!temp)
777 goto error;
778 Py_DECREF(temp);
779 }
780 /* Pass down known globals */
781 temp = PyNumber_InPlaceOr(newglobal, global);
782 if (!temp)
783 goto error;
784 Py_DECREF(temp);
785 }
786 else {
787 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000788 if (!GET_IDENTIFIER(__class__))
789 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 if (PySet_Add(newbound, __class__) < 0)
791 goto error;
792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300794 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 newbound, newglobal now contain the names visible in
797 nested blocks. The free variables in the children will
798 be collected in allfree.
799 */
800 allfree = PySet_New(NULL);
801 if (!allfree)
802 goto error;
803 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
804 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
805 PySTEntryObject* entry;
806 assert(c && PySTEntry_Check(c));
807 entry = (PySTEntryObject*)c;
808 if (!analyze_child_block(entry, newbound, newfree, newglobal,
809 allfree))
810 goto error;
811 /* Check if any children have free variables */
812 if (entry->ste_free || entry->ste_child_free)
813 ste->ste_child_free = 1;
814 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 temp = PyNumber_InPlaceOr(newfree, allfree);
817 if (!temp)
818 goto error;
819 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500822 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500824 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 goto error;
826 /* Records the results of the analysis in the symbol table entry */
827 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
828 ste->ste_type == ClassBlock))
829 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 temp = PyNumber_InPlaceOr(free, newfree);
832 if (!temp)
833 goto error;
834 Py_DECREF(temp);
835 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 Py_XDECREF(scopes);
838 Py_XDECREF(local);
839 Py_XDECREF(newbound);
840 Py_XDECREF(newglobal);
841 Py_XDECREF(newfree);
842 Py_XDECREF(allfree);
843 if (!success)
844 assert(PyErr_Occurred());
845 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846}
847
848static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
850 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
853 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000856
Martin Panter3ee62702016-06-04 04:57:19 +0000857 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 current block. The analyze_block() call modifies these
859 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 */
862 temp_bound = PySet_New(bound);
863 if (!temp_bound)
864 goto error;
865 temp_free = PySet_New(free);
866 if (!temp_free)
867 goto error;
868 temp_global = PySet_New(global);
869 if (!temp_global)
870 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
873 goto error;
874 temp = PyNumber_InPlaceOr(child_free, temp_free);
875 if (!temp)
876 goto error;
877 Py_DECREF(temp);
878 Py_DECREF(temp_bound);
879 Py_DECREF(temp_free);
880 Py_DECREF(temp_global);
881 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000882 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 Py_XDECREF(temp_bound);
884 Py_XDECREF(temp_free);
885 Py_XDECREF(temp_global);
886 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000887}
888
889static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890symtable_analyze(struct symtable *st)
891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 PyObject *free, *global;
893 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 free = PySet_New(NULL);
896 if (!free)
897 return 0;
898 global = PySet_New(NULL);
899 if (!global) {
900 Py_DECREF(free);
901 return 0;
902 }
903 r = analyze_block(st->st_top, NULL, free, global);
904 Py_DECREF(free);
905 Py_DECREF(global);
906 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907}
908
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000909/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 This reference is released when the block is exited, via the DECREF
911 in symtable_exit_block().
912*/
913
914static int
915symtable_exit_block(struct symtable *st, void *ast)
916{
Benjamin Peterson609da582011-06-29 22:52:39 -0500917 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918
Benjamin Peterson609da582011-06-29 22:52:39 -0500919 st->st_cur = NULL;
920 size = PyList_GET_SIZE(st->st_stack);
921 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500922 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500924 if (--size)
925 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 }
927 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928}
929
930static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000932 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933{
Benjamin Peterson609da582011-06-29 22:52:39 -0500934 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935
Benjamin Peterson609da582011-06-29 22:52:39 -0500936 ste = ste_new(st, name, block, ast, lineno, col_offset);
937 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500939 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
940 Py_DECREF(ste);
941 return 0;
942 }
943 prev = st->st_cur;
944 /* The entry is owned by the stack. Borrow it for st_cur. */
945 Py_DECREF(ste);
946 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000947 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 st->st_global = st->st_cur->ste_symbols;
949 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500950 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 return 0;
952 }
953 }
954 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955}
956
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000957static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958symtable_lookup(struct symtable *st, PyObject *name)
959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 PyObject *o;
961 PyObject *mangled = _Py_Mangle(st->st_private, name);
962 if (!mangled)
963 return 0;
964 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
965 Py_DECREF(mangled);
966 if (!o)
967 return 0;
968 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969}
970
971static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 PyObject *o;
975 PyObject *dict;
976 long val;
977 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978
Jeremy Hylton81e95022007-02-27 06:50:52 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 if (!mangled)
981 return 0;
982 dict = st->st_cur->ste_symbols;
983 if ((o = PyDict_GetItem(dict, mangled))) {
984 val = PyLong_AS_LONG(o);
985 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
986 /* Is it better to use 'mangled' or 'name' here? */
987 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +0200988 PyErr_SyntaxLocationObject(st->st_filename,
989 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -0400990 st->st_cur->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 goto error;
992 }
993 val |= flag;
994 } else
995 val = flag;
996 o = PyLong_FromLong(val);
997 if (o == NULL)
998 goto error;
999 if (PyDict_SetItem(dict, mangled, o) < 0) {
1000 Py_DECREF(o);
1001 goto error;
1002 }
1003 Py_DECREF(o);
1004
1005 if (flag & DEF_PARAM) {
1006 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1007 goto error;
1008 } else if (flag & DEF_GLOBAL) {
1009 /* XXX need to update DEF_GLOBAL for other flags too;
1010 perhaps only DEF_FREE_GLOBAL */
1011 val = flag;
1012 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1013 val |= PyLong_AS_LONG(o);
1014 }
1015 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 goto error;
1018 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1019 Py_DECREF(o);
1020 goto error;
1021 }
1022 Py_DECREF(o);
1023 }
1024 Py_DECREF(mangled);
1025 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001026
1027error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 Py_DECREF(mangled);
1029 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030}
1031
1032/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1033 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 function.
1035
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1037 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001038
1039 VISIT_QUIT macro returns the specified value exiting from the function but
1040 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041*/
1042
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001043#define VISIT_QUIT(ST, X) \
1044 return --(ST)->recursion_depth,(X)
1045
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001048 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001049
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 int i; \
1052 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1053 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1054 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1055 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001056 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 int i; \
1062 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1063 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1064 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1065 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001066 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001069
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001070#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001072 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001074 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001076 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001077 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001079}
1080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001082symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1083{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001084 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001085 int res;
1086 if (!st->st_cur->ste_directives) {
1087 st->st_cur->ste_directives = PyList_New(0);
1088 if (!st->st_cur->ste_directives)
1089 return 0;
1090 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001091 mangled = _Py_Mangle(st->st_private, name);
1092 if (!mangled)
1093 return 0;
1094 data = Py_BuildValue("(Nii)", mangled, s->lineno, s->col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001095 if (!data)
1096 return 0;
1097 res = PyList_Append(st->st_cur->ste_directives, data);
1098 Py_DECREF(data);
1099 return res == 0;
1100}
1101
1102
1103static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104symtable_visit_stmt(struct symtable *st, stmt_ty s)
1105{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001106 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001107 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001108 "maximum recursion depth exceeded during compilation");
1109 VISIT_QUIT(st, 0);
1110 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 switch (s->kind) {
1112 case FunctionDef_kind:
1113 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001114 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 if (s->v.FunctionDef.args->defaults)
1116 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1117 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001118 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001119 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1120 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001121 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 if (s->v.FunctionDef.decorator_list)
1123 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1124 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001125 FunctionBlock, (void *)s, s->lineno,
1126 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001127 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001128 VISIT(st, arguments, s->v.FunctionDef.args);
1129 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001131 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 break;
1133 case ClassDef_kind: {
1134 PyObject *tmp;
1135 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001136 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1138 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (s->v.ClassDef.decorator_list)
1140 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1141 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001142 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001143 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 tmp = st->st_private;
1145 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001146 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 st->st_private = tmp;
1148 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001149 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 break;
1151 }
1152 case Return_kind:
1153 if (s->v.Return.value) {
1154 VISIT(st, expr, s->v.Return.value);
1155 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 }
1157 break;
1158 case Delete_kind:
1159 VISIT_SEQ(st, expr, s->v.Delete.targets);
1160 break;
1161 case Assign_kind:
1162 VISIT_SEQ(st, expr, s->v.Assign.targets);
1163 VISIT(st, expr, s->v.Assign.value);
1164 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001165 case AnnAssign_kind:
1166 if (s->v.AnnAssign.target->kind == Name_kind) {
1167 expr_ty e_name = s->v.AnnAssign.target;
1168 long cur = symtable_lookup(st, e_name->v.Name.id);
1169 if (cur < 0) {
1170 VISIT_QUIT(st, 0);
1171 }
1172 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001173 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001174 && s->v.AnnAssign.simple) {
1175 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001176 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1177 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001178 PyErr_SyntaxLocationObject(st->st_filename,
1179 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001180 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001181 VISIT_QUIT(st, 0);
1182 }
1183 if (s->v.AnnAssign.simple &&
1184 !symtable_add_def(st, e_name->v.Name.id,
1185 DEF_ANNOT | DEF_LOCAL)) {
1186 VISIT_QUIT(st, 0);
1187 }
1188 else {
1189 if (s->v.AnnAssign.value
1190 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1191 VISIT_QUIT(st, 0);
1192 }
1193 }
1194 }
1195 else {
1196 VISIT(st, expr, s->v.AnnAssign.target);
1197 }
1198 VISIT(st, expr, s->v.AnnAssign.annotation);
1199 if (s->v.AnnAssign.value) {
1200 VISIT(st, expr, s->v.AnnAssign.value);
1201 }
1202 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 case AugAssign_kind:
1204 VISIT(st, expr, s->v.AugAssign.target);
1205 VISIT(st, expr, s->v.AugAssign.value);
1206 break;
1207 case For_kind:
1208 VISIT(st, expr, s->v.For.target);
1209 VISIT(st, expr, s->v.For.iter);
1210 VISIT_SEQ(st, stmt, s->v.For.body);
1211 if (s->v.For.orelse)
1212 VISIT_SEQ(st, stmt, s->v.For.orelse);
1213 break;
1214 case While_kind:
1215 VISIT(st, expr, s->v.While.test);
1216 VISIT_SEQ(st, stmt, s->v.While.body);
1217 if (s->v.While.orelse)
1218 VISIT_SEQ(st, stmt, s->v.While.orelse);
1219 break;
1220 case If_kind:
1221 /* XXX if 0: and lookup_yield() hacks */
1222 VISIT(st, expr, s->v.If.test);
1223 VISIT_SEQ(st, stmt, s->v.If.body);
1224 if (s->v.If.orelse)
1225 VISIT_SEQ(st, stmt, s->v.If.orelse);
1226 break;
1227 case Raise_kind:
1228 if (s->v.Raise.exc) {
1229 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001230 if (s->v.Raise.cause) {
1231 VISIT(st, expr, s->v.Raise.cause);
1232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 }
1234 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001235 case Try_kind:
1236 VISIT_SEQ(st, stmt, s->v.Try.body);
1237 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1238 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1239 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 break;
1241 case Assert_kind:
1242 VISIT(st, expr, s->v.Assert.test);
1243 if (s->v.Assert.msg)
1244 VISIT(st, expr, s->v.Assert.msg);
1245 break;
1246 case Import_kind:
1247 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 break;
1249 case ImportFrom_kind:
1250 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 break;
1252 case Global_kind: {
1253 int i;
1254 asdl_seq *seq = s->v.Global.names;
1255 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1256 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 long cur = symtable_lookup(st, name);
1258 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001259 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001260 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1261 const char* msg;
1262 if (cur & DEF_PARAM) {
1263 msg = GLOBAL_PARAM;
1264 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001265 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001266 } else if (cur & DEF_ANNOT) {
1267 msg = GLOBAL_ANNOT;
1268 } else { /* DEF_LOCAL */
1269 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001270 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001271 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001272 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001273 PyErr_SyntaxLocationObject(st->st_filename,
1274 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001275 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001276 VISIT_QUIT(st, 0);
1277 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001279 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001280 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001281 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 }
1283 break;
1284 }
1285 case Nonlocal_kind: {
1286 int i;
1287 asdl_seq *seq = s->v.Nonlocal.names;
1288 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1289 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 long cur = symtable_lookup(st, name);
1291 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001292 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001293 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1294 const char* msg;
1295 if (cur & DEF_PARAM) {
1296 msg = NONLOCAL_PARAM;
1297 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001298 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001299 } else if (cur & DEF_ANNOT) {
1300 msg = NONLOCAL_ANNOT;
1301 } else { /* DEF_LOCAL */
1302 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001303 }
1304 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001305 PyErr_SyntaxLocationObject(st->st_filename,
1306 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001307 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001308 VISIT_QUIT(st, 0);
1309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001311 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001312 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001313 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 }
1315 break;
1316 }
1317 case Expr_kind:
1318 VISIT(st, expr, s->v.Expr.value);
1319 break;
1320 case Pass_kind:
1321 case Break_kind:
1322 case Continue_kind:
1323 /* nothing to do here */
1324 break;
1325 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001326 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 VISIT_SEQ(st, stmt, s->v.With.body);
1328 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001329 case AsyncFunctionDef_kind:
1330 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1331 VISIT_QUIT(st, 0);
1332 if (s->v.AsyncFunctionDef.args->defaults)
1333 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1334 if (s->v.AsyncFunctionDef.args->kw_defaults)
1335 VISIT_SEQ_WITH_NULL(st, expr,
1336 s->v.AsyncFunctionDef.args->kw_defaults);
1337 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1338 s->v.AsyncFunctionDef.returns))
1339 VISIT_QUIT(st, 0);
1340 if (s->v.AsyncFunctionDef.decorator_list)
1341 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1342 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1343 FunctionBlock, (void *)s, s->lineno,
1344 s->col_offset))
1345 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001346 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001347 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1348 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1349 if (!symtable_exit_block(st, s))
1350 VISIT_QUIT(st, 0);
1351 break;
1352 case AsyncWith_kind:
1353 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1354 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1355 break;
1356 case AsyncFor_kind:
1357 VISIT(st, expr, s->v.AsyncFor.target);
1358 VISIT(st, expr, s->v.AsyncFor.iter);
1359 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1360 if (s->v.AsyncFor.orelse)
1361 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1362 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001364 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365}
1366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368symtable_visit_expr(struct symtable *st, expr_ty e)
1369{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001370 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001371 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001372 "maximum recursion depth exceeded during compilation");
1373 VISIT_QUIT(st, 0);
1374 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 switch (e->kind) {
1376 case BoolOp_kind:
1377 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1378 break;
1379 case BinOp_kind:
1380 VISIT(st, expr, e->v.BinOp.left);
1381 VISIT(st, expr, e->v.BinOp.right);
1382 break;
1383 case UnaryOp_kind:
1384 VISIT(st, expr, e->v.UnaryOp.operand);
1385 break;
1386 case Lambda_kind: {
1387 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001388 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 if (e->v.Lambda.args->defaults)
1390 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001391 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001392 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001394 FunctionBlock, (void *)e, e->lineno,
1395 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001396 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001397 VISIT(st, arguments, e->v.Lambda.args);
1398 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001400 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 break;
1402 }
1403 case IfExp_kind:
1404 VISIT(st, expr, e->v.IfExp.test);
1405 VISIT(st, expr, e->v.IfExp.body);
1406 VISIT(st, expr, e->v.IfExp.orelse);
1407 break;
1408 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001409 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 VISIT_SEQ(st, expr, e->v.Dict.values);
1411 break;
1412 case Set_kind:
1413 VISIT_SEQ(st, expr, e->v.Set.elts);
1414 break;
1415 case GeneratorExp_kind:
1416 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001417 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 break;
1419 case ListComp_kind:
1420 if (!symtable_visit_listcomp(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 SetComp_kind:
1424 if (!symtable_visit_setcomp(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 DictComp_kind:
1428 if (!symtable_visit_dictcomp(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 Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001432 if (e->v.Yield.value)
1433 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001436 case YieldFrom_kind:
1437 VISIT(st, expr, e->v.YieldFrom.value);
1438 st->st_cur->ste_generator = 1;
1439 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001440 case Await_kind:
1441 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001442 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001443 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 case Compare_kind:
1445 VISIT(st, expr, e->v.Compare.left);
1446 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1447 break;
1448 case Call_kind:
1449 VISIT(st, expr, e->v.Call.func);
1450 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001451 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001453 case FormattedValue_kind:
1454 VISIT(st, expr, e->v.FormattedValue.value);
1455 if (e->v.FormattedValue.format_spec)
1456 VISIT(st, expr, e->v.FormattedValue.format_spec);
1457 break;
1458 case JoinedStr_kind:
1459 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1460 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001461 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 /* Nothing to do here. */
1463 break;
1464 /* The following exprs can be assignment targets. */
1465 case Attribute_kind:
1466 VISIT(st, expr, e->v.Attribute.value);
1467 break;
1468 case Subscript_kind:
1469 VISIT(st, expr, e->v.Subscript.value);
1470 VISIT(st, slice, e->v.Subscript.slice);
1471 break;
1472 case Starred_kind:
1473 VISIT(st, expr, e->v.Starred.value);
1474 break;
1475 case Name_kind:
1476 if (!symtable_add_def(st, e->v.Name.id,
1477 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001478 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 /* Special-case super: it counts as a use of __class__ */
1480 if (e->v.Name.ctx == Load &&
1481 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001482 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001483 if (!GET_IDENTIFIER(__class__) ||
1484 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001485 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 }
1487 break;
1488 /* child nodes of List and Tuple will have expr_context set */
1489 case List_kind:
1490 VISIT_SEQ(st, expr, e->v.List.elts);
1491 break;
1492 case Tuple_kind:
1493 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1494 break;
1495 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001496 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497}
1498
1499static int
1500symtable_implicit_arg(struct symtable *st, int pos)
1501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1503 if (id == NULL)
1504 return 0;
1505 if (!symtable_add_def(st, id, DEF_PARAM)) {
1506 Py_DECREF(id);
1507 return 0;
1508 }
1509 Py_DECREF(id);
1510 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511}
1512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001514symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 if (!args)
1519 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 for (i = 0; i < asdl_seq_LEN(args); i++) {
1522 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1523 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1524 return 0;
1525 }
1526
1527 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528}
1529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001531symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 if (!args)
1536 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 for (i = 0; i < asdl_seq_LEN(args); i++) {
1539 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1540 if (arg->annotation)
1541 VISIT(st, expr, arg->annotation);
1542 }
1543
1544 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001545}
1546
Neal Norwitzc1505362006-12-28 06:47:50 +00001547static int
Yury Selivanov75445082015-05-11 22:57:16 -04001548symtable_visit_annotations(struct symtable *st, stmt_ty s,
1549 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 if (a->args && !symtable_visit_argannotations(st, a->args))
1552 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001553 if (a->vararg && a->vararg->annotation)
1554 VISIT(st, expr, a->vararg->annotation);
1555 if (a->kwarg && a->kwarg->annotation)
1556 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1558 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001559 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001560 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562}
1563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565symtable_visit_arguments(struct symtable *st, arguments_ty a)
1566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 /* skip default arguments inside function block
1568 XXX should ast be different?
1569 */
1570 if (a->args && !symtable_visit_params(st, a->args))
1571 return 0;
1572 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1573 return 0;
1574 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001575 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 return 0;
1577 st->st_cur->ste_varargs = 1;
1578 }
1579 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001580 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 return 0;
1582 st->st_cur->ste_varkeywords = 1;
1583 }
1584 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585}
1586
1587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 if (eh->v.ExceptHandler.type)
1592 VISIT(st, expr, eh->v.ExceptHandler.type);
1593 if (eh->v.ExceptHandler.name)
1594 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1595 return 0;
1596 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1597 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598}
1599
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001600static int
1601symtable_visit_withitem(struct symtable *st, withitem_ty item)
1602{
1603 VISIT(st, expr, item->context_expr);
1604 if (item->optional_vars) {
1605 VISIT(st, expr, item->optional_vars);
1606 }
1607 return 1;
1608}
1609
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612symtable_visit_alias(struct symtable *st, alias_ty a)
1613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001615 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 dotted package name (e.g. spam.eggs)
1617 */
1618 PyObject *store_name;
1619 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001620 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1621 PyUnicode_GET_LENGTH(name), 1);
1622 if (dot != -1) {
1623 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (!store_name)
1625 return 0;
1626 }
1627 else {
1628 store_name = name;
1629 Py_INCREF(store_name);
1630 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001631 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1633 Py_DECREF(store_name);
1634 return r;
1635 }
1636 else {
1637 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001638 int lineno = st->st_cur->ste_lineno;
1639 int col_offset = st->st_cur->ste_col_offset;
1640 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001641 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001642 Py_DECREF(store_name);
1643 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 Py_DECREF(store_name);
1646 return 1;
1647 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648}
1649
1650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 VISIT(st, expr, lc->target);
1655 VISIT(st, expr, lc->iter);
1656 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001657 if (lc->is_async) {
1658 st->st_cur->ste_coroutine = 1;
1659 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661}
1662
1663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665symtable_visit_keyword(struct symtable *st, keyword_ty k)
1666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 VISIT(st, expr, k->value);
1668 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669}
1670
1671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673symtable_visit_slice(struct symtable *st, slice_ty s)
1674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 switch (s->kind) {
1676 case Slice_kind:
1677 if (s->v.Slice.lower)
1678 VISIT(st, expr, s->v.Slice.lower)
1679 if (s->v.Slice.upper)
1680 VISIT(st, expr, s->v.Slice.upper)
1681 if (s->v.Slice.step)
1682 VISIT(st, expr, s->v.Slice.step)
1683 break;
1684 case ExtSlice_kind:
1685 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1686 break;
1687 case Index_kind:
1688 VISIT(st, expr, s->v.Index.value)
1689 break;
1690 }
1691 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692}
1693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001695symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001696 identifier scope_name, asdl_seq *generators,
1697 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 comprehension_ty outermost = ((comprehension_ty)
1701 asdl_seq_GET(generators, 0));
1702 /* Outermost iterator is evaluated in current scope */
1703 VISIT(st, expr, outermost->iter);
1704 /* Create comprehension scope for the rest */
1705 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001706 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1707 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 return 0;
1709 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001710 if (outermost->is_async) {
1711 st->st_cur->ste_coroutine = 1;
1712 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 /* Outermost iter is received as an argument */
1714 if (!symtable_implicit_arg(st, 0)) {
1715 symtable_exit_block(st, (void *)e);
1716 return 0;
1717 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001718 VISIT(st, expr, outermost->target);
1719 VISIT_SEQ(st, expr, outermost->ifs);
1720 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001722 VISIT(st, expr, value);
1723 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001724 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001725 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001726 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1727 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1728 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1729 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001730 PyErr_SyntaxLocationObject(st->st_filename,
1731 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001732 st->st_cur->ste_col_offset + 1);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001733 symtable_exit_block(st, (void *)e);
1734 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001735 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001736 st->st_cur->ste_generator = is_generator;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001741symtable_visit_genexp(struct symtable *st, expr_ty e)
1742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1744 e->v.GeneratorExp.generators,
1745 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001746}
1747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001749symtable_visit_listcomp(struct symtable *st, expr_ty e)
1750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1752 e->v.ListComp.generators,
1753 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001754}
1755
1756static int
1757symtable_visit_setcomp(struct symtable *st, expr_ty e)
1758{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1760 e->v.SetComp.generators,
1761 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001762}
1763
1764static int
1765symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1768 e->v.DictComp.generators,
1769 e->v.DictComp.key,
1770 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001771}