blob: 49d153bd3b840cf4ca61a4fa4bfc470c9d8b9d89 [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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007/* two error strings used for warnings */
8#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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014PySTEntryObject *
15PySTEntry_New(struct symtable *st, identifier name, block_ty block,
16 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000017{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018 PySTEntryObject *ste = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000019 PyObject *k, *v;
20
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021 k = PyLong_FromVoidPtr(key);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000022 if (k == NULL)
23 goto fail;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024 ste = (PySTEntryObject *)PyObject_New(PySTEntryObject,
25 &PySTEntry_Type);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000026 ste->ste_table = st;
27 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000029
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000030 ste->ste_name = name;
31 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000032
33 v = PyDict_New();
34 if (v == NULL)
35 goto fail;
36 ste->ste_symbols = v;
37
38 v = PyList_New(0);
39 if (v == NULL)
40 goto fail;
41 ste->ste_varnames = v;
42
43 v = PyList_New(0);
44 if (v == NULL)
45 goto fail;
46 ste->ste_children = v;
47
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048 ste->ste_type = block;
49 ste->ste_unoptimized = 0;
50 ste->ste_nested = 0;
51 ste->ste_free = 0;
52 ste->ste_varargs = 0;
53 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000054 ste->ste_opt_lineno = 0;
Jeremy Hylton4d508ad2003-05-21 17:34:50 +000055 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000056 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000057
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000058 if (st->st_cur != NULL &&
59 (st->st_cur->ste_nested ||
60 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000061 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000062 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000063 ste->ste_generator = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000064
65 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
66 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000067
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000068 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069 fail:
70 Py_XDECREF(ste);
71 return NULL;
72}
73
74static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000075ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000076{
77 char buf[256];
78
Barry Warsaw4b4ab202001-11-28 21:36:28 +000079 PyOS_snprintf(buf, sizeof(buf),
80 "<symtable entry %.100s(%ld), line %d>",
81 PyString_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083 return PyString_FromString(buf);
84}
85
86static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088{
89 ste->ste_table = NULL;
90 Py_XDECREF(ste->ste_id);
91 Py_XDECREF(ste->ste_name);
92 Py_XDECREF(ste->ste_symbols);
93 Py_XDECREF(ste->ste_varnames);
94 Py_XDECREF(ste->ste_children);
95 PyObject_Del(ste);
96}
97
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000099
Guido van Rossum6f799372001-09-20 20:46:19 +0000100static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000101 {"id", T_OBJECT, OFF(ste_id), READONLY},
102 {"name", T_OBJECT, OFF(ste_name), READONLY},
103 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
104 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
105 {"children", T_OBJECT, OFF(ste_children), READONLY},
106 {"type", T_INT, OFF(ste_type), READONLY},
107 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108 {NULL}
109};
110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111PyTypeObject PySTEntry_Type = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000112 PyObject_HEAD_INIT(&PyType_Type)
113 0,
114 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000115 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000116 0,
117 (destructor)ste_dealloc, /* tp_dealloc */
118 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000119 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120 0, /* tp_setattr */
121 0, /* tp_compare */
122 (reprfunc)ste_repr, /* tp_repr */
123 0, /* tp_as_number */
124 0, /* tp_as_sequence */
125 0, /* tp_as_mapping */
126 0, /* tp_hash */
127 0, /* tp_call */
128 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000129 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130 0, /* tp_setattro */
131 0, /* tp_as_buffer */
132 Py_TPFLAGS_DEFAULT, /* tp_flags */
133 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000134 0, /* tp_traverse */
135 0, /* tp_clear */
136 0, /* tp_richcompare */
137 0, /* tp_weaklistoffset */
138 0, /* tp_iter */
139 0, /* tp_iternext */
140 0, /* tp_methods */
141 ste_memberlist, /* tp_members */
142 0, /* tp_getset */
143 0, /* tp_base */
144 0, /* tp_dict */
145 0, /* tp_descr_get */
146 0, /* tp_descr_set */
147 0, /* tp_dictoffset */
148 0, /* tp_init */
149 0, /* tp_alloc */
150 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000151};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000152
153static int symtable_analyze(struct symtable *st);
154static int symtable_warn(struct symtable *st, char *msg);
155static int symtable_enter_block(struct symtable *st, identifier name,
156 block_ty block, void *ast, int lineno);
157static int symtable_exit_block(struct symtable *st, void *ast);
158static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
159static int symtable_visit_expr(struct symtable *st, expr_ty s);
160static int symtable_visit_genexp(struct symtable *st, expr_ty s);
161static int symtable_visit_arguments(struct symtable *st, arguments_ty);
162static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
163static int symtable_visit_alias(struct symtable *st, alias_ty);
164static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
165static int symtable_visit_keyword(struct symtable *st, keyword_ty);
166static int symtable_visit_slice(struct symtable *st, slice_ty);
167static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
168static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
169static int symtable_implicit_arg(struct symtable *st, int pos);
170
171
172static identifier top = NULL, lambda = NULL;
173
174#define GET_IDENTIFIER(VAR) \
175 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
176
177#define DUPLICATE_ARGUMENT \
178"duplicate argument '%s' in function definition"
179
180static struct symtable *
181symtable_new(void)
182{
183 struct symtable *st;
184
185 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
186 if (st == NULL)
187 return NULL;
188
189 st->st_filename = NULL;
190 if ((st->st_stack = PyList_New(0)) == NULL)
191 goto fail;
192 if ((st->st_symbols = PyDict_New()) == NULL)
193 goto fail;
194 st->st_cur = NULL;
195 st->st_tmpname = 0;
196 st->st_private = NULL;
197 return st;
198 fail:
199 PySymtable_Free(st);
200 return NULL;
201}
202
203struct symtable *
204PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
205{
206 struct symtable *st = symtable_new();
207 asdl_seq *seq;
208 int i;
209
210 if (st == NULL)
211 return st;
212 st->st_filename = filename;
213 st->st_future = future;
214 symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock,
215 (void *)mod, 0);
216 st->st_top = st->st_cur;
217 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
218 /* Any other top-level initialization? */
219 switch (mod->kind) {
220 case Module_kind:
221 seq = mod->v.Module.body;
222 for (i = 0; i < asdl_seq_LEN(seq); i++)
223 if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
224 goto error;
225 break;
226 case Expression_kind:
227 if (!symtable_visit_expr(st, mod->v.Expression.body))
228 goto error;
229 break;
230 case Interactive_kind:
231 seq = mod->v.Interactive.body;
232 for (i = 0; i < asdl_seq_LEN(seq); i++)
233 if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
234 goto error;
235 break;
236 case Suite_kind:
237 PyErr_SetString(PyExc_RuntimeError,
238 "this compiler does not handle Suites");
239 return NULL;
240 }
241 if (!symtable_exit_block(st, (void *)mod))
242 return NULL;
243 if (symtable_analyze(st))
244 return st;
245 error:
246 PySymtable_Free(st);
247 return NULL;
248}
249
250void
251PySymtable_Free(struct symtable *st)
252{
253 Py_XDECREF(st->st_symbols);
254 Py_XDECREF(st->st_stack);
255 PyMem_Free((void *)st);
256}
257
258PySTEntryObject *
259PySymtable_Lookup(struct symtable *st, void *key)
260{
261 PyObject *k, *v;
262
263 k = PyLong_FromVoidPtr(key);
264 if (k == NULL)
265 return NULL;
266 v = PyDict_GetItem(st->st_symbols, k);
267 if (v) {
268 assert(PySTEntry_Check(v));
269 Py_DECREF(k);
270 Py_INCREF(v);
271 return (PySTEntryObject *)v;
272 }
273 else {
274 PyErr_SetString(PyExc_KeyError,
275 "unknown symbol table entry");
276 return NULL;
277 }
278}
279
280int
281PyST_GetScope(PySTEntryObject *ste, PyObject *name)
282{
283 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
284 if (!v)
285 return 0;
286 assert(PyInt_Check(v));
287 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
288}
289
290
291/* Analyze raw symbol information to determine scope of each name.
292
293 The next several functions are helpers for PySymtable_Analyze(),
294 which determines whether a name is local, global, or free. In addition,
295 it determines which local variables are cell variables; they provide
296 bindings that are used for free variables in enclosed blocks.
297
298 There are also two kinds of free variables, implicit and explicit. An
299 explicit global is declared with the global statement. An implicit
300 global is a free variable for which the compiler has found no binding
301 in an enclosing function scope. The implicit global is either a global
302 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
303 to handle these names to implement slightly odd semantics. In such a
304 block, the name is treated as global until it is assigned to; then it
305 is treated as a local.
306
307 The symbol table requires two passes to determine the scope of each name.
308 The first pass collects raw facts from the AST: the name is a parameter
309 here, the name is used by not defined here, etc. The second pass analyzes
310 these facts during a pass over the PySTEntryObjects created during pass 1.
311
312 When a function is entered during the second pass, the parent passes
313 the set of all name bindings visible to its children. These bindings
314 are used to determine if the variable is free or an implicit global.
315 After doing the local analysis, it analyzes each of its child blocks
316 using an updated set of name bindings.
317
318 The children update the free variable set. If a local variable is free
319 in a child, the variable is marked as a cell. The current function must
320 provide runtime storage for the variable that may outlive the function's
321 frame. Cell variables are removed from the free set before the analyze
322 function returns to its parent.
323
324 The sets of bound and free variables are implemented as dictionaries
325 mapping strings to None.
326*/
327
328#define SET_SCOPE(DICT, NAME, I) { \
329 PyObject *o = PyInt_FromLong(I); \
330 if (!o) \
331 return 0; \
332 if (PyDict_SetItem((DICT), (NAME), o) < 0) \
333 return 0; \
334}
335
336/* Decide on scope of name, given flags.
337
338 The dicts passed in as arguments are modified as necessary.
339 ste is passed so that flags can be updated.
340*/
341
342static int
343analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, int flags,
344 PyObject *bound, PyObject *local, PyObject *free,
345 PyObject *global)
346{
347 if (flags & DEF_GLOBAL) {
348 if (flags & DEF_PARAM) {
349 PyErr_Format(PyExc_SyntaxError,
350 "name '%s' is local and global",
351 PyString_AS_STRING(name));
352 return 0;
353 }
354 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
355 if (PyDict_SetItem(global, name, Py_None) < 0)
356 return 0;
357 if (bound && PyDict_GetItem(bound, name)) {
358 if (PyDict_DelItem(bound, name) < 0)
359 return 0;
360 }
361 return 1;
362 }
363 if (flags & DEF_BOUND) {
364 SET_SCOPE(dict, name, LOCAL);
365 if (PyDict_SetItem(local, name, Py_None) < 0)
366 return 0;
367 if (PyDict_GetItem(global, name)) {
368 if (PyDict_DelItem(global, name) < 0)
369 return 0;
370 }
371 return 1;
372 }
373 /* If an enclosing block has a binding for this name, it
374 is a free variable rather than a global variable.
375 Note that having a non-NULL bound implies that the block
376 is nested.
377 */
378 if (bound && PyDict_GetItem(bound, name)) {
379 SET_SCOPE(dict, name, FREE);
380 ste->ste_free = 1;
381 if (PyDict_SetItem(free, name, Py_None) < 0)
382 return 0;
383 return 1;
384 }
385 /* If a parent has a global statement, then call it global
386 explicit? It could also be global implicit.
387 */
388 else if (global && PyDict_GetItem(global, name)) {
389 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
390 return 1;
391 }
392 else {
393 if (ste->ste_nested)
394 ste->ste_free = 1;
395 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
396 return 1;
397 }
398 return 0; /* Can't get here */
399}
400
401#undef SET_SCOPE
402
403/* If a name is defined in free and also in locals, then this block
404 provides the binding for the free variable. The name should be
405 marked CELL in this block and removed from the free list.
406
407 Note that the current block's free variables are included in free.
408 That's safe because no name can be free and local in the same scope.
409*/
410
411static int
412analyze_cells(PyObject *scope, PyObject *free)
413{
414 PyObject *name, *v, *w;
415 int flags, pos = 0, success = 0;
416
417 w = PyInt_FromLong(CELL);
418 if (!w)
419 return 0;
420 while (PyDict_Next(scope, &pos, &name, &v)) {
421 assert(PyInt_Check(v));
422 flags = PyInt_AS_LONG(v);
423 if (flags != LOCAL)
424 continue;
425 if (!PyDict_GetItem(free, name))
426 continue;
427 /* Replace LOCAL with CELL for this name, and remove
428 from free. It is safe to replace the value of name
429 in the dict, because it will not cause a resize.
430 */
431 if (PyDict_SetItem(scope, name, w) < 0)
432 goto error;
433 if (!PyDict_DelItem(free, name) < 0)
434 goto error;
435 }
436 success = 1;
437 error:
438 Py_DECREF(w);
439 return success;
440}
441
442/* Check for illegal statements in unoptimized namespaces */
443static int
444check_unoptimized(const PySTEntryObject* ste) {
445 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000446 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000447
448 if (ste->ste_type == ModuleBlock || !ste->ste_unoptimized
449 || !(ste->ste_free || ste->ste_child_free))
450 return 1;
451
Armin Rigo31441302005-10-21 12:57:31 +0000452 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453 "contains a nested function with free variables" :
454 "is a nested function");
455
456 switch (ste->ste_unoptimized) {
457 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
458 case OPT_EXEC: /* qualified exec is fine */
459 return 1;
460 case OPT_IMPORT_STAR:
461 PyOS_snprintf(buf, sizeof(buf),
462 "import * is not allowed in function '%.100s' "
463 "because it is %s",
464 PyString_AS_STRING(ste->ste_name), trailer);
465 break;
466 case OPT_BARE_EXEC:
467 PyOS_snprintf(buf, sizeof(buf),
468 "unqualified exec is not allowed in function "
469 "'%.100s' it %s",
470 PyString_AS_STRING(ste->ste_name), trailer);
471 break;
472 default:
473 PyOS_snprintf(buf, sizeof(buf),
474 "function '%.100s' uses import * and bare exec, "
475 "which are illegal because it %s",
476 PyString_AS_STRING(ste->ste_name), trailer);
477 break;
478 }
479
480 PyErr_SetString(PyExc_SyntaxError, buf);
481 PyErr_SyntaxLocation(ste->ste_table->st_filename,
482 ste->ste_opt_lineno);
483 return 0;
484}
485
486/* Enter the final scope information into the st_symbols dict.
487 *
488 * All arguments are dicts. Modifies symbols, others are read-only.
489*/
490static int
491update_symbols(PyObject *symbols, PyObject *scope,
492 PyObject *bound, PyObject *free, int class)
493{
494 PyObject *name, *v, *u, *w, *free_value = NULL;
495 int i, flags, pos = 0;
496
497 while (PyDict_Next(symbols, &pos, &name, &v)) {
498 assert(PyInt_Check(v));
499 flags = PyInt_AS_LONG(v);
500 w = PyDict_GetItem(scope, name);
501 assert(w && PyInt_Check(w));
502 i = PyInt_AS_LONG(w);
503 flags |= (i << SCOPE_OFF);
504 u = PyInt_FromLong(flags);
505 if (PyDict_SetItem(symbols, name, u) < 0) {
506 Py_DECREF(u);
507 return 0;
508 }
509 Py_DECREF(u);
510 }
511
512 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
513 if (!free_value)
514 return 0;
515
516 /* add a free variable when it's only use is for creating a closure */
517 pos = 0;
518 while (PyDict_Next(free, &pos, &name, &v)) {
519 PyObject *o = PyDict_GetItem(symbols, name);
520
521 if (o) {
522 /* It could be a free variable in a method of
523 the class that has the same name as a local
524 or global in the class scope.
525 */
526 if (class &&
527 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
528 int i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
529 o = PyInt_FromLong(i);
530 if (!o) {
531 Py_DECREF(free_value);
532 return 0;
533 }
534 if (PyDict_SetItem(symbols, name, o) < 0) {
535 Py_DECREF(o);
536 Py_DECREF(free_value);
537 return 0;
538 }
539 }
540 /* else it's not free, probably a cell */
541 continue;
542 }
543 if (!PyDict_GetItem(bound, name))
544 continue; /* it's a global */
545
546 if (PyDict_SetItem(symbols, name, free_value) < 0) {
547 Py_DECREF(free_value);
548 return 0;
549 }
550 }
551 Py_DECREF(free_value);
552 return 1;
553}
554
555/* Make final symbol table decisions for block of ste.
556 Arguments:
557 ste -- current symtable entry (input/output)
558 bound -- set of variables bound in enclosing scopes (input)
559 free -- set of free variables in enclosed scopes (output)
560 globals -- set of declared global variables in enclosing scopes (input)
561*/
562
563static int
564analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
565 PyObject *global)
566{
567 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
568 PyObject *newglobal = NULL, *newfree = NULL;
569 int i, flags, pos = 0, success = 0;
570
571 local = PyDict_New();
572 if (!local)
573 goto error;
574 scope = PyDict_New();
575 if (!scope)
576 goto error;
577 newglobal = PyDict_New();
578 if (!newglobal)
579 goto error;
580 newfree = PyDict_New();
581 if (!newfree)
582 goto error;
583 newbound = PyDict_New();
584 if (!newbound)
585 goto error;
586
587 if (ste->ste_type == ClassBlock) {
588 /* make a copy of globals before calling analyze_name(),
589 because global statements in the class have no effect
590 on nested functions.
591 */
592 if (PyDict_Update(newglobal, global) < 0)
593 goto error;
594 if (bound)
595 if (PyDict_Update(newbound, bound) < 0)
596 goto error;
597 }
598
599 assert(PySTEntry_Check(ste));
600 assert(PyDict_Check(ste->ste_symbols));
601 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
602 flags = PyInt_AS_LONG(v);
603 if (!analyze_name(ste, scope, name, flags, bound, local, free,
604 global))
605 goto error;
606 }
607
608 if (ste->ste_type != ClassBlock) {
609 if (ste->ste_type == FunctionBlock) {
610 if (PyDict_Update(newbound, local) < 0)
611 goto error;
612 }
613 if (bound) {
614 if (PyDict_Update(newbound, bound) < 0)
615 goto error;
616 }
617 if (PyDict_Update(newglobal, global) < 0)
618 goto error;
619 }
620
621 /* Recursively call analyze_block() on each child block */
622 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
623 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000624 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000626 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627 if (!analyze_block(entry, newbound, newfree, newglobal))
628 goto error;
629 if (entry->ste_free || entry->ste_child_free)
630 ste->ste_child_free = 1;
631 }
632
633 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
634 goto error;
635 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
636 ste->ste_type == ClassBlock))
637 goto error;
638 if (!check_unoptimized(ste))
639 goto error;
640
641 if (PyDict_Update(free, newfree) < 0)
642 goto error;
643 success = 1;
644 error:
645 Py_XDECREF(local);
646 Py_XDECREF(scope);
647 Py_XDECREF(newbound);
648 Py_XDECREF(newglobal);
649 Py_XDECREF(newfree);
650 if (!success)
651 assert(PyErr_Occurred());
652 return success;
653}
654
655static int
656symtable_analyze(struct symtable *st)
657{
658 PyObject *free, *global;
659 int r;
660
661 free = PyDict_New();
662 if (!free)
663 return 0;
664 global = PyDict_New();
665 if (!global) {
666 Py_DECREF(global);
667 return 0;
668 }
669 r = analyze_block(st->st_top, NULL, free, global);
670 Py_DECREF(free);
671 Py_DECREF(global);
672 return r;
673}
674
675
676static int
677symtable_warn(struct symtable *st, char *msg)
678{
679 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
680 st->st_cur->ste_lineno, NULL, NULL) < 0) {
681 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
682 PyErr_SetString(PyExc_SyntaxError, msg);
683 PyErr_SyntaxLocation(st->st_filename,
684 st->st_cur->ste_lineno);
685 }
686 return 0;
687 }
688 return 1;
689}
690
691/* symtable_enter_block() gets a reference via PySTEntry_New().
692 This reference is released when the block is exited, via the DECREF
693 in symtable_exit_block().
694*/
695
696static int
697symtable_exit_block(struct symtable *st, void *ast)
698{
699 int end;
700
701 Py_DECREF(st->st_cur);
702 end = PyList_GET_SIZE(st->st_stack) - 1;
703 if (end >= 0) {
704 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
705 end);
706 Py_INCREF(st->st_cur);
707 if (PySequence_DelItem(st->st_stack, end) < 0)
708 return 0;
709 }
710 return 1;
711}
712
713static int
714symtable_enter_block(struct symtable *st, identifier name, block_ty block,
715 void *ast, int lineno)
716{
717 PySTEntryObject *prev = NULL;
718
719 if (st->st_cur) {
720 prev = st->st_cur;
721 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
722 Py_DECREF(st->st_cur);
723 return 0;
724 }
725 Py_DECREF(st->st_cur);
726 }
727 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
728 if (name == GET_IDENTIFIER(top))
729 st->st_global = st->st_cur->ste_symbols;
730 if (prev) {
731 if (PyList_Append(prev->ste_children,
732 (PyObject *)st->st_cur) < 0) {
733 return 0;
734 }
735 }
736 return 1;
737}
738
739static int
740symtable_lookup(struct symtable *st, PyObject *name)
741{
742 PyObject *o;
743
744 o = PyDict_GetItem(st->st_cur->ste_symbols, name);
745 if (!o)
746 return 0;
747 return PyInt_AsLong(o);
748}
749
750static int
751symtable_add_def(struct symtable *st, PyObject *name, int flag)
752{
753 PyObject *o;
754 PyObject *dict;
755 int val;
756
757 dict = st->st_cur->ste_symbols;
758 if ((o = PyDict_GetItem(dict, name))) {
759 val = PyInt_AS_LONG(o);
760 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
761 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
762 PyString_AsString(name));
763 PyErr_SyntaxLocation(st->st_filename,
764 st->st_cur->ste_lineno);
765 return 0;
766 }
767 val |= flag;
768 } else
769 val = flag;
770 o = PyInt_FromLong(val);
771 if (o == NULL)
772 return 0;
773 if (PyDict_SetItem(dict, name, o) < 0) {
774 Py_DECREF(o);
775 return 0;
776 }
777 Py_DECREF(o);
778
779 if (flag & DEF_PARAM) {
780 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
781 return 0;
782 } else if (flag & DEF_GLOBAL) {
783 /* XXX need to update DEF_GLOBAL for other flags too;
784 perhaps only DEF_FREE_GLOBAL */
785 val = flag;
786 if ((o = PyDict_GetItem(st->st_global, name))) {
787 val |= PyInt_AS_LONG(o);
788 }
789 o = PyInt_FromLong(val);
790 if (o == NULL)
791 return 0;
792 if (PyDict_SetItem(st->st_global, name, o) < 0) {
793 Py_DECREF(o);
794 return 0;
795 }
796 Py_DECREF(o);
797 }
798 return 1;
799}
800
801/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
802 They use the ASDL name to synthesize the name of the C type and the visit
803 function.
804
805 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
806 useful if the first node in the sequence requires special treatment.
807*/
808
809#define VISIT(ST, TYPE, V) \
810 if (!symtable_visit_ ## TYPE((ST), (V))) \
811 return 0;
812
813#define VISIT_SEQ(ST, TYPE, SEQ) { \
814 int i; \
815 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
816 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
817 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
818 if (!symtable_visit_ ## TYPE((ST), elt)) \
819 return 0; \
820 } \
821}
822
823#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
824 int i; \
825 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
826 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
827 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
828 if (!symtable_visit_ ## TYPE((ST), elt)) \
829 return 0; \
830 } \
831}
832
833static int
834symtable_visit_stmt(struct symtable *st, stmt_ty s)
835{
836 switch (s->kind) {
837 case FunctionDef_kind:
838 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
839 return 0;
840 if (s->v.FunctionDef.args->defaults)
841 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
842 if (s->v.FunctionDef.decorators)
843 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
844 if (!symtable_enter_block(st, s->v.FunctionDef.name,
845 FunctionBlock, (void *)s, s->lineno))
846 return 0;
847 VISIT(st, arguments, s->v.FunctionDef.args);
848 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
849 if (!symtable_exit_block(st, s))
850 return 0;
851 break;
852 case ClassDef_kind:
853 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
854 return 0;
855 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
856 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
857 (void *)s, s->lineno))
858 return 0;
859 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
860 if (!symtable_exit_block(st, s))
861 return 0;
862 break;
863 case Return_kind:
864 if (s->v.Return.value)
865 VISIT(st, expr, s->v.Return.value);
866 break;
867 case Delete_kind:
868 VISIT_SEQ(st, expr, s->v.Delete.targets);
869 break;
870 case Assign_kind:
871 VISIT_SEQ(st, expr, s->v.Assign.targets);
872 VISIT(st, expr, s->v.Assign.value);
873 break;
874 case AugAssign_kind:
875 VISIT(st, expr, s->v.AugAssign.target);
876 VISIT(st, expr, s->v.AugAssign.value);
877 break;
878 case Print_kind:
879 if (s->v.Print.dest)
880 VISIT(st, expr, s->v.Print.dest);
881 VISIT_SEQ(st, expr, s->v.Print.values);
882 break;
883 case For_kind:
884 VISIT(st, expr, s->v.For.target);
885 VISIT(st, expr, s->v.For.iter);
886 VISIT_SEQ(st, stmt, s->v.For.body);
887 if (s->v.For.orelse)
888 VISIT_SEQ(st, stmt, s->v.For.orelse);
889 break;
890 case While_kind:
891 VISIT(st, expr, s->v.While.test);
892 VISIT_SEQ(st, stmt, s->v.While.body);
893 if (s->v.While.orelse)
894 VISIT_SEQ(st, stmt, s->v.While.orelse);
895 break;
896 case If_kind:
897 /* XXX if 0: and lookup_yield() hacks */
898 VISIT(st, expr, s->v.If.test);
899 VISIT_SEQ(st, stmt, s->v.If.body);
900 if (s->v.If.orelse)
901 VISIT_SEQ(st, stmt, s->v.If.orelse);
902 break;
903 case Raise_kind:
904 if (s->v.Raise.type) {
905 VISIT(st, expr, s->v.Raise.type);
906 if (s->v.Raise.inst) {
907 VISIT(st, expr, s->v.Raise.inst);
908 if (s->v.Raise.tback)
909 VISIT(st, expr, s->v.Raise.tback);
910 }
911 }
912 break;
913 case TryExcept_kind:
914 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
915 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
916 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
917 break;
918 case TryFinally_kind:
919 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
920 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
921 break;
922 case Assert_kind:
923 VISIT(st, expr, s->v.Assert.test);
924 if (s->v.Assert.msg)
925 VISIT(st, expr, s->v.Assert.msg);
926 break;
927 case Import_kind:
928 VISIT_SEQ(st, alias, s->v.Import.names);
929 /* XXX Don't have the lineno available inside
930 visit_alias */
931 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
932 st->st_cur->ste_opt_lineno = s->lineno;
933 break;
934 case ImportFrom_kind:
935 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
936 /* XXX Don't have the lineno available inside
937 visit_alias */
938 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
939 st->st_cur->ste_opt_lineno = s->lineno;
940 break;
941 case Exec_kind:
942 VISIT(st, expr, s->v.Exec.body);
943 if (!st->st_cur->ste_opt_lineno)
944 st->st_cur->ste_opt_lineno = s->lineno;
945 if (s->v.Exec.globals) {
946 st->st_cur->ste_unoptimized |= OPT_EXEC;
947 VISIT(st, expr, s->v.Exec.globals);
948 if (s->v.Exec.locals)
949 VISIT(st, expr, s->v.Exec.locals);
950 } else {
951 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
952 }
953 break;
954 case Global_kind: {
955 int i;
956 asdl_seq *seq = s->v.Global.names;
957 for (i = 0; i < asdl_seq_LEN(seq); i++) {
958 identifier name = asdl_seq_GET(seq, i);
959 char *c_name = PyString_AS_STRING(name);
960 int cur = symtable_lookup(st, name);
961 if (cur < 0)
962 return 0;
963 if (cur & (DEF_LOCAL | USE)) {
964 char buf[1000];
965 if (cur & DEF_LOCAL)
966 PyOS_snprintf(buf, sizeof(buf),
967 GLOBAL_AFTER_ASSIGN,
968 c_name);
969 else
970 PyOS_snprintf(buf, sizeof(buf),
971 GLOBAL_AFTER_USE,
972 c_name);
973 if (!symtable_warn(st, buf))
974 return 0;
975 }
976 if (!symtable_add_def(st, name, DEF_GLOBAL))
977 return 0;
978
979 }
980
981 break;
982 }
983 case Expr_kind:
984 VISIT(st, expr, s->v.Expr.value);
985 break;
986 case Pass_kind:
987 case Break_kind:
988 case Continue_kind:
989 /* nothing to do here */
990 break;
991 }
992 return 1;
993}
994
995static int
996symtable_visit_expr(struct symtable *st, expr_ty e)
997{
998 switch (e->kind) {
999 case BoolOp_kind:
1000 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1001 break;
1002 case BinOp_kind:
1003 VISIT(st, expr, e->v.BinOp.left);
1004 VISIT(st, expr, e->v.BinOp.right);
1005 break;
1006 case UnaryOp_kind:
1007 VISIT(st, expr, e->v.UnaryOp.operand);
1008 break;
1009 case Lambda_kind: {
1010 if (!symtable_add_def(st, GET_IDENTIFIER(lambda), DEF_LOCAL))
1011 return 0;
1012 if (e->v.Lambda.args->defaults)
1013 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1014 /* XXX how to get line numbers for expressions */
1015 if (!symtable_enter_block(st, GET_IDENTIFIER(lambda),
1016 FunctionBlock, (void *)e, 0))
1017 return 0;
1018 VISIT(st, arguments, e->v.Lambda.args);
1019 VISIT(st, expr, e->v.Lambda.body);
1020 if (!symtable_exit_block(st, (void *)e))
1021 return 0;
1022 break;
1023 }
1024 case Dict_kind:
1025 VISIT_SEQ(st, expr, e->v.Dict.keys);
1026 VISIT_SEQ(st, expr, e->v.Dict.values);
1027 break;
1028 case ListComp_kind: {
1029 char tmpname[256];
1030 identifier tmp;
1031
1032 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1033 ++st->st_cur->ste_tmpname);
1034 tmp = PyString_FromString(tmpname);
1035 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1036 return 0;
1037 VISIT(st, expr, e->v.ListComp.elt);
1038 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1039 break;
1040 }
1041 case GeneratorExp_kind: {
1042 if (!symtable_visit_genexp(st, e)) {
1043 return 0;
1044 }
1045 break;
1046 }
1047 case Yield_kind:
1048 if (e->v.Yield.value)
1049 VISIT(st, expr, e->v.Yield.value);
1050 st->st_cur->ste_generator = 1;
1051 break;
1052 case Compare_kind:
1053 VISIT(st, expr, e->v.Compare.left);
1054 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1055 break;
1056 case Call_kind:
1057 VISIT(st, expr, e->v.Call.func);
1058 VISIT_SEQ(st, expr, e->v.Call.args);
1059 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1060 if (e->v.Call.starargs)
1061 VISIT(st, expr, e->v.Call.starargs);
1062 if (e->v.Call.kwargs)
1063 VISIT(st, expr, e->v.Call.kwargs);
1064 break;
1065 case Repr_kind:
1066 VISIT(st, expr, e->v.Repr.value);
1067 break;
1068 case Num_kind:
1069 case Str_kind:
1070 /* Nothing to do here. */
1071 break;
1072 /* The following exprs can be assignment targets. */
1073 case Attribute_kind:
1074 VISIT(st, expr, e->v.Attribute.value);
1075 break;
1076 case Subscript_kind:
1077 VISIT(st, expr, e->v.Subscript.value);
1078 VISIT(st, slice, e->v.Subscript.slice);
1079 break;
1080 case Name_kind:
1081 if (!symtable_add_def(st, e->v.Name.id,
1082 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1083 return 0;
1084 break;
1085 /* child nodes of List and Tuple will have expr_context set */
1086 case List_kind:
1087 VISIT_SEQ(st, expr, e->v.List.elts);
1088 break;
1089 case Tuple_kind:
1090 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1091 break;
1092 }
1093 return 1;
1094}
1095
1096static int
1097symtable_implicit_arg(struct symtable *st, int pos)
1098{
1099 PyObject *id = PyString_FromFormat(".%d", pos);
1100 if (id == NULL)
1101 return 0;
1102 if (!symtable_add_def(st, id, DEF_PARAM)) {
1103 Py_DECREF(id);
1104 return 0;
1105 }
1106 Py_DECREF(id);
1107 return 1;
1108}
1109
1110static int
1111symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1112{
1113 int i, complex = 0;
1114
1115 /* go through all the toplevel arguments first */
1116 for (i = 0; i < asdl_seq_LEN(args); i++) {
1117 expr_ty arg = asdl_seq_GET(args, i);
1118 if (arg->kind == Name_kind) {
1119 assert(arg->v.Name.ctx == Param ||
1120 (arg->v.Name.ctx == Store && !toplevel));
1121 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1122 return 0;
1123 }
1124 else if (arg->kind == Tuple_kind) {
1125 assert(arg->v.Tuple.ctx == Store);
1126 complex = 1;
1127 if (toplevel) {
1128 if (!symtable_implicit_arg(st, i))
1129 return 0;
1130 }
1131 }
1132 else {
1133 /* syntax error */
1134 fprintf(stderr, "unexpected expr in parameter list\n");
1135 return 0;
1136 }
1137 }
1138
1139 if (!toplevel) {
1140 if (!symtable_visit_params_nested(st, args))
1141 return 0;
1142 }
1143
1144 return 1;
1145}
1146
1147static int
1148symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1149{
1150 int i;
1151 for (i = 0; i < asdl_seq_LEN(args); i++) {
1152 expr_ty arg = asdl_seq_GET(args, i);
1153 if (arg->kind == Tuple_kind &&
1154 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1155 return 0;
1156 }
1157
1158 return 1;
1159}
1160
1161static int
1162symtable_visit_arguments(struct symtable *st, arguments_ty a)
1163{
1164 /* skip default arguments inside function block
1165 XXX should ast be different?
1166 */
1167 if (a->args && !symtable_visit_params(st, a->args, 1))
1168 return 0;
1169 if (a->vararg) {
1170 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1171 return 0;
1172 st->st_cur->ste_varargs = 1;
1173 }
1174 if (a->kwarg) {
1175 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1176 return 0;
1177 st->st_cur->ste_varkeywords = 1;
1178 }
1179 if (a->args && !symtable_visit_params_nested(st, a->args))
1180 return 0;
1181 return 1;
1182}
1183
1184
1185static int
1186symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1187{
1188 if (eh->type)
1189 VISIT(st, expr, eh->type);
1190 if (eh->name)
1191 VISIT(st, expr, eh->name);
1192 VISIT_SEQ(st, stmt, eh->body);
1193 return 1;
1194}
1195
1196
1197static int
1198symtable_visit_alias(struct symtable *st, alias_ty a)
1199{
1200 /* Compute store_name, the name actually bound by the import
1201 operation. It is diferent than a->name when a->name is a
1202 dotted package name (e.g. spam.eggs)
1203 */
1204 PyObject *store_name;
1205 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1206 const char *base = PyString_AS_STRING(name);
1207 char *dot = strchr(base, '.');
1208 if (dot)
1209 store_name = PyString_FromStringAndSize(base, dot - base);
1210 else {
1211 store_name = name;
1212 Py_INCREF(store_name);
1213 }
1214 if (strcmp(PyString_AS_STRING(name), "*")) {
1215 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1216 Py_DECREF(store_name);
1217 return r;
1218 }
1219 else {
1220 if (st->st_cur->ste_type != ModuleBlock) {
1221 if (!symtable_warn(st,
1222 "import * only allowed at module level"))
1223 return 0;
1224 }
1225 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1226 return 1;
1227 }
1228}
1229
1230
1231static int
1232symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1233{
1234 VISIT(st, expr, lc->target);
1235 VISIT(st, expr, lc->iter);
1236 VISIT_SEQ(st, expr, lc->ifs);
1237 return 1;
1238}
1239
1240
1241static int
1242symtable_visit_keyword(struct symtable *st, keyword_ty k)
1243{
1244 VISIT(st, expr, k->value);
1245 return 1;
1246}
1247
1248
1249static int
1250symtable_visit_slice(struct symtable *st, slice_ty s)
1251{
1252 switch (s->kind) {
1253 case Slice_kind:
1254 if (s->v.Slice.lower)
1255 VISIT(st, expr, s->v.Slice.lower)
1256 if (s->v.Slice.upper)
1257 VISIT(st, expr, s->v.Slice.upper)
1258 if (s->v.Slice.step)
1259 VISIT(st, expr, s->v.Slice.step)
1260 break;
1261 case ExtSlice_kind:
1262 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1263 break;
1264 case Index_kind:
1265 VISIT(st, expr, s->v.Index.value)
1266 break;
1267 case Ellipsis_kind:
1268 break;
1269 }
1270 return 1;
1271}
1272
1273static int
1274symtable_visit_genexp(struct symtable *st, expr_ty e)
1275{
1276 identifier tmp;
1277 comprehension_ty outermost = ((comprehension_ty)
1278 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1279 /* Outermost iterator is evaluated in current scope */
1280 VISIT(st, expr, outermost->iter);
1281 /* Create generator scope for the rest */
1282 tmp = PyString_FromString("<genexpr>");
1283 if (!symtable_enter_block(st, tmp, FunctionBlock, (void *)e, 0)) {
1284 return 0;
1285 }
1286 st->st_cur->ste_generator = 1;
1287 /* Outermost iter is received as an argument */
1288 if (!symtable_implicit_arg(st, 0)) {
1289 return 0;
1290 }
1291 VISIT(st, expr, outermost->target);
1292 VISIT_SEQ(st, expr, outermost->ifs);
1293 VISIT_SEQ_TAIL(st, comprehension, e->v.GeneratorExp.generators, 1);
1294 VISIT(st, expr, e->v.GeneratorExp.elt);
1295 if (!symtable_exit_block(st, (void *)e))
1296 return 0;
1297 return 1;
1298}