blob: 21790b1cd1877cd93128b78d95cd94954c9444b8 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
3#include "code.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005#include "structmember.h"
6
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#define GLOBAL_AFTER_ASSIGN \
9"name '%.400s' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000010
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#define GLOBAL_AFTER_USE \
12"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000013
Neal Norwitz5d0ad502005-12-19 04:27:42 +000014#define IMPORT_STAR_WARNING "import * only allowed at module level"
15
Georg Brandlddbaa662006-06-04 21:56:52 +000016#define RETURN_VAL_IN_GENERATOR \
17 "'return' with argument inside generator"
Neal Norwitz5d0ad502005-12-19 04:27:42 +000018
Benjamin Petersone0d12eb2008-08-16 23:29:40 +000019
Neal Norwitz090b3dd2006-02-28 22:36:46 +000020static PySTEntryObject *
Benjamin Petersone0d12eb2008-08-16 23:29:40 +000021ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000022 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000023{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000024 PySTEntryObject *ste = NULL;
Christian Heimes8c1bce02012-09-10 03:08:46 +020025 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000026
Antoine Pitrouc83ea132010-05-09 14:46:46 +000027 k = PyLong_FromVoidPtr(key);
28 if (k == NULL)
29 goto fail;
30 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimesdfaf90d2012-09-12 17:58:10 +020031 if (ste == NULL) {
32 Py_DECREF(k);
Antoine Pitrouc83ea132010-05-09 14:46:46 +000033 goto fail;
Christian Heimesdfaf90d2012-09-12 17:58:10 +020034 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +000035 ste->ste_table = st;
Christian Heimes65a01412012-09-12 17:52:46 +020036 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000037
Antoine Pitrouc83ea132010-05-09 14:46:46 +000038 ste->ste_name = name;
39 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000040
Antoine Pitrouc83ea132010-05-09 14:46:46 +000041 ste->ste_symbols = NULL;
42 ste->ste_varnames = NULL;
43 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Antoine Pitrouc83ea132010-05-09 14:46:46 +000045 ste->ste_symbols = PyDict_New();
46 if (ste->ste_symbols == NULL)
47 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000048
Antoine Pitrouc83ea132010-05-09 14:46:46 +000049 ste->ste_varnames = PyList_New(0);
50 if (ste->ste_varnames == NULL)
51 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000052
Antoine Pitrouc83ea132010-05-09 14:46:46 +000053 ste->ste_children = PyList_New(0);
54 if (ste->ste_children == NULL)
55 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000056
Antoine Pitrouc83ea132010-05-09 14:46:46 +000057 ste->ste_type = block;
58 ste->ste_unoptimized = 0;
59 ste->ste_nested = 0;
60 ste->ste_free = 0;
61 ste->ste_varargs = 0;
62 ste->ste_varkeywords = 0;
63 ste->ste_opt_lineno = 0;
Benjamin Peterson045bbcd2010-10-20 21:28:09 +000064 ste->ste_tmpname = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +000065 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000066
Antoine Pitrouc83ea132010-05-09 14:46:46 +000067 if (st->st_cur != NULL &&
68 (st->st_cur->ste_nested ||
69 st->st_cur->ste_type == FunctionBlock))
70 ste->ste_nested = 1;
71 ste->ste_child_free = 0;
72 ste->ste_generator = 0;
73 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000074
Antoine Pitrouc83ea132010-05-09 14:46:46 +000075 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
76 goto fail;
77
78 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000079 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +000080 Py_XDECREF(ste);
81 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082}
83
84static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000086{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000087 char buf[256];
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088
Antoine Pitrouc83ea132010-05-09 14:46:46 +000089 PyOS_snprintf(buf, sizeof(buf),
90 "<symtable entry %.100s(%ld), line %d>",
91 PyString_AS_STRING(ste->ste_name),
92 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
93 return PyString_FromString(buf);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000094}
95
96static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000098{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000099 ste->ste_table = NULL;
100 Py_XDECREF(ste->ste_id);
101 Py_XDECREF(ste->ste_name);
102 Py_XDECREF(ste->ste_symbols);
103 Py_XDECREF(ste->ste_varnames);
104 Py_XDECREF(ste->ste_children);
105 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000106}
107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109
Guido van Rossum6f799372001-09-20 20:46:19 +0000110static PyMemberDef ste_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000111 {"id", T_OBJECT, OFF(ste_id), READONLY},
112 {"name", T_OBJECT, OFF(ste_name), READONLY},
113 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
114 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
115 {"children", T_OBJECT, OFF(ste_children), READONLY},
116 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
117 {"nested", T_INT, OFF(ste_nested), READONLY},
118 {"type", T_INT, OFF(ste_type), READONLY},
119 {"lineno", T_INT, OFF(ste_lineno), READONLY},
120 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121};
122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123PyTypeObject PySTEntry_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000124 PyVarObject_HEAD_INIT(&PyType_Type, 0)
125 "symtable entry",
126 sizeof(PySTEntryObject),
127 0,
128 (destructor)ste_dealloc, /* tp_dealloc */
129 0, /* tp_print */
130 0, /* tp_getattr */
131 0, /* tp_setattr */
132 0, /* tp_compare */
133 (reprfunc)ste_repr, /* tp_repr */
134 0, /* tp_as_number */
135 0, /* tp_as_sequence */
136 0, /* tp_as_mapping */
137 0, /* tp_hash */
138 0, /* tp_call */
139 0, /* tp_str */
140 PyObject_GenericGetAttr, /* tp_getattro */
141 0, /* tp_setattro */
142 0, /* tp_as_buffer */
143 Py_TPFLAGS_DEFAULT, /* tp_flags */
144 0, /* tp_doc */
145 0, /* tp_traverse */
146 0, /* tp_clear */
147 0, /* tp_richcompare */
148 0, /* tp_weaklistoffset */
149 0, /* tp_iter */
150 0, /* tp_iternext */
151 0, /* tp_methods */
152 ste_memberlist, /* tp_members */
153 0, /* tp_getset */
154 0, /* tp_base */
155 0, /* tp_dict */
156 0, /* tp_descr_get */
157 0, /* tp_descr_set */
158 0, /* tp_dictoffset */
159 0, /* tp_init */
160 0, /* tp_alloc */
161 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000162};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163
164static int symtable_analyze(struct symtable *st);
Serhiy Storchaka65d18872017-12-02 21:00:09 +0200165static int symtable_warn(struct symtable *st,
166 PyObject *warn, const char *msg, int lineno);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167static int symtable_enter_block(struct symtable *st, identifier name,
168 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169static int symtable_exit_block(struct symtable *st, void *ast);
170static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
171static int symtable_visit_expr(struct symtable *st, expr_ty s);
Serhiy Storchaka65d18872017-12-02 21:00:09 +0200172static int symtable_visit_listcomp(struct symtable *st, expr_ty e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000174static int symtable_visit_setcomp(struct symtable *st, expr_ty e);
175static int symtable_visit_dictcomp(struct symtable *st, expr_ty e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000176static int symtable_visit_arguments(struct symtable *st, arguments_ty);
177static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
178static int symtable_visit_alias(struct symtable *st, alias_ty);
179static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
180static int symtable_visit_keyword(struct symtable *st, keyword_ty);
181static int symtable_visit_slice(struct symtable *st, slice_ty);
182static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
183static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
184static int symtable_implicit_arg(struct symtable *st, int pos);
185
186
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000187static identifier top = NULL, lambda = NULL, genexpr = NULL, setcomp = NULL,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000188 dictcomp = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189
190#define GET_IDENTIFIER(VAR) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000191 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
193#define DUPLICATE_ARGUMENT \
194"duplicate argument '%s' in function definition"
195
196static struct symtable *
197symtable_new(void)
198{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
202 if (st == NULL)
203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000205 st->st_filename = NULL;
206 st->st_symbols = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000207
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000208 if ((st->st_stack = PyList_New(0)) == NULL)
209 goto fail;
210 if ((st->st_symbols = PyDict_New()) == NULL)
211 goto fail;
212 st->st_cur = NULL;
213 st->st_private = NULL;
214 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000216 PySymtable_Free(st);
217 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218}
219
220struct symtable *
221PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
222{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 struct symtable *st = symtable_new();
224 asdl_seq *seq;
225 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000227 if (st == NULL)
228 return st;
229 st->st_filename = filename;
230 st->st_future = future;
231 if (!GET_IDENTIFIER(top) ||
232 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
233 PySymtable_Free(st);
234 return NULL;
235 }
Neal Norwitzd12bd012006-07-21 07:59:47 +0000236
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000237 st->st_top = st->st_cur;
238 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
239 /* Any other top-level initialization? */
240 switch (mod->kind) {
241 case Module_kind:
242 seq = mod->v.Module.body;
243 for (i = 0; i < asdl_seq_LEN(seq); i++)
244 if (!symtable_visit_stmt(st,
245 (stmt_ty)asdl_seq_GET(seq, i)))
246 goto error;
247 break;
248 case Expression_kind:
249 if (!symtable_visit_expr(st, mod->v.Expression.body))
250 goto error;
251 break;
252 case Interactive_kind:
253 seq = mod->v.Interactive.body;
254 for (i = 0; i < asdl_seq_LEN(seq); i++)
255 if (!symtable_visit_stmt(st,
256 (stmt_ty)asdl_seq_GET(seq, i)))
257 goto error;
258 break;
259 case Suite_kind:
260 PyErr_SetString(PyExc_RuntimeError,
261 "this compiler does not handle Suites");
262 goto error;
263 }
264 if (!symtable_exit_block(st, (void *)mod)) {
265 PySymtable_Free(st);
266 return NULL;
267 }
268 if (symtable_analyze(st))
269 return st;
270 PySymtable_Free(st);
271 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000273 (void) symtable_exit_block(st, (void *)mod);
274 PySymtable_Free(st);
275 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000276}
277
278void
279PySymtable_Free(struct symtable *st)
280{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000281 Py_XDECREF(st->st_symbols);
282 Py_XDECREF(st->st_stack);
283 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284}
285
286PySTEntryObject *
287PySymtable_Lookup(struct symtable *st, void *key)
288{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000289 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000291 k = PyLong_FromVoidPtr(key);
292 if (k == NULL)
293 return NULL;
294 v = PyDict_GetItem(st->st_symbols, k);
295 if (v) {
296 assert(PySTEntry_Check(v));
297 Py_INCREF(v);
298 }
299 else {
300 PyErr_SetString(PyExc_KeyError,
301 "unknown symbol table entry");
302 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 Py_DECREF(k);
305 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306}
307
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000308int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309PyST_GetScope(PySTEntryObject *ste, PyObject *name)
310{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000311 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
312 if (!v)
313 return 0;
314 assert(PyInt_Check(v));
315 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000316}
317
318
319/* Analyze raw symbol information to determine scope of each name.
320
321 The next several functions are helpers for PySymtable_Analyze(),
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000322 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323 it determines which local variables are cell variables; they provide
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000324 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000326 There are also two kinds of free variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327 explicit global is declared with the global statement. An implicit
328 global is a free variable for which the compiler has found no binding
329 in an enclosing function scope. The implicit global is either a global
330 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
331 to handle these names to implement slightly odd semantics. In such a
332 block, the name is treated as global until it is assigned to; then it
333 is treated as a local.
334
335 The symbol table requires two passes to determine the scope of each name.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000336 The first pass collects raw facts from the AST: the name is a parameter
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337 here, the name is used by not defined here, etc. The second pass analyzes
338 these facts during a pass over the PySTEntryObjects created during pass 1.
339
340 When a function is entered during the second pass, the parent passes
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 the set of all name bindings visible to its children. These bindings
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342 are used to determine if the variable is free or an implicit global.
343 After doing the local analysis, it analyzes each of its child blocks
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000344 using an updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 The children update the free variable set. If a local variable is free
347 in a child, the variable is marked as a cell. The current function must
348 provide runtime storage for the variable that may outlive the function's
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349 frame. Cell variables are removed from the free set before the analyze
350 function returns to its parent.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000351
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 The sets of bound and free variables are implemented as dictionaries
353 mapping strings to None.
354*/
355
356#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000357 PyObject *o = PyInt_FromLong(I); \
358 if (!o) \
359 return 0; \
360 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
361 Py_DECREF(o); \
362 return 0; \
363 } \
364 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365}
366
367/* Decide on scope of name, given flags.
368
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000369 The namespace dictionaries may be modified to record information
370 about the new name. For example, a new global will add an entry to
371 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372*/
373
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000374static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000375analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000376 PyObject *bound, PyObject *local, PyObject *free,
377 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000379 if (flags & DEF_GLOBAL) {
380 if (flags & DEF_PARAM) {
381 PyErr_Format(PyExc_SyntaxError,
382 "name '%s' is local and global",
383 PyString_AS_STRING(name));
384 PyErr_SyntaxLocation(ste->ste_table->st_filename,
385 ste->ste_lineno);
386
387 return 0;
388 }
389 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
390 if (PyDict_SetItem(global, name, Py_None) < 0)
391 return 0;
392 if (bound && PyDict_GetItem(bound, name)) {
393 if (PyDict_DelItem(bound, name) < 0)
394 return 0;
395 }
396 return 1;
397 }
398 if (flags & DEF_BOUND) {
399 SET_SCOPE(dict, name, LOCAL);
400 if (PyDict_SetItem(local, name, Py_None) < 0)
401 return 0;
402 if (PyDict_GetItem(global, name)) {
403 if (PyDict_DelItem(global, name) < 0)
404 return 0;
405 }
406 return 1;
407 }
408 /* If an enclosing block has a binding for this name, it
409 is a free variable rather than a global variable.
410 Note that having a non-NULL bound implies that the block
411 is nested.
412 */
413 if (bound && PyDict_GetItem(bound, name)) {
414 SET_SCOPE(dict, name, FREE);
415 ste->ste_free = 1;
416 if (PyDict_SetItem(free, name, Py_None) < 0)
417 return 0;
418 return 1;
419 }
420 /* If a parent has a global statement, then call it global
421 explicit? It could also be global implicit.
422 */
423 else if (global && PyDict_GetItem(global, name)) {
424 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
425 return 1;
426 }
427 else {
428 if (ste->ste_nested)
429 ste->ste_free = 1;
430 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
431 return 1;
432 }
433 /* Should never get here. */
434 PyErr_Format(PyExc_SystemError, "failed to set scope for %s",
435 PyString_AS_STRING(name));
436 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437}
438
439#undef SET_SCOPE
440
441/* If a name is defined in free and also in locals, then this block
442 provides the binding for the free variable. The name should be
443 marked CELL in this block and removed from the free list.
444
445 Note that the current block's free variables are included in free.
446 That's safe because no name can be free and local in the same scope.
447*/
448
449static int
450analyze_cells(PyObject *scope, PyObject *free)
451{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000452 PyObject *name, *v, *w;
453 int success = 0;
454 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000456 w = PyInt_FromLong(CELL);
457 if (!w)
458 return 0;
459 while (PyDict_Next(scope, &pos, &name, &v)) {
460 long flags;
461 assert(PyInt_Check(v));
462 flags = PyInt_AS_LONG(v);
463 if (flags != LOCAL)
464 continue;
465 if (!PyDict_GetItem(free, name))
466 continue;
467 /* Replace LOCAL with CELL for this name, and remove
468 from free. It is safe to replace the value of name
469 in the dict, because it will not cause a resize.
470 */
471 if (PyDict_SetItem(scope, name, w) < 0)
472 goto error;
Benjamin Peterson8363f772014-01-16 16:56:22 -0500473 if (PyDict_DelItem(free, name) < 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000474 goto error;
475 }
476 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000478 Py_DECREF(w);
479 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480}
481
482/* Check for illegal statements in unoptimized namespaces */
483static int
484check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000485 char buf[300];
486 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000488 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
489 || !(ste->ste_free || ste->ste_child_free))
490 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000491
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000492 trailer = (ste->ste_child_free ?
493 "contains a nested function with free variables" :
494 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000496 switch (ste->ste_unoptimized) {
497 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
498 case OPT_EXEC: /* qualified exec is fine */
499 return 1;
500 case OPT_IMPORT_STAR:
501 PyOS_snprintf(buf, sizeof(buf),
502 "import * is not allowed in function '%.100s' "
503 "because it %s",
504 PyString_AS_STRING(ste->ste_name), trailer);
505 break;
506 case OPT_BARE_EXEC:
507 PyOS_snprintf(buf, sizeof(buf),
508 "unqualified exec is not allowed in function "
Benjamin Petersonee5729d2014-07-18 16:25:13 -0700509 "'%.100s' because it %s",
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000510 PyString_AS_STRING(ste->ste_name), trailer);
511 break;
512 default:
513 PyOS_snprintf(buf, sizeof(buf),
514 "function '%.100s' uses import * and bare exec, "
515 "which are illegal because it %s",
516 PyString_AS_STRING(ste->ste_name), trailer);
517 break;
518 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000520 PyErr_SetString(PyExc_SyntaxError, buf);
521 PyErr_SyntaxLocation(ste->ste_table->st_filename,
522 ste->ste_opt_lineno);
523 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524}
525
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000526/* Enter the final scope information into the st_symbols dict.
527 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528 * All arguments are dicts. Modifies symbols, others are read-only.
529*/
530static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000531update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000532 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000534 PyObject *name, *v, *u, *w, *free_value = NULL;
535 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000537 while (PyDict_Next(symbols, &pos, &name, &v)) {
538 long i, flags;
539 assert(PyInt_Check(v));
540 flags = PyInt_AS_LONG(v);
541 w = PyDict_GetItem(scope, name);
542 assert(w && PyInt_Check(w));
543 i = PyInt_AS_LONG(w);
544 flags |= (i << SCOPE_OFF);
545 u = PyInt_FromLong(flags);
546 if (!u)
547 return 0;
548 if (PyDict_SetItem(symbols, name, u) < 0) {
549 Py_DECREF(u);
550 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000552 Py_DECREF(u);
553 }
554
555 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
556 if (!free_value)
557 return 0;
558
559 /* add a free variable when it's only use is for creating a closure */
560 pos = 0;
561 while (PyDict_Next(free, &pos, &name, &v)) {
562 PyObject *o = PyDict_GetItem(symbols, name);
563
564 if (o) {
565 /* It could be a free variable in a method of
566 the class that has the same name as a local
567 or global in the class scope.
568 */
569 if (classflag &&
570 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
571 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
572 o = PyInt_FromLong(i);
573 if (!o) {
574 Py_DECREF(free_value);
575 return 0;
576 }
577 if (PyDict_SetItem(symbols, name, o) < 0) {
578 Py_DECREF(o);
579 Py_DECREF(free_value);
580 return 0;
581 }
582 Py_DECREF(o);
583 }
584 /* else it's not free, probably a cell */
585 continue;
586 }
587 if (!PyDict_GetItem(bound, name))
588 continue; /* it's a global */
589
590 if (PyDict_SetItem(symbols, name, free_value) < 0) {
591 Py_DECREF(free_value);
592 return 0;
593 }
594 }
595 Py_DECREF(free_value);
596 return 1;
597}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598
599/* Make final symbol table decisions for block of ste.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000600
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 Arguments:
602 ste -- current symtable entry (input/output)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000603 bound -- set of variables bound in enclosing scopes (input). bound
604 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 free -- set of free variables in enclosed scopes (output)
606 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000607
608 The implementation uses two mutually recursive functions,
609 analyze_block() and analyze_child_block(). analyze_block() is
610 responsible for analyzing the individual names defined in a block.
611 analyze_child_block() prepares temporary namespace dictionaries
612 used to evaluated nested blocks.
613
614 The two functions exist because a child block should see the name
615 bindings of its enclosing blocks, but those bindings should not
616 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617*/
618
619static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000620analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
621 PyObject *global, PyObject* child_free);
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000622
623static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000624analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
625 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000627 PyObject *name, *v, *local = NULL, *scope = NULL;
628 PyObject *newbound = NULL, *newglobal = NULL;
629 PyObject *newfree = NULL, *allfree = NULL;
630 int i, success = 0;
631 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000633 local = PyDict_New(); /* collect new names bound in block */
634 if (!local)
635 goto error;
636 scope = PyDict_New(); /* collect scopes defined for each name */
637 if (!scope)
638 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000639
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000640 /* Allocate new global and bound variable dictionaries. These
641 dictionaries hold the names visible in nested blocks. For
642 ClassBlocks, the bound and global names are initialized
643 before analyzing names, because class bindings aren't
644 visible in methods. For other blocks, they are initialized
645 after names are analyzed.
646 */
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000647
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000648 /* TODO(jhylton): Package these dicts in a struct so that we
649 can write reasonable helper functions?
650 */
651 newglobal = PyDict_New();
652 if (!newglobal)
653 goto error;
654 newbound = PyDict_New();
655 if (!newbound)
656 goto error;
657 newfree = PyDict_New();
658 if (!newfree)
659 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000661 if (ste->ste_type == ClassBlock) {
662 if (PyDict_Update(newglobal, global) < 0)
663 goto error;
664 if (bound)
665 if (PyDict_Update(newbound, bound) < 0)
666 goto error;
667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000669 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
670 long flags = PyInt_AS_LONG(v);
671 if (!analyze_name(ste, scope, name, flags,
672 bound, local, free, global))
673 goto error;
674 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000676 if (ste->ste_type != ClassBlock) {
677 if (ste->ste_type == FunctionBlock) {
678 if (PyDict_Update(newbound, local) < 0)
679 goto error;
680 }
681 if (bound) {
682 if (PyDict_Update(newbound, bound) < 0)
683 goto error;
684 }
685 if (PyDict_Update(newglobal, global) < 0)
686 goto error;
687 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000689 /* Recursively call analyze_block() on each child block.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000690
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000691 newbound, newglobal now contain the names visible in
692 nested blocks. The free variables in the children will
693 be collected in allfree.
694 */
695 allfree = PyDict_New();
696 if (!allfree)
697 goto error;
698 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
699 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
700 PySTEntryObject* entry;
701 assert(c && PySTEntry_Check(c));
702 entry = (PySTEntryObject*)c;
703 if (!analyze_child_block(entry, newbound, newfree, newglobal,
704 allfree))
705 goto error;
706 if (entry->ste_free || entry->ste_child_free)
707 ste->ste_child_free = 1;
708 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000710 if (PyDict_Update(newfree, allfree) < 0)
711 goto error;
712 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
713 goto error;
714 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
715 ste->ste_type == ClassBlock))
716 goto error;
717 if (!check_unoptimized(ste))
718 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000720 if (PyDict_Update(free, newfree) < 0)
721 goto error;
722 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000724 Py_XDECREF(local);
725 Py_XDECREF(scope);
726 Py_XDECREF(newbound);
727 Py_XDECREF(newglobal);
728 Py_XDECREF(newfree);
729 Py_XDECREF(allfree);
730 if (!success)
731 assert(PyErr_Occurred());
732 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733}
734
735static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000736analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
737 PyObject *global, PyObject* child_free)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000738{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000739 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000740
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000741 /* Copy the bound and global dictionaries.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000742
Martin Panterb5f487a2016-06-04 04:57:19 +0000743 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000744 current block. The analyze_block() call modifies these
745 dictionaries.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000746
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000747 */
748 temp_bound = PyDict_New();
749 if (!temp_bound)
750 goto error;
751 if (PyDict_Update(temp_bound, bound) < 0)
752 goto error;
753 temp_free = PyDict_New();
754 if (!temp_free)
755 goto error;
756 if (PyDict_Update(temp_free, free) < 0)
757 goto error;
758 temp_global = PyDict_New();
759 if (!temp_global)
760 goto error;
761 if (PyDict_Update(temp_global, global) < 0)
762 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000763
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000764 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
765 goto error;
766 if (PyDict_Update(child_free, temp_free) < 0)
767 goto error;
768 Py_DECREF(temp_bound);
769 Py_DECREF(temp_free);
770 Py_DECREF(temp_global);
771 return 1;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000772 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000773 Py_XDECREF(temp_bound);
774 Py_XDECREF(temp_free);
775 Py_XDECREF(temp_global);
776 return 0;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000777}
778
779static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780symtable_analyze(struct symtable *st)
781{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000782 PyObject *free, *global;
783 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000785 free = PyDict_New();
786 if (!free)
787 return 0;
788 global = PyDict_New();
789 if (!global) {
790 Py_DECREF(free);
791 return 0;
792 }
793 r = analyze_block(st->st_top, NULL, free, global);
794 Py_DECREF(free);
795 Py_DECREF(global);
796 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797}
798
799
800static int
Serhiy Storchaka65d18872017-12-02 21:00:09 +0200801symtable_warn(struct symtable *st, PyObject *warn, const char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802{
Serhiy Storchaka65d18872017-12-02 21:00:09 +0200803 if (lineno < 0) {
804 lineno = st->st_cur->ste_lineno;
805 }
806 if (PyErr_WarnExplicit(warn, msg, st->st_filename, lineno, NULL, NULL) < 0) {
807 if (PyErr_ExceptionMatches(warn)) {
808 /* Replace the warning exception with a SyntaxError
809 to get a more accurate error report */
810 PyErr_Clear();
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000811 PyErr_SetString(PyExc_SyntaxError, msg);
Serhiy Storchaka65d18872017-12-02 21:00:09 +0200812 PyErr_SyntaxLocation(st->st_filename, lineno);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000813 }
814 return 0;
815 }
816 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817}
818
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000819/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 This reference is released when the block is exited, via the DECREF
821 in symtable_exit_block().
822*/
823
824static int
825symtable_exit_block(struct symtable *st, void *ast)
826{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000827 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000829 Py_CLEAR(st->st_cur);
830 end = PyList_GET_SIZE(st->st_stack) - 1;
831 if (end >= 0) {
832 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
833 end);
834 if (st->st_cur == NULL)
835 return 0;
836 Py_INCREF(st->st_cur);
837 if (PySequence_DelItem(st->st_stack, end) < 0)
838 return 0;
839 }
840 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841}
842
843static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000844symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
845 void *ast, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000847 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000849 if (st->st_cur) {
850 prev = st->st_cur;
851 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
852 return 0;
853 }
854 Py_DECREF(st->st_cur);
855 }
856 st->st_cur = ste_new(st, name, block, ast, lineno);
857 if (st->st_cur == NULL)
858 return 0;
Benjamin Petersonf76942d2010-10-16 03:51:38 +0000859 if (block == ModuleBlock)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000860 st->st_global = st->st_cur->ste_symbols;
861 if (prev) {
862 if (PyList_Append(prev->ste_children,
863 (PyObject *)st->st_cur) < 0) {
864 return 0;
865 }
866 }
867 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868}
869
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000870static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871symtable_lookup(struct symtable *st, PyObject *name)
872{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000873 PyObject *o;
874 PyObject *mangled = _Py_Mangle(st->st_private, name);
875 if (!mangled)
876 return 0;
877 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
878 Py_DECREF(mangled);
879 if (!o)
880 return 0;
881 return PyInt_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882}
883
884static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000885symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000887 PyObject *o;
888 PyObject *dict;
889 long val;
890 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000892 if (!mangled)
893 return 0;
894 dict = st->st_cur->ste_symbols;
895 if ((o = PyDict_GetItem(dict, mangled))) {
896 val = PyInt_AS_LONG(o);
897 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
898 /* Is it better to use 'mangled' or 'name' here? */
899 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
900 PyString_AsString(name));
901 PyErr_SyntaxLocation(st->st_filename,
902 st->st_cur->ste_lineno);
903 goto error;
904 }
905 val |= flag;
906 } else
907 val = flag;
908 o = PyInt_FromLong(val);
909 if (o == NULL)
910 goto error;
911 if (PyDict_SetItem(dict, mangled, o) < 0) {
912 Py_DECREF(o);
913 goto error;
914 }
915 Py_DECREF(o);
916
917 if (flag & DEF_PARAM) {
918 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
919 goto error;
920 } else if (flag & DEF_GLOBAL) {
921 /* XXX need to update DEF_GLOBAL for other flags too;
922 perhaps only DEF_FREE_GLOBAL */
923 val = flag;
924 if ((o = PyDict_GetItem(st->st_global, mangled))) {
925 val |= PyInt_AS_LONG(o);
926 }
927 o = PyInt_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928 if (o == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000929 goto error;
930 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
931 Py_DECREF(o);
932 goto error;
933 }
934 Py_DECREF(o);
935 }
936 Py_DECREF(mangled);
937 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000938
939error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000940 Py_DECREF(mangled);
941 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942}
943
944/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
945 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000946 function.
947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
949 useful if the first node in the sequence requires special treatment.
950*/
951
952#define VISIT(ST, TYPE, V) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000953 if (!symtable_visit_ ## TYPE((ST), (V))) \
954 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000955
956#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000957 if (!symtable_visit_ ## TYPE((ST), (V))) { \
958 symtable_exit_block((ST), (S)); \
959 return 0; \
960 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000961
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000963 int i; \
964 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
965 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
966 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
967 if (!symtable_visit_ ## TYPE((ST), elt)) \
968 return 0; \
969 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000971
972#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000973 int i; \
974 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
975 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
976 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
977 if (!symtable_visit_ ## TYPE((ST), elt)) { \
978 symtable_exit_block((ST), (S)); \
979 return 0; \
980 } \
981 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000982}
983
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000985 int i; \
986 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
987 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
988 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
989 if (!symtable_visit_ ## TYPE((ST), elt)) \
990 return 0; \
991 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000993
994#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000995 int i; \
996 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
997 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
998 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
999 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1000 symtable_exit_block((ST), (S)); \
1001 return 0; \
1002 } \
1003 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001004}
1005
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006static int
1007symtable_visit_stmt(struct symtable *st, stmt_ty s)
1008{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001009 switch (s->kind) {
1010 case FunctionDef_kind:
1011 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1012 return 0;
1013 if (s->v.FunctionDef.args->defaults)
1014 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1015 if (s->v.FunctionDef.decorator_list)
1016 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1017 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1018 FunctionBlock, (void *)s, s->lineno))
1019 return 0;
1020 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1021 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1022 if (!symtable_exit_block(st, s))
1023 return 0;
1024 break;
1025 case ClassDef_kind: {
1026 PyObject *tmp;
1027 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1028 return 0;
1029 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1030 if (s->v.ClassDef.decorator_list)
1031 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1032 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1033 (void *)s, s->lineno))
1034 return 0;
1035 tmp = st->st_private;
1036 st->st_private = s->v.ClassDef.name;
1037 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1038 st->st_private = tmp;
1039 if (!symtable_exit_block(st, s))
1040 return 0;
1041 break;
1042 }
1043 case Return_kind:
1044 if (s->v.Return.value) {
1045 VISIT(st, expr, s->v.Return.value);
1046 st->st_cur->ste_returns_value = 1;
1047 if (st->st_cur->ste_generator) {
1048 PyErr_SetString(PyExc_SyntaxError,
1049 RETURN_VAL_IN_GENERATOR);
1050 PyErr_SyntaxLocation(st->st_filename,
1051 s->lineno);
1052 return 0;
1053 }
1054 }
1055 break;
1056 case Delete_kind:
1057 VISIT_SEQ(st, expr, s->v.Delete.targets);
1058 break;
1059 case Assign_kind:
1060 VISIT_SEQ(st, expr, s->v.Assign.targets);
1061 VISIT(st, expr, s->v.Assign.value);
1062 break;
1063 case AugAssign_kind:
1064 VISIT(st, expr, s->v.AugAssign.target);
1065 VISIT(st, expr, s->v.AugAssign.value);
1066 break;
1067 case Print_kind:
1068 if (s->v.Print.dest)
1069 VISIT(st, expr, s->v.Print.dest);
1070 VISIT_SEQ(st, expr, s->v.Print.values);
1071 break;
1072 case For_kind:
1073 VISIT(st, expr, s->v.For.target);
1074 VISIT(st, expr, s->v.For.iter);
1075 VISIT_SEQ(st, stmt, s->v.For.body);
1076 if (s->v.For.orelse)
1077 VISIT_SEQ(st, stmt, s->v.For.orelse);
1078 break;
1079 case While_kind:
1080 VISIT(st, expr, s->v.While.test);
1081 VISIT_SEQ(st, stmt, s->v.While.body);
1082 if (s->v.While.orelse)
1083 VISIT_SEQ(st, stmt, s->v.While.orelse);
1084 break;
1085 case If_kind:
1086 /* XXX if 0: and lookup_yield() hacks */
1087 VISIT(st, expr, s->v.If.test);
1088 VISIT_SEQ(st, stmt, s->v.If.body);
1089 if (s->v.If.orelse)
1090 VISIT_SEQ(st, stmt, s->v.If.orelse);
1091 break;
1092 case Raise_kind:
1093 if (s->v.Raise.type) {
1094 VISIT(st, expr, s->v.Raise.type);
1095 if (s->v.Raise.inst) {
1096 VISIT(st, expr, s->v.Raise.inst);
1097 if (s->v.Raise.tback)
1098 VISIT(st, expr, s->v.Raise.tback);
1099 }
1100 }
1101 break;
1102 case TryExcept_kind:
1103 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1104 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1105 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1106 break;
1107 case TryFinally_kind:
1108 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1109 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1110 break;
1111 case Assert_kind:
1112 VISIT(st, expr, s->v.Assert.test);
1113 if (s->v.Assert.msg)
1114 VISIT(st, expr, s->v.Assert.msg);
1115 break;
1116 case Import_kind:
1117 VISIT_SEQ(st, alias, s->v.Import.names);
1118 /* XXX Don't have the lineno available inside
1119 visit_alias */
1120 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1121 st->st_cur->ste_opt_lineno = s->lineno;
1122 break;
1123 case ImportFrom_kind:
1124 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1125 /* XXX Don't have the lineno available inside
1126 visit_alias */
1127 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1128 st->st_cur->ste_opt_lineno = s->lineno;
1129 break;
1130 case Exec_kind:
1131 VISIT(st, expr, s->v.Exec.body);
1132 if (!st->st_cur->ste_opt_lineno)
1133 st->st_cur->ste_opt_lineno = s->lineno;
1134 if (s->v.Exec.globals) {
1135 st->st_cur->ste_unoptimized |= OPT_EXEC;
1136 VISIT(st, expr, s->v.Exec.globals);
1137 if (s->v.Exec.locals)
1138 VISIT(st, expr, s->v.Exec.locals);
1139 } else {
1140 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1141 }
1142 break;
1143 case Global_kind: {
1144 int i;
1145 asdl_seq *seq = s->v.Global.names;
1146 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1147 identifier name = (identifier)asdl_seq_GET(seq, i);
1148 char *c_name = PyString_AS_STRING(name);
1149 long cur = symtable_lookup(st, name);
1150 if (cur < 0)
1151 return 0;
1152 if (cur & (DEF_LOCAL | USE)) {
1153 char buf[256];
1154 if (cur & DEF_LOCAL)
1155 PyOS_snprintf(buf, sizeof(buf),
1156 GLOBAL_AFTER_ASSIGN,
1157 c_name);
1158 else
1159 PyOS_snprintf(buf, sizeof(buf),
1160 GLOBAL_AFTER_USE,
1161 c_name);
Serhiy Storchaka65d18872017-12-02 21:00:09 +02001162 if (!symtable_warn(st, PyExc_SyntaxWarning, buf, s->lineno))
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001163 return 0;
1164 }
1165 if (!symtable_add_def(st, name, DEF_GLOBAL))
1166 return 0;
1167 }
1168 break;
1169 }
1170 case Expr_kind:
1171 VISIT(st, expr, s->v.Expr.value);
1172 break;
1173 case Pass_kind:
1174 case Break_kind:
1175 case Continue_kind:
1176 /* nothing to do here */
1177 break;
1178 case With_kind:
1179 VISIT(st, expr, s->v.With.context_expr);
1180 if (s->v.With.optional_vars) {
1181 VISIT(st, expr, s->v.With.optional_vars);
1182 }
1183 VISIT_SEQ(st, stmt, s->v.With.body);
1184 break;
1185 }
1186 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187}
1188
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001189static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190symtable_visit_expr(struct symtable *st, expr_ty e)
1191{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001192 switch (e->kind) {
1193 case BoolOp_kind:
1194 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1195 break;
1196 case BinOp_kind:
1197 VISIT(st, expr, e->v.BinOp.left);
1198 VISIT(st, expr, e->v.BinOp.right);
1199 break;
1200 case UnaryOp_kind:
1201 VISIT(st, expr, e->v.UnaryOp.operand);
1202 break;
1203 case Lambda_kind: {
1204 if (!GET_IDENTIFIER(lambda))
1205 return 0;
1206 if (e->v.Lambda.args->defaults)
1207 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1208 if (!symtable_enter_block(st, lambda,
1209 FunctionBlock, (void *)e, e->lineno))
1210 return 0;
1211 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1212 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1213 if (!symtable_exit_block(st, (void *)e))
1214 return 0;
1215 break;
1216 }
1217 case IfExp_kind:
1218 VISIT(st, expr, e->v.IfExp.test);
1219 VISIT(st, expr, e->v.IfExp.body);
1220 VISIT(st, expr, e->v.IfExp.orelse);
1221 break;
1222 case Dict_kind:
1223 VISIT_SEQ(st, expr, e->v.Dict.keys);
1224 VISIT_SEQ(st, expr, e->v.Dict.values);
1225 break;
1226 case Set_kind:
1227 VISIT_SEQ(st, expr, e->v.Set.elts);
1228 break;
1229 case ListComp_kind:
Serhiy Storchaka65d18872017-12-02 21:00:09 +02001230 if (!symtable_visit_listcomp(st, e))
1231 return 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001232 break;
1233 case GeneratorExp_kind:
1234 if (!symtable_visit_genexp(st, e))
1235 return 0;
1236 break;
1237 case SetComp_kind:
1238 if (!symtable_visit_setcomp(st, e))
1239 return 0;
1240 break;
1241 case DictComp_kind:
1242 if (!symtable_visit_dictcomp(st, e))
1243 return 0;
1244 break;
1245 case Yield_kind:
1246 if (e->v.Yield.value)
1247 VISIT(st, expr, e->v.Yield.value);
1248 st->st_cur->ste_generator = 1;
1249 if (st->st_cur->ste_returns_value) {
1250 PyErr_SetString(PyExc_SyntaxError,
1251 RETURN_VAL_IN_GENERATOR);
1252 PyErr_SyntaxLocation(st->st_filename,
1253 e->lineno);
1254 return 0;
1255 }
1256 break;
1257 case Compare_kind:
1258 VISIT(st, expr, e->v.Compare.left);
1259 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1260 break;
1261 case Call_kind:
1262 VISIT(st, expr, e->v.Call.func);
1263 VISIT_SEQ(st, expr, e->v.Call.args);
1264 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1265 if (e->v.Call.starargs)
1266 VISIT(st, expr, e->v.Call.starargs);
1267 if (e->v.Call.kwargs)
1268 VISIT(st, expr, e->v.Call.kwargs);
1269 break;
1270 case Repr_kind:
1271 VISIT(st, expr, e->v.Repr.value);
1272 break;
1273 case Num_kind:
1274 case Str_kind:
1275 /* Nothing to do here. */
1276 break;
1277 /* The following exprs can be assignment targets. */
1278 case Attribute_kind:
1279 VISIT(st, expr, e->v.Attribute.value);
1280 break;
1281 case Subscript_kind:
1282 VISIT(st, expr, e->v.Subscript.value);
1283 VISIT(st, slice, e->v.Subscript.slice);
1284 break;
1285 case Name_kind:
1286 if (!symtable_add_def(st, e->v.Name.id,
1287 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1288 return 0;
1289 break;
1290 /* child nodes of List and Tuple will have expr_context set */
1291 case List_kind:
1292 VISIT_SEQ(st, expr, e->v.List.elts);
1293 break;
1294 case Tuple_kind:
1295 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1296 break;
1297 }
1298 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299}
1300
1301static int
1302symtable_implicit_arg(struct symtable *st, int pos)
1303{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001304 PyObject *id = PyString_FromFormat(".%d", pos);
1305 if (id == NULL)
1306 return 0;
1307 if (!symtable_add_def(st, id, DEF_PARAM)) {
1308 Py_DECREF(id);
1309 return 0;
1310 }
1311 Py_DECREF(id);
1312 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313}
1314
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001315static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1317{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001318 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001320 /* go through all the toplevel arguments first */
1321 for (i = 0; i < asdl_seq_LEN(args); i++) {
1322 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1323 if (arg->kind == Name_kind) {
1324 assert(arg->v.Name.ctx == Param ||
1325 (arg->v.Name.ctx == Store && !toplevel));
1326 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1327 return 0;
1328 }
1329 else if (arg->kind == Tuple_kind) {
1330 assert(arg->v.Tuple.ctx == Store);
1331 if (toplevel) {
1332 if (!symtable_implicit_arg(st, i))
1333 return 0;
1334 }
1335 }
1336 else {
1337 PyErr_SetString(PyExc_SyntaxError,
1338 "invalid expression in parameter list");
1339 PyErr_SyntaxLocation(st->st_filename,
1340 st->st_cur->ste_lineno);
1341 return 0;
1342 }
1343 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001345 if (!toplevel) {
1346 if (!symtable_visit_params_nested(st, args))
1347 return 0;
1348 }
1349
1350 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351}
1352
1353static int
1354symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1355{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001356 int i;
1357 for (i = 0; i < asdl_seq_LEN(args); i++) {
1358 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1359 if (arg->kind == Tuple_kind &&
1360 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1361 return 0;
1362 }
1363
1364 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365}
1366
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001367static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368symtable_visit_arguments(struct symtable *st, arguments_ty a)
1369{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001370 /* skip default arguments inside function block
1371 XXX should ast be different?
1372 */
1373 if (a->args && !symtable_visit_params(st, a->args, 1))
1374 return 0;
1375 if (a->vararg) {
1376 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1377 return 0;
1378 st->st_cur->ste_varargs = 1;
1379 }
1380 if (a->kwarg) {
1381 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1382 return 0;
1383 st->st_cur->ste_varkeywords = 1;
1384 }
1385 if (a->args && !symtable_visit_params_nested(st, a->args))
1386 return 0;
1387 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388}
1389
1390
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001391static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1393{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001394 if (eh->v.ExceptHandler.type)
1395 VISIT(st, expr, eh->v.ExceptHandler.type);
1396 if (eh->v.ExceptHandler.name)
1397 VISIT(st, expr, eh->v.ExceptHandler.name);
1398 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1399 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400}
1401
1402
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001403static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404symtable_visit_alias(struct symtable *st, alias_ty a)
1405{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001406 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc6660cf2010-06-11 21:40:37 +00001407 operation. It is different than a->name when a->name is a
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001408 dotted package name (e.g. spam.eggs)
1409 */
1410 PyObject *store_name;
1411 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1412 const char *base = PyString_AS_STRING(name);
1413 char *dot = strchr(base, '.');
1414 if (dot) {
1415 store_name = PyString_FromStringAndSize(base, dot - base);
1416 if (!store_name)
1417 return 0;
1418 }
1419 else {
1420 store_name = name;
1421 Py_INCREF(store_name);
1422 }
1423 if (strcmp(PyString_AS_STRING(name), "*")) {
1424 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1425 Py_DECREF(store_name);
1426 return r;
1427 }
1428 else {
Serhiy Storchaka65d18872017-12-02 21:00:09 +02001429 if (st->st_cur->ste_type != ModuleBlock &&
1430 !symtable_warn(st, PyExc_SyntaxWarning, IMPORT_STAR_WARNING, -1))
1431 {
1432 Py_DECREF(store_name);
1433 return 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001434 }
1435 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1436 Py_DECREF(store_name);
1437 return 1;
1438 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439}
1440
1441
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001442static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1444{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001445 VISIT(st, expr, lc->target);
1446 VISIT(st, expr, lc->iter);
1447 VISIT_SEQ(st, expr, lc->ifs);
1448 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449}
1450
1451
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001452static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453symtable_visit_keyword(struct symtable *st, keyword_ty k)
1454{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001455 VISIT(st, expr, k->value);
1456 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457}
1458
1459
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001460static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461symtable_visit_slice(struct symtable *st, slice_ty s)
1462{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001463 switch (s->kind) {
1464 case Slice_kind:
1465 if (s->v.Slice.lower)
1466 VISIT(st, expr, s->v.Slice.lower)
1467 if (s->v.Slice.upper)
1468 VISIT(st, expr, s->v.Slice.upper)
1469 if (s->v.Slice.step)
1470 VISIT(st, expr, s->v.Slice.step)
1471 break;
1472 case ExtSlice_kind:
1473 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1474 break;
1475 case Index_kind:
1476 VISIT(st, expr, s->v.Index.value)
1477 break;
1478 case Ellipsis_kind:
1479 break;
1480 }
1481 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482}
1483
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001484static int
1485symtable_new_tmpname(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001487 char tmpname[256];
1488 identifier tmp;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001489
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001490 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1491 ++st->st_cur->ste_tmpname);
1492 tmp = PyString_InternFromString(tmpname);
1493 if (!tmp)
1494 return 0;
1495 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1496 return 0;
1497 Py_DECREF(tmp);
1498 return 1;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001499}
1500
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001501static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001502symtable_handle_comprehension(struct symtable *st, expr_ty e,
1503 identifier scope_name, asdl_seq *generators,
1504 expr_ty elt, expr_ty value)
1505{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001506 int is_generator = (e->kind == GeneratorExp_kind);
1507 int needs_tmp = !is_generator;
1508 comprehension_ty outermost = ((comprehension_ty)
1509 asdl_seq_GET(generators, 0));
1510 /* Outermost iterator is evaluated in current scope */
1511 VISIT(st, expr, outermost->iter);
1512 /* Create comprehension scope for the rest */
1513 if (!scope_name ||
1514 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
1515 return 0;
1516 }
Serhiy Storchaka65d18872017-12-02 21:00:09 +02001517 /* In order to check for yield expressions under '-3', we clear
1518 the generator flag, and restore it at the end */
1519 is_generator |= st->st_cur->ste_generator;
1520 st->st_cur->ste_generator = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001521 /* Outermost iter is received as an argument */
1522 if (!symtable_implicit_arg(st, 0)) {
1523 symtable_exit_block(st, (void *)e);
1524 return 0;
1525 }
1526 /* Allocate temporary name if needed */
1527 if (needs_tmp && !symtable_new_tmpname(st)) {
1528 symtable_exit_block(st, (void *)e);
1529 return 0;
1530 }
1531 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1532 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1533 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1534 generators, 1, (void*)e);
1535 if (value)
1536 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1537 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Serhiy Storchaka65d18872017-12-02 21:00:09 +02001538 if (Py_Py3kWarningFlag && st->st_cur->ste_generator) {
1539 const char *msg = (
1540 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1541 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1542 "'yield' inside generator expression");
1543 if (!symtable_warn(st, PyExc_DeprecationWarning, msg, -1)) {
1544 symtable_exit_block(st, (void *)e);
1545 return 0;
1546 }
1547 }
1548 st->st_cur->ste_generator |= is_generator;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001549 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550}
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001551
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001552static int
Serhiy Storchaka65d18872017-12-02 21:00:09 +02001553symtable_visit_listcomp(struct symtable *st, expr_ty e)
1554{
1555 asdl_seq *generators = e->v.ListComp.generators;
1556 int i, is_generator;
1557 /* In order to check for yield expressions under '-3', we clear
1558 the generator flag, and restore it at the end */
1559 is_generator = st->st_cur->ste_generator;
1560 st->st_cur->ste_generator = 0;
1561 VISIT(st, expr, e->v.ListComp.elt);
1562 for (i = 0; i < asdl_seq_LEN(generators); i++) {
1563 comprehension_ty lc = (comprehension_ty)asdl_seq_GET(generators, i);
1564 VISIT(st, expr, lc->target);
1565 if (i == 0 && !st->st_cur->ste_generator) {
1566 /* 'yield' in the outermost iterator doesn't cause a warning */
1567 VISIT(st, expr, lc->iter);
1568 is_generator |= st->st_cur->ste_generator;
1569 st->st_cur->ste_generator = 0;
1570 }
1571 else {
1572 VISIT(st, expr, lc->iter);
1573 }
1574 VISIT_SEQ(st, expr, lc->ifs);
1575 }
1576
1577 if (Py_Py3kWarningFlag && st->st_cur->ste_generator) {
1578 const char *msg = "'yield' inside list comprehension";
1579 if (!symtable_warn(st, PyExc_DeprecationWarning, msg, -1)) {
1580 return 0;
1581 }
1582 }
1583 st->st_cur->ste_generator |= is_generator;
1584 return 1;
1585}
1586
1587static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001588symtable_visit_genexp(struct symtable *st, expr_ty e)
1589{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001590 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1591 e->v.GeneratorExp.generators,
1592 e->v.GeneratorExp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001593}
1594
1595static int
1596symtable_visit_setcomp(struct symtable *st, expr_ty e)
1597{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001598 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1599 e->v.SetComp.generators,
1600 e->v.SetComp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001601}
1602
1603static int
1604symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1605{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001606 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1607 e->v.DictComp.generators,
1608 e->v.DictComp.key,
1609 e->v.DictComp.value);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001610}