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