blob: 96f7bcda5e2665987a1188c69ee8820988bcd69e [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));
Zackery Spytzad65f152018-11-16 08:58:55 -0700213 if (st == NULL) {
214 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700216 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 st->st_filename = NULL;
219 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if ((st->st_stack = PyList_New(0)) == NULL)
222 goto fail;
223 if ((st->st_blocks = PyDict_New()) == NULL)
224 goto fail;
225 st->st_cur = NULL;
226 st->st_private = NULL;
227 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 PySymtable_Free(st);
230 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231}
232
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000233/* When compiling the use of C stack is probably going to be a lot
234 lighter than when executing Python code but still can overflow
235 and causing a Python crash if not checked (e.g. eval("()"*300000)).
236 Using the current recursion limit for the compiler seems too
237 restrictive (it caused at least one test to fail) so a factor is
238 used to allow deeper recursion when compiling an expression.
239
240 Using a scaling factor means this should automatically adjust when
241 the recursion limit is adjusted for small or large C stack allocations.
242*/
243#define COMPILER_STACK_FRAME_SCALE 3
244
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200246PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000248 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 asdl_seq *seq;
250 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000251 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400252 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200255 return NULL;
256 if (filename == NULL) {
257 PySymtable_Free(st);
258 return NULL;
259 }
260 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 st->st_filename = filename;
262 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000263
264 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100265 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000266 if (!tstate) {
267 PySymtable_Free(st);
268 return NULL;
269 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400270 /* Be careful here to prevent overflow. */
271 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
272 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
273 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
274 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 /* Make the initial symbol information gathering pass */
277 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000278 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 PySymtable_Free(st);
280 return NULL;
281 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 switch (mod->kind) {
285 case Module_kind:
286 seq = mod->v.Module.body;
287 for (i = 0; i < asdl_seq_LEN(seq); i++)
288 if (!symtable_visit_stmt(st,
289 (stmt_ty)asdl_seq_GET(seq, i)))
290 goto error;
291 break;
292 case Expression_kind:
293 if (!symtable_visit_expr(st, mod->v.Expression.body))
294 goto error;
295 break;
296 case Interactive_kind:
297 seq = mod->v.Interactive.body;
298 for (i = 0; i < asdl_seq_LEN(seq); i++)
299 if (!symtable_visit_stmt(st,
300 (stmt_ty)asdl_seq_GET(seq, i)))
301 goto error;
302 break;
303 case Suite_kind:
304 PyErr_SetString(PyExc_RuntimeError,
305 "this compiler does not handle Suites");
306 goto error;
307 }
308 if (!symtable_exit_block(st, (void *)mod)) {
309 PySymtable_Free(st);
310 return NULL;
311 }
312 /* Make the second symbol analysis pass */
313 if (symtable_analyze(st))
314 return st;
315 PySymtable_Free(st);
316 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 (void) symtable_exit_block(st, (void *)mod);
319 PySymtable_Free(st);
320 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321}
322
Victor Stinner14e461d2013-08-26 22:28:21 +0200323struct symtable *
324PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
325{
326 PyObject *filename;
327 struct symtable *st;
328 filename = PyUnicode_DecodeFSDefault(filename_str);
329 if (filename == NULL)
330 return NULL;
331 st = PySymtable_BuildObject(mod, filename, future);
332 Py_DECREF(filename);
333 return st;
334}
335
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336void
337PySymtable_Free(struct symtable *st)
338{
Victor Stinner14e461d2013-08-26 22:28:21 +0200339 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 Py_XDECREF(st->st_blocks);
341 Py_XDECREF(st->st_stack);
342 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343}
344
345PySTEntryObject *
346PySymtable_Lookup(struct symtable *st, void *key)
347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 k = PyLong_FromVoidPtr(key);
351 if (k == NULL)
352 return NULL;
353 v = PyDict_GetItem(st->st_blocks, k);
354 if (v) {
355 assert(PySTEntry_Check(v));
356 Py_INCREF(v);
357 }
358 else {
359 PyErr_SetString(PyExc_KeyError,
360 "unknown symbol table entry");
361 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 Py_DECREF(k);
364 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365}
366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368PyST_GetScope(PySTEntryObject *ste, PyObject *name)
369{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
371 if (!v)
372 return 0;
373 assert(PyLong_Check(v));
374 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375}
376
Benjamin Petersond9c87022012-10-31 20:26:20 -0400377static int
378error_at_directive(PySTEntryObject *ste, PyObject *name)
379{
380 Py_ssize_t i;
381 PyObject *data;
382 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600383 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400384 data = PyList_GET_ITEM(ste->ste_directives, i);
385 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600386 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
387 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
388 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
389 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
Ammar Askar025eb982018-09-24 17:12:49 -0400390 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600391
392 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700393 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400394 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600395 PyErr_SetString(PyExc_RuntimeError,
396 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400397 return 0;
398}
399
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400
401/* Analyze raw symbol information to determine scope of each name.
402
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000403 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409 explicit global is declared with the global statement. An implicit
410 global is a free variable for which the compiler has found no binding
411 in an enclosing function scope. The implicit global is either a global
412 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
413 to handle these names to implement slightly odd semantics. In such a
414 block, the name is treated as global until it is assigned to; then it
415 is treated as a local.
416
417 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000418 The first pass collects raw facts from the AST via the symtable_visit_*
419 functions: the name is a parameter here, the name is used but not defined
420 here, etc. The second pass analyzes these facts during a pass over the
421 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422
423 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000425 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000426 Names which are explicitly declared nonlocal must exist in this set of
427 visible names - if they do not, a syntax error is raised. After doing
428 the local analysis, it analyzes each of its child blocks using an
429 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430
Nick Coghlan650f0d02007-04-15 12:05:43 +0000431 The children update the free variable set. If a local variable is added to
432 the free variable set by the child, the variable is marked as a cell. The
433 function object being defined must provide runtime storage for the variable
434 that may outlive the function's frame. Cell variables are removed from the
435 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000436
Nick Coghlan650f0d02007-04-15 12:05:43 +0000437 During analysis, the names are:
438 symbols: dict mapping from symbol names to flag values (including offset scope values)
439 scopes: dict mapping from symbol names to scope values (no offset)
440 local: set of all symbol names local to the current scope
441 bound: set of all symbol names local to a containing function scope
442 free: set of all symbol names referenced but not bound in child scopes
443 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444*/
445
446#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 PyObject *o = PyLong_FromLong(I); \
448 if (!o) \
449 return 0; \
450 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
451 Py_DECREF(o); \
452 return 0; \
453 } \
454 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455}
456
457/* Decide on scope of name, given flags.
458
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000459 The namespace dictionaries may be modified to record information
460 about the new name. For example, a new global will add an entry to
461 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462*/
463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000465analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 PyObject *bound, PyObject *local, PyObject *free,
467 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (flags & DEF_NONLOCAL) {
471 PyErr_Format(PyExc_SyntaxError,
472 "name '%U' is nonlocal and global",
473 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400474 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 }
476 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
477 if (PySet_Add(global, name) < 0)
478 return 0;
479 if (bound && (PySet_Discard(bound, name) < 0))
480 return 0;
481 return 1;
482 }
483 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 if (!bound) {
485 PyErr_Format(PyExc_SyntaxError,
486 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400487 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 }
489 if (!PySet_Contains(bound, name)) {
490 PyErr_Format(PyExc_SyntaxError,
491 "no binding for nonlocal '%U' found",
492 name);
493
Benjamin Petersond9c87022012-10-31 20:26:20 -0400494 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 }
496 SET_SCOPE(scopes, name, FREE);
497 ste->ste_free = 1;
498 return PySet_Add(free, name) >= 0;
499 }
500 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000501 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (PySet_Add(local, name) < 0)
503 return 0;
504 if (PySet_Discard(global, name) < 0)
505 return 0;
506 return 1;
507 }
508 /* If an enclosing block has a binding for this name, it
509 is a free variable rather than a global variable.
510 Note that having a non-NULL bound implies that the block
511 is nested.
512 */
513 if (bound && PySet_Contains(bound, name)) {
514 SET_SCOPE(scopes, name, FREE);
515 ste->ste_free = 1;
516 return PySet_Add(free, name) >= 0;
517 }
518 /* If a parent has a global statement, then call it global
519 explicit? It could also be global implicit.
520 */
521 if (global && PySet_Contains(global, name)) {
522 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
523 return 1;
524 }
525 if (ste->ste_nested)
526 ste->ste_free = 1;
527 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
528 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529}
530
531#undef SET_SCOPE
532
533/* If a name is defined in free and also in locals, then this block
534 provides the binding for the free variable. The name should be
535 marked CELL in this block and removed from the free list.
536
537 Note that the current block's free variables are included in free.
538 That's safe because no name can be free and local in the same scope.
539*/
540
541static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500542analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 PyObject *name, *v, *v_cell;
545 int success = 0;
546 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 v_cell = PyLong_FromLong(CELL);
549 if (!v_cell)
550 return 0;
551 while (PyDict_Next(scopes, &pos, &name, &v)) {
552 long scope;
553 assert(PyLong_Check(v));
554 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000555 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 continue;
557 if (!PySet_Contains(free, name))
558 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* Replace LOCAL with CELL for this name, and remove
560 from free. It is safe to replace the value of name
561 in the dict, because it will not cause a resize.
562 */
563 if (PyDict_SetItem(scopes, name, v_cell) < 0)
564 goto error;
565 if (PySet_Discard(free, name) < 0)
566 goto error;
567 }
568 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 Py_DECREF(v_cell);
571 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572}
573
Benjamin Peterson312595c2013-05-15 15:26:42 -0500574static int
575drop_class_free(PySTEntryObject *ste, PyObject *free)
576{
577 int res;
578 if (!GET_IDENTIFIER(__class__))
579 return 0;
580 res = PySet_Discard(free, __class__);
581 if (res < 0)
582 return 0;
583 if (res)
584 ste->ste_needs_class_closure = 1;
585 return 1;
586}
587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588/* Enter the final scope information into the ste_symbols dict.
589 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590 * All arguments are dicts. Modifies symbols, others are read-only.
591*/
592static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000594 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 PyObject *name = NULL, *itr = NULL;
597 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
598 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 /* Update scope information for all symbols in this scope */
601 while (PyDict_Next(symbols, &pos, &name, &v)) {
602 long scope, flags;
603 assert(PyLong_Check(v));
604 flags = PyLong_AS_LONG(v);
605 v_scope = PyDict_GetItem(scopes, name);
606 assert(v_scope && PyLong_Check(v_scope));
607 scope = PyLong_AS_LONG(v_scope);
608 flags |= (scope << SCOPE_OFFSET);
609 v_new = PyLong_FromLong(flags);
610 if (!v_new)
611 return 0;
612 if (PyDict_SetItem(symbols, name, v_new) < 0) {
613 Py_DECREF(v_new);
614 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 Py_DECREF(v_new);
617 }
618
619 /* Record not yet resolved free variables from children (if any) */
620 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
621 if (!v_free)
622 return 0;
623
624 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600625 if (itr == NULL) {
626 Py_DECREF(v_free);
627 return 0;
628 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629
630 while ((name = PyIter_Next(itr))) {
631 v = PyDict_GetItem(symbols, name);
632
633 /* Handle symbol that already exists in this scope */
634 if (v) {
635 /* Handle a free variable in a method of
636 the class that has the same name as a local
637 or global in the class scope.
638 */
639 if (classflag &&
640 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
641 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
642 v_new = PyLong_FromLong(flags);
643 if (!v_new) {
644 goto error;
645 }
646 if (PyDict_SetItem(symbols, name, v_new) < 0) {
647 Py_DECREF(v_new);
648 goto error;
649 }
650 Py_DECREF(v_new);
651 }
652 /* It's a cell, or already free in this scope */
653 Py_DECREF(name);
654 continue;
655 }
656 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200657 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 Py_DECREF(name);
659 continue; /* it's a global */
660 }
661 /* Propagate new free symbol up the lexical stack */
662 if (PyDict_SetItem(symbols, name, v_free) < 0) {
663 goto error;
664 }
665 Py_DECREF(name);
666 }
667 Py_DECREF(itr);
668 Py_DECREF(v_free);
669 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000670error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 Py_XDECREF(v_free);
672 Py_XDECREF(itr);
673 Py_XDECREF(name);
674 return 0;
675}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676
677/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000678
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 Arguments:
680 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000681 bound -- set of variables bound in enclosing scopes (input). bound
682 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683 free -- set of free variables in enclosed scopes (output)
684 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000685
686 The implementation uses two mutually recursive functions,
687 analyze_block() and analyze_child_block(). analyze_block() is
688 responsible for analyzing the individual names defined in a block.
689 analyze_child_block() prepares temporary namespace dictionaries
690 used to evaluated nested blocks.
691
692 The two functions exist because a child block should see the name
693 bindings of its enclosing blocks, but those bindings should not
694 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695*/
696
697static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
699 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000700
701static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
703 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
706 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
707 PyObject *temp;
708 int i, success = 0;
709 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 local = PySet_New(NULL); /* collect new names bound in block */
712 if (!local)
713 goto error;
714 scopes = PyDict_New(); /* collect scopes defined for each name */
715 if (!scopes)
716 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 /* Allocate new global and bound variable dictionaries. These
719 dictionaries hold the names visible in nested blocks. For
720 ClassBlocks, the bound and global names are initialized
721 before analyzing names, because class bindings aren't
722 visible in methods. For other blocks, they are initialized
723 after names are analyzed.
724 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 /* TODO(jhylton): Package these dicts in a struct so that we
727 can write reasonable helper functions?
728 */
729 newglobal = PySet_New(NULL);
730 if (!newglobal)
731 goto error;
732 newfree = PySet_New(NULL);
733 if (!newfree)
734 goto error;
735 newbound = PySet_New(NULL);
736 if (!newbound)
737 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 /* Class namespace has no effect on names visible in
740 nested functions, so populate the global and bound
741 sets to be passed to child blocks before analyzing
742 this one.
743 */
744 if (ste->ste_type == ClassBlock) {
745 /* Pass down known globals */
746 temp = PyNumber_InPlaceOr(newglobal, global);
747 if (!temp)
748 goto error;
749 Py_DECREF(temp);
750 /* Pass down previously bound symbols */
751 if (bound) {
752 temp = PyNumber_InPlaceOr(newbound, bound);
753 if (!temp)
754 goto error;
755 Py_DECREF(temp);
756 }
757 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
760 long flags = PyLong_AS_LONG(v);
761 if (!analyze_name(ste, scopes, name, flags,
762 bound, local, free, global))
763 goto error;
764 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 /* Populate global and bound sets to be passed to children. */
767 if (ste->ste_type != ClassBlock) {
768 /* Add function locals to bound set */
769 if (ste->ste_type == FunctionBlock) {
770 temp = PyNumber_InPlaceOr(newbound, local);
771 if (!temp)
772 goto error;
773 Py_DECREF(temp);
774 }
775 /* Pass down previously bound symbols */
776 if (bound) {
777 temp = PyNumber_InPlaceOr(newbound, bound);
778 if (!temp)
779 goto error;
780 Py_DECREF(temp);
781 }
782 /* Pass down known globals */
783 temp = PyNumber_InPlaceOr(newglobal, global);
784 if (!temp)
785 goto error;
786 Py_DECREF(temp);
787 }
788 else {
789 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000790 if (!GET_IDENTIFIER(__class__))
791 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 if (PySet_Add(newbound, __class__) < 0)
793 goto error;
794 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300796 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 newbound, newglobal now contain the names visible in
799 nested blocks. The free variables in the children will
800 be collected in allfree.
801 */
802 allfree = PySet_New(NULL);
803 if (!allfree)
804 goto error;
805 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
806 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
807 PySTEntryObject* entry;
808 assert(c && PySTEntry_Check(c));
809 entry = (PySTEntryObject*)c;
810 if (!analyze_child_block(entry, newbound, newfree, newglobal,
811 allfree))
812 goto error;
813 /* Check if any children have free variables */
814 if (entry->ste_free || entry->ste_child_free)
815 ste->ste_child_free = 1;
816 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 temp = PyNumber_InPlaceOr(newfree, allfree);
819 if (!temp)
820 goto error;
821 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500824 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500826 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 goto error;
828 /* Records the results of the analysis in the symbol table entry */
829 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
830 ste->ste_type == ClassBlock))
831 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 temp = PyNumber_InPlaceOr(free, newfree);
834 if (!temp)
835 goto error;
836 Py_DECREF(temp);
837 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 Py_XDECREF(scopes);
840 Py_XDECREF(local);
841 Py_XDECREF(newbound);
842 Py_XDECREF(newglobal);
843 Py_XDECREF(newfree);
844 Py_XDECREF(allfree);
845 if (!success)
846 assert(PyErr_Occurred());
847 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848}
849
850static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
852 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
855 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000858
Martin Panter3ee62702016-06-04 04:57:19 +0000859 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 current block. The analyze_block() call modifies these
861 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 */
864 temp_bound = PySet_New(bound);
865 if (!temp_bound)
866 goto error;
867 temp_free = PySet_New(free);
868 if (!temp_free)
869 goto error;
870 temp_global = PySet_New(global);
871 if (!temp_global)
872 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
875 goto error;
876 temp = PyNumber_InPlaceOr(child_free, temp_free);
877 if (!temp)
878 goto error;
879 Py_DECREF(temp);
880 Py_DECREF(temp_bound);
881 Py_DECREF(temp_free);
882 Py_DECREF(temp_global);
883 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000884 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 Py_XDECREF(temp_bound);
886 Py_XDECREF(temp_free);
887 Py_XDECREF(temp_global);
888 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000889}
890
891static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892symtable_analyze(struct symtable *st)
893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 PyObject *free, *global;
895 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 free = PySet_New(NULL);
898 if (!free)
899 return 0;
900 global = PySet_New(NULL);
901 if (!global) {
902 Py_DECREF(free);
903 return 0;
904 }
905 r = analyze_block(st->st_top, NULL, free, global);
906 Py_DECREF(free);
907 Py_DECREF(global);
908 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909}
910
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000911/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 This reference is released when the block is exited, via the DECREF
913 in symtable_exit_block().
914*/
915
916static int
917symtable_exit_block(struct symtable *st, void *ast)
918{
Benjamin Peterson609da582011-06-29 22:52:39 -0500919 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920
Benjamin Peterson609da582011-06-29 22:52:39 -0500921 st->st_cur = NULL;
922 size = PyList_GET_SIZE(st->st_stack);
923 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500924 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500926 if (--size)
927 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 }
929 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930}
931
932static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000934 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935{
Benjamin Peterson609da582011-06-29 22:52:39 -0500936 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937
Benjamin Peterson609da582011-06-29 22:52:39 -0500938 ste = ste_new(st, name, block, ast, lineno, col_offset);
939 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500941 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
942 Py_DECREF(ste);
943 return 0;
944 }
945 prev = st->st_cur;
946 /* The entry is owned by the stack. Borrow it for st_cur. */
947 Py_DECREF(ste);
948 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000949 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 st->st_global = st->st_cur->ste_symbols;
951 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500952 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 return 0;
954 }
955 }
956 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957}
958
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000959static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960symtable_lookup(struct symtable *st, PyObject *name)
961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 PyObject *o;
963 PyObject *mangled = _Py_Mangle(st->st_private, name);
964 if (!mangled)
965 return 0;
966 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
967 Py_DECREF(mangled);
968 if (!o)
969 return 0;
970 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971}
972
973static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 PyObject *o;
977 PyObject *dict;
978 long val;
979 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980
Jeremy Hylton81e95022007-02-27 06:50:52 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 if (!mangled)
983 return 0;
984 dict = st->st_cur->ste_symbols;
985 if ((o = PyDict_GetItem(dict, mangled))) {
986 val = PyLong_AS_LONG(o);
987 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
988 /* Is it better to use 'mangled' or 'name' here? */
989 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +0200990 PyErr_SyntaxLocationObject(st->st_filename,
991 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -0400992 st->st_cur->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 goto error;
994 }
995 val |= flag;
996 } else
997 val = flag;
998 o = PyLong_FromLong(val);
999 if (o == NULL)
1000 goto error;
1001 if (PyDict_SetItem(dict, mangled, o) < 0) {
1002 Py_DECREF(o);
1003 goto error;
1004 }
1005 Py_DECREF(o);
1006
1007 if (flag & DEF_PARAM) {
1008 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1009 goto error;
1010 } else if (flag & DEF_GLOBAL) {
1011 /* XXX need to update DEF_GLOBAL for other flags too;
1012 perhaps only DEF_FREE_GLOBAL */
1013 val = flag;
1014 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1015 val |= PyLong_AS_LONG(o);
1016 }
1017 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 goto error;
1020 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1021 Py_DECREF(o);
1022 goto error;
1023 }
1024 Py_DECREF(o);
1025 }
1026 Py_DECREF(mangled);
1027 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001028
1029error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 Py_DECREF(mangled);
1031 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032}
1033
1034/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1035 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 function.
1037
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1039 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001040
1041 VISIT_QUIT macro returns the specified value exiting from the function but
1042 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043*/
1044
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001045#define VISIT_QUIT(ST, X) \
1046 return --(ST)->recursion_depth,(X)
1047
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001050 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 int i; \
1054 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1055 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1056 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1057 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001058 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 int i; \
1064 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1065 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1066 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1067 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001068 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001071
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001072#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001074 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001076 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001078 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001079 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001081}
1082
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001084symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1085{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001086 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001087 int res;
1088 if (!st->st_cur->ste_directives) {
1089 st->st_cur->ste_directives = PyList_New(0);
1090 if (!st->st_cur->ste_directives)
1091 return 0;
1092 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001093 mangled = _Py_Mangle(st->st_private, name);
1094 if (!mangled)
1095 return 0;
1096 data = Py_BuildValue("(Nii)", mangled, s->lineno, s->col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001097 if (!data)
1098 return 0;
1099 res = PyList_Append(st->st_cur->ste_directives, data);
1100 Py_DECREF(data);
1101 return res == 0;
1102}
1103
1104
1105static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106symtable_visit_stmt(struct symtable *st, stmt_ty s)
1107{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001108 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001109 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001110 "maximum recursion depth exceeded during compilation");
1111 VISIT_QUIT(st, 0);
1112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 switch (s->kind) {
1114 case FunctionDef_kind:
1115 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001116 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 if (s->v.FunctionDef.args->defaults)
1118 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1119 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001120 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001121 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1122 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001123 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 if (s->v.FunctionDef.decorator_list)
1125 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1126 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001127 FunctionBlock, (void *)s, s->lineno,
1128 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001129 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001130 VISIT(st, arguments, s->v.FunctionDef.args);
1131 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001133 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 break;
1135 case ClassDef_kind: {
1136 PyObject *tmp;
1137 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001138 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1140 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 if (s->v.ClassDef.decorator_list)
1142 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1143 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001144 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001145 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 tmp = st->st_private;
1147 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001148 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 st->st_private = tmp;
1150 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001151 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 break;
1153 }
1154 case Return_kind:
1155 if (s->v.Return.value) {
1156 VISIT(st, expr, s->v.Return.value);
1157 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 }
1159 break;
1160 case Delete_kind:
1161 VISIT_SEQ(st, expr, s->v.Delete.targets);
1162 break;
1163 case Assign_kind:
1164 VISIT_SEQ(st, expr, s->v.Assign.targets);
1165 VISIT(st, expr, s->v.Assign.value);
1166 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001167 case AnnAssign_kind:
1168 if (s->v.AnnAssign.target->kind == Name_kind) {
1169 expr_ty e_name = s->v.AnnAssign.target;
1170 long cur = symtable_lookup(st, e_name->v.Name.id);
1171 if (cur < 0) {
1172 VISIT_QUIT(st, 0);
1173 }
1174 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001175 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001176 && s->v.AnnAssign.simple) {
1177 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001178 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1179 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001180 PyErr_SyntaxLocationObject(st->st_filename,
1181 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001182 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001183 VISIT_QUIT(st, 0);
1184 }
1185 if (s->v.AnnAssign.simple &&
1186 !symtable_add_def(st, e_name->v.Name.id,
1187 DEF_ANNOT | DEF_LOCAL)) {
1188 VISIT_QUIT(st, 0);
1189 }
1190 else {
1191 if (s->v.AnnAssign.value
1192 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1193 VISIT_QUIT(st, 0);
1194 }
1195 }
1196 }
1197 else {
1198 VISIT(st, expr, s->v.AnnAssign.target);
1199 }
1200 VISIT(st, expr, s->v.AnnAssign.annotation);
1201 if (s->v.AnnAssign.value) {
1202 VISIT(st, expr, s->v.AnnAssign.value);
1203 }
1204 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 case AugAssign_kind:
1206 VISIT(st, expr, s->v.AugAssign.target);
1207 VISIT(st, expr, s->v.AugAssign.value);
1208 break;
1209 case For_kind:
1210 VISIT(st, expr, s->v.For.target);
1211 VISIT(st, expr, s->v.For.iter);
1212 VISIT_SEQ(st, stmt, s->v.For.body);
1213 if (s->v.For.orelse)
1214 VISIT_SEQ(st, stmt, s->v.For.orelse);
1215 break;
1216 case While_kind:
1217 VISIT(st, expr, s->v.While.test);
1218 VISIT_SEQ(st, stmt, s->v.While.body);
1219 if (s->v.While.orelse)
1220 VISIT_SEQ(st, stmt, s->v.While.orelse);
1221 break;
1222 case If_kind:
1223 /* XXX if 0: and lookup_yield() hacks */
1224 VISIT(st, expr, s->v.If.test);
1225 VISIT_SEQ(st, stmt, s->v.If.body);
1226 if (s->v.If.orelse)
1227 VISIT_SEQ(st, stmt, s->v.If.orelse);
1228 break;
1229 case Raise_kind:
1230 if (s->v.Raise.exc) {
1231 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001232 if (s->v.Raise.cause) {
1233 VISIT(st, expr, s->v.Raise.cause);
1234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
1236 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001237 case Try_kind:
1238 VISIT_SEQ(st, stmt, s->v.Try.body);
1239 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1240 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1241 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 break;
1243 case Assert_kind:
1244 VISIT(st, expr, s->v.Assert.test);
1245 if (s->v.Assert.msg)
1246 VISIT(st, expr, s->v.Assert.msg);
1247 break;
1248 case Import_kind:
1249 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 break;
1251 case ImportFrom_kind:
1252 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 break;
1254 case Global_kind: {
1255 int i;
1256 asdl_seq *seq = s->v.Global.names;
1257 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1258 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 long cur = symtable_lookup(st, name);
1260 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001261 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001262 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1263 const char* msg;
1264 if (cur & DEF_PARAM) {
1265 msg = GLOBAL_PARAM;
1266 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001267 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001268 } else if (cur & DEF_ANNOT) {
1269 msg = GLOBAL_ANNOT;
1270 } else { /* DEF_LOCAL */
1271 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001272 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001273 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001274 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001275 PyErr_SyntaxLocationObject(st->st_filename,
1276 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001277 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001278 VISIT_QUIT(st, 0);
1279 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001281 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001282 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001283 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 }
1285 break;
1286 }
1287 case Nonlocal_kind: {
1288 int i;
1289 asdl_seq *seq = s->v.Nonlocal.names;
1290 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1291 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 long cur = symtable_lookup(st, name);
1293 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001294 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001295 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1296 const char* msg;
1297 if (cur & DEF_PARAM) {
1298 msg = NONLOCAL_PARAM;
1299 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001300 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001301 } else if (cur & DEF_ANNOT) {
1302 msg = NONLOCAL_ANNOT;
1303 } else { /* DEF_LOCAL */
1304 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001305 }
1306 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001307 PyErr_SyntaxLocationObject(st->st_filename,
1308 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001309 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001310 VISIT_QUIT(st, 0);
1311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001313 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001314 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001315 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 }
1317 break;
1318 }
1319 case Expr_kind:
1320 VISIT(st, expr, s->v.Expr.value);
1321 break;
1322 case Pass_kind:
1323 case Break_kind:
1324 case Continue_kind:
1325 /* nothing to do here */
1326 break;
1327 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001328 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 VISIT_SEQ(st, stmt, s->v.With.body);
1330 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001331 case AsyncFunctionDef_kind:
1332 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1333 VISIT_QUIT(st, 0);
1334 if (s->v.AsyncFunctionDef.args->defaults)
1335 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1336 if (s->v.AsyncFunctionDef.args->kw_defaults)
1337 VISIT_SEQ_WITH_NULL(st, expr,
1338 s->v.AsyncFunctionDef.args->kw_defaults);
1339 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1340 s->v.AsyncFunctionDef.returns))
1341 VISIT_QUIT(st, 0);
1342 if (s->v.AsyncFunctionDef.decorator_list)
1343 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1344 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1345 FunctionBlock, (void *)s, s->lineno,
1346 s->col_offset))
1347 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001348 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001349 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1350 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1351 if (!symtable_exit_block(st, s))
1352 VISIT_QUIT(st, 0);
1353 break;
1354 case AsyncWith_kind:
1355 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1356 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1357 break;
1358 case AsyncFor_kind:
1359 VISIT(st, expr, s->v.AsyncFor.target);
1360 VISIT(st, expr, s->v.AsyncFor.iter);
1361 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1362 if (s->v.AsyncFor.orelse)
1363 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1364 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001366 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367}
1368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370symtable_visit_expr(struct symtable *st, expr_ty e)
1371{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001372 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001373 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001374 "maximum recursion depth exceeded during compilation");
1375 VISIT_QUIT(st, 0);
1376 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 switch (e->kind) {
1378 case BoolOp_kind:
1379 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1380 break;
1381 case BinOp_kind:
1382 VISIT(st, expr, e->v.BinOp.left);
1383 VISIT(st, expr, e->v.BinOp.right);
1384 break;
1385 case UnaryOp_kind:
1386 VISIT(st, expr, e->v.UnaryOp.operand);
1387 break;
1388 case Lambda_kind: {
1389 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001390 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 if (e->v.Lambda.args->defaults)
1392 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001393 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001394 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001396 FunctionBlock, (void *)e, e->lineno,
1397 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001398 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001399 VISIT(st, arguments, e->v.Lambda.args);
1400 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001402 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 break;
1404 }
1405 case IfExp_kind:
1406 VISIT(st, expr, e->v.IfExp.test);
1407 VISIT(st, expr, e->v.IfExp.body);
1408 VISIT(st, expr, e->v.IfExp.orelse);
1409 break;
1410 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001411 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 VISIT_SEQ(st, expr, e->v.Dict.values);
1413 break;
1414 case Set_kind:
1415 VISIT_SEQ(st, expr, e->v.Set.elts);
1416 break;
1417 case GeneratorExp_kind:
1418 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001419 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 break;
1421 case ListComp_kind:
1422 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001423 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 break;
1425 case SetComp_kind:
1426 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001427 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 break;
1429 case DictComp_kind:
1430 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001431 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 break;
1433 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001434 if (e->v.Yield.value)
1435 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001438 case YieldFrom_kind:
1439 VISIT(st, expr, e->v.YieldFrom.value);
1440 st->st_cur->ste_generator = 1;
1441 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001442 case Await_kind:
1443 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001444 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001445 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 case Compare_kind:
1447 VISIT(st, expr, e->v.Compare.left);
1448 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1449 break;
1450 case Call_kind:
1451 VISIT(st, expr, e->v.Call.func);
1452 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001453 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001455 case FormattedValue_kind:
1456 VISIT(st, expr, e->v.FormattedValue.value);
1457 if (e->v.FormattedValue.format_spec)
1458 VISIT(st, expr, e->v.FormattedValue.format_spec);
1459 break;
1460 case JoinedStr_kind:
1461 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1462 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001463 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 /* Nothing to do here. */
1465 break;
1466 /* The following exprs can be assignment targets. */
1467 case Attribute_kind:
1468 VISIT(st, expr, e->v.Attribute.value);
1469 break;
1470 case Subscript_kind:
1471 VISIT(st, expr, e->v.Subscript.value);
1472 VISIT(st, slice, e->v.Subscript.slice);
1473 break;
1474 case Starred_kind:
1475 VISIT(st, expr, e->v.Starred.value);
1476 break;
1477 case Name_kind:
1478 if (!symtable_add_def(st, e->v.Name.id,
1479 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001480 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 /* Special-case super: it counts as a use of __class__ */
1482 if (e->v.Name.ctx == Load &&
1483 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001484 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001485 if (!GET_IDENTIFIER(__class__) ||
1486 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001487 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 }
1489 break;
1490 /* child nodes of List and Tuple will have expr_context set */
1491 case List_kind:
1492 VISIT_SEQ(st, expr, e->v.List.elts);
1493 break;
1494 case Tuple_kind:
1495 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1496 break;
1497 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001498 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499}
1500
1501static int
1502symtable_implicit_arg(struct symtable *st, int pos)
1503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1505 if (id == NULL)
1506 return 0;
1507 if (!symtable_add_def(st, id, DEF_PARAM)) {
1508 Py_DECREF(id);
1509 return 0;
1510 }
1511 Py_DECREF(id);
1512 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513}
1514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001516symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (!args)
1521 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 for (i = 0; i < asdl_seq_LEN(args); i++) {
1524 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1525 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1526 return 0;
1527 }
1528
1529 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530}
1531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001533symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (!args)
1538 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 for (i = 0; i < asdl_seq_LEN(args); i++) {
1541 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1542 if (arg->annotation)
1543 VISIT(st, expr, arg->annotation);
1544 }
1545
1546 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001547}
1548
Neal Norwitzc1505362006-12-28 06:47:50 +00001549static int
Yury Selivanov75445082015-05-11 22:57:16 -04001550symtable_visit_annotations(struct symtable *st, stmt_ty s,
1551 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 if (a->args && !symtable_visit_argannotations(st, a->args))
1554 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001555 if (a->vararg && a->vararg->annotation)
1556 VISIT(st, expr, a->vararg->annotation);
1557 if (a->kwarg && a->kwarg->annotation)
1558 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1560 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001561 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001562 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564}
1565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567symtable_visit_arguments(struct symtable *st, arguments_ty a)
1568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 /* skip default arguments inside function block
1570 XXX should ast be different?
1571 */
1572 if (a->args && !symtable_visit_params(st, a->args))
1573 return 0;
1574 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1575 return 0;
1576 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001577 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 return 0;
1579 st->st_cur->ste_varargs = 1;
1580 }
1581 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001582 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 return 0;
1584 st->st_cur->ste_varkeywords = 1;
1585 }
1586 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587}
1588
1589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 if (eh->v.ExceptHandler.type)
1594 VISIT(st, expr, eh->v.ExceptHandler.type);
1595 if (eh->v.ExceptHandler.name)
1596 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1597 return 0;
1598 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1599 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600}
1601
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001602static int
1603symtable_visit_withitem(struct symtable *st, withitem_ty item)
1604{
1605 VISIT(st, expr, item->context_expr);
1606 if (item->optional_vars) {
1607 VISIT(st, expr, item->optional_vars);
1608 }
1609 return 1;
1610}
1611
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614symtable_visit_alias(struct symtable *st, alias_ty a)
1615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001617 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 dotted package name (e.g. spam.eggs)
1619 */
1620 PyObject *store_name;
1621 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001622 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1623 PyUnicode_GET_LENGTH(name), 1);
1624 if (dot != -1) {
1625 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 if (!store_name)
1627 return 0;
1628 }
1629 else {
1630 store_name = name;
1631 Py_INCREF(store_name);
1632 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001633 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1635 Py_DECREF(store_name);
1636 return r;
1637 }
1638 else {
1639 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001640 int lineno = st->st_cur->ste_lineno;
1641 int col_offset = st->st_cur->ste_col_offset;
1642 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001643 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001644 Py_DECREF(store_name);
1645 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 Py_DECREF(store_name);
1648 return 1;
1649 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650}
1651
1652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 VISIT(st, expr, lc->target);
1657 VISIT(st, expr, lc->iter);
1658 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001659 if (lc->is_async) {
1660 st->st_cur->ste_coroutine = 1;
1661 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663}
1664
1665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667symtable_visit_keyword(struct symtable *st, keyword_ty k)
1668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 VISIT(st, expr, k->value);
1670 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671}
1672
1673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675symtable_visit_slice(struct symtable *st, slice_ty s)
1676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 switch (s->kind) {
1678 case Slice_kind:
1679 if (s->v.Slice.lower)
1680 VISIT(st, expr, s->v.Slice.lower)
1681 if (s->v.Slice.upper)
1682 VISIT(st, expr, s->v.Slice.upper)
1683 if (s->v.Slice.step)
1684 VISIT(st, expr, s->v.Slice.step)
1685 break;
1686 case ExtSlice_kind:
1687 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1688 break;
1689 case Index_kind:
1690 VISIT(st, expr, s->v.Index.value)
1691 break;
1692 }
1693 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694}
1695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001697symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001698 identifier scope_name, asdl_seq *generators,
1699 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 comprehension_ty outermost = ((comprehension_ty)
1703 asdl_seq_GET(generators, 0));
1704 /* Outermost iterator is evaluated in current scope */
1705 VISIT(st, expr, outermost->iter);
1706 /* Create comprehension scope for the rest */
1707 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001708 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1709 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 return 0;
1711 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001712 if (outermost->is_async) {
1713 st->st_cur->ste_coroutine = 1;
1714 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 /* Outermost iter is received as an argument */
1716 if (!symtable_implicit_arg(st, 0)) {
1717 symtable_exit_block(st, (void *)e);
1718 return 0;
1719 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001720 VISIT(st, expr, outermost->target);
1721 VISIT_SEQ(st, expr, outermost->ifs);
1722 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001724 VISIT(st, expr, value);
1725 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001726 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001727 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001728 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1729 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1730 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1731 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001732 PyErr_SyntaxLocationObject(st->st_filename,
1733 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001734 st->st_cur->ste_col_offset + 1);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001735 symtable_exit_block(st, (void *)e);
1736 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001737 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001738 st->st_cur->ste_generator = is_generator;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001743symtable_visit_genexp(struct symtable *st, expr_ty e)
1744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1746 e->v.GeneratorExp.generators,
1747 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001748}
1749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001751symtable_visit_listcomp(struct symtable *st, expr_ty e)
1752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1754 e->v.ListComp.generators,
1755 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001756}
1757
1758static int
1759symtable_visit_setcomp(struct symtable *st, expr_ty e)
1760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1762 e->v.SetComp.generators,
1763 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001764}
1765
1766static int
1767symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1770 e->v.DictComp.generators,
1771 e->v.DictComp.key,
1772 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001773}