proxy the __exit__ call
[python.git] / Python / symtable.c
blob23f0184304385a2ded7add49d899d8613ace5d70
1 #include "Python.h"
2 #include "Python-ast.h"
3 #include "code.h"
4 #include "symtable.h"
5 #include "structmember.h"
7 /* error strings used for warnings */
8 #define GLOBAL_AFTER_ASSIGN \
9 "name '%.400s' is assigned to before global declaration"
11 #define GLOBAL_AFTER_USE \
12 "name '%.400s' is used prior to global declaration"
14 #define IMPORT_STAR_WARNING "import * only allowed at module level"
16 #define RETURN_VAL_IN_GENERATOR \
17 "'return' with argument inside generator"
20 static PySTEntryObject *
21 ste_new(struct symtable *st, identifier name, _Py_block_ty block,
22 void *key, int lineno)
24 PySTEntryObject *ste = NULL;
25 PyObject *k;
27 k = PyLong_FromVoidPtr(key);
28 if (k == NULL)
29 goto fail;
30 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
31 if (ste == NULL)
32 goto fail;
33 ste->ste_table = st;
34 ste->ste_id = k;
36 ste->ste_name = name;
37 Py_INCREF(name);
39 ste->ste_symbols = NULL;
40 ste->ste_varnames = NULL;
41 ste->ste_children = NULL;
43 ste->ste_symbols = PyDict_New();
44 if (ste->ste_symbols == NULL)
45 goto fail;
47 ste->ste_varnames = PyList_New(0);
48 if (ste->ste_varnames == NULL)
49 goto fail;
51 ste->ste_children = PyList_New(0);
52 if (ste->ste_children == NULL)
53 goto fail;
55 ste->ste_type = block;
56 ste->ste_unoptimized = 0;
57 ste->ste_nested = 0;
58 ste->ste_free = 0;
59 ste->ste_varargs = 0;
60 ste->ste_varkeywords = 0;
61 ste->ste_opt_lineno = 0;
62 ste->ste_lineno = lineno;
64 if (st->st_cur != NULL &&
65 (st->st_cur->ste_nested ||
66 st->st_cur->ste_type == FunctionBlock))
67 ste->ste_nested = 1;
68 ste->ste_child_free = 0;
69 ste->ste_generator = 0;
70 ste->ste_returns_value = 0;
72 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
73 goto fail;
75 return ste;
76 fail:
77 Py_XDECREF(ste);
78 return NULL;
81 static PyObject *
82 ste_repr(PySTEntryObject *ste)
84 char buf[256];
86 PyOS_snprintf(buf, sizeof(buf),
87 "<symtable entry %.100s(%ld), line %d>",
88 PyString_AS_STRING(ste->ste_name),
89 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
90 return PyString_FromString(buf);
93 static void
94 ste_dealloc(PySTEntryObject *ste)
96 ste->ste_table = NULL;
97 Py_XDECREF(ste->ste_id);
98 Py_XDECREF(ste->ste_name);
99 Py_XDECREF(ste->ste_symbols);
100 Py_XDECREF(ste->ste_varnames);
101 Py_XDECREF(ste->ste_children);
102 PyObject_Del(ste);
105 #define OFF(x) offsetof(PySTEntryObject, x)
107 static PyMemberDef ste_memberlist[] = {
108 {"id", T_OBJECT, OFF(ste_id), READONLY},
109 {"name", T_OBJECT, OFF(ste_name), READONLY},
110 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
111 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
112 {"children", T_OBJECT, OFF(ste_children), READONLY},
113 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
114 {"nested", T_INT, OFF(ste_nested), READONLY},
115 {"type", T_INT, OFF(ste_type), READONLY},
116 {"lineno", T_INT, OFF(ste_lineno), READONLY},
117 {NULL}
120 PyTypeObject PySTEntry_Type = {
121 PyVarObject_HEAD_INIT(&PyType_Type, 0)
122 "symtable entry",
123 sizeof(PySTEntryObject),
125 (destructor)ste_dealloc, /* tp_dealloc */
126 0, /* tp_print */
127 0, /* tp_getattr */
128 0, /* tp_setattr */
129 0, /* tp_compare */
130 (reprfunc)ste_repr, /* tp_repr */
131 0, /* tp_as_number */
132 0, /* tp_as_sequence */
133 0, /* tp_as_mapping */
134 0, /* tp_hash */
135 0, /* tp_call */
136 0, /* tp_str */
137 PyObject_GenericGetAttr, /* tp_getattro */
138 0, /* tp_setattro */
139 0, /* tp_as_buffer */
140 Py_TPFLAGS_DEFAULT, /* tp_flags */
141 0, /* tp_doc */
142 0, /* tp_traverse */
143 0, /* tp_clear */
144 0, /* tp_richcompare */
145 0, /* tp_weaklistoffset */
146 0, /* tp_iter */
147 0, /* tp_iternext */
148 0, /* tp_methods */
149 ste_memberlist, /* tp_members */
150 0, /* tp_getset */
151 0, /* tp_base */
152 0, /* tp_dict */
153 0, /* tp_descr_get */
154 0, /* tp_descr_set */
155 0, /* tp_dictoffset */
156 0, /* tp_init */
157 0, /* tp_alloc */
158 0, /* tp_new */
161 static int symtable_analyze(struct symtable *st);
162 static int symtable_warn(struct symtable *st, char *msg, int lineno);
163 static int symtable_enter_block(struct symtable *st, identifier name,
164 _Py_block_ty block, void *ast, int lineno);
165 static int symtable_exit_block(struct symtable *st, void *ast);
166 static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
167 static int symtable_visit_expr(struct symtable *st, expr_ty s);
168 static int symtable_visit_genexp(struct symtable *st, expr_ty s);
169 static int symtable_visit_arguments(struct symtable *st, arguments_ty);
170 static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
171 static int symtable_visit_alias(struct symtable *st, alias_ty);
172 static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
173 static int symtable_visit_keyword(struct symtable *st, keyword_ty);
174 static int symtable_visit_slice(struct symtable *st, slice_ty);
175 static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
176 static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
177 static int symtable_implicit_arg(struct symtable *st, int pos);
180 static identifier top = NULL, lambda = NULL, genexpr = NULL;
182 #define GET_IDENTIFIER(VAR) \
183 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
185 #define DUPLICATE_ARGUMENT \
186 "duplicate argument '%s' in function definition"
188 static struct symtable *
189 symtable_new(void)
191 struct symtable *st;
193 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
194 if (st == NULL)
195 return NULL;
197 st->st_filename = NULL;
198 st->st_symbols = NULL;
200 if ((st->st_stack = PyList_New(0)) == NULL)
201 goto fail;
202 if ((st->st_symbols = PyDict_New()) == NULL)
203 goto fail;
204 st->st_cur = NULL;
205 st->st_private = NULL;
206 return st;
207 fail:
208 PySymtable_Free(st);
209 return NULL;
212 struct symtable *
213 PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
215 struct symtable *st = symtable_new();
216 asdl_seq *seq;
217 int i;
219 if (st == NULL)
220 return st;
221 st->st_filename = filename;
222 st->st_future = future;
223 if (!GET_IDENTIFIER(top) ||
224 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
225 PySymtable_Free(st);
226 return NULL;
229 st->st_top = st->st_cur;
230 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
231 /* Any other top-level initialization? */
232 switch (mod->kind) {
233 case Module_kind:
234 seq = mod->v.Module.body;
235 for (i = 0; i < asdl_seq_LEN(seq); i++)
236 if (!symtable_visit_stmt(st,
237 (stmt_ty)asdl_seq_GET(seq, i)))
238 goto error;
239 break;
240 case Expression_kind:
241 if (!symtable_visit_expr(st, mod->v.Expression.body))
242 goto error;
243 break;
244 case Interactive_kind:
245 seq = mod->v.Interactive.body;
246 for (i = 0; i < asdl_seq_LEN(seq); i++)
247 if (!symtable_visit_stmt(st,
248 (stmt_ty)asdl_seq_GET(seq, i)))
249 goto error;
250 break;
251 case Suite_kind:
252 PyErr_SetString(PyExc_RuntimeError,
253 "this compiler does not handle Suites");
254 goto error;
256 if (!symtable_exit_block(st, (void *)mod)) {
257 PySymtable_Free(st);
258 return NULL;
260 if (symtable_analyze(st))
261 return st;
262 PySymtable_Free(st);
263 return NULL;
264 error:
265 (void) symtable_exit_block(st, (void *)mod);
266 PySymtable_Free(st);
267 return NULL;
270 void
271 PySymtable_Free(struct symtable *st)
273 Py_XDECREF(st->st_symbols);
274 Py_XDECREF(st->st_stack);
275 PyMem_Free((void *)st);
278 PySTEntryObject *
279 PySymtable_Lookup(struct symtable *st, void *key)
281 PyObject *k, *v;
283 k = PyLong_FromVoidPtr(key);
284 if (k == NULL)
285 return NULL;
286 v = PyDict_GetItem(st->st_symbols, k);
287 if (v) {
288 assert(PySTEntry_Check(v));
289 Py_INCREF(v);
291 else {
292 PyErr_SetString(PyExc_KeyError,
293 "unknown symbol table entry");
296 Py_DECREF(k);
297 return (PySTEntryObject *)v;
300 int
301 PyST_GetScope(PySTEntryObject *ste, PyObject *name)
303 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
304 if (!v)
305 return 0;
306 assert(PyInt_Check(v));
307 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
311 /* Analyze raw symbol information to determine scope of each name.
313 The next several functions are helpers for PySymtable_Analyze(),
314 which determines whether a name is local, global, or free. In addition,
315 it determines which local variables are cell variables; they provide
316 bindings that are used for free variables in enclosed blocks.
318 There are also two kinds of free variables, implicit and explicit. An
319 explicit global is declared with the global statement. An implicit
320 global is a free variable for which the compiler has found no binding
321 in an enclosing function scope. The implicit global is either a global
322 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
323 to handle these names to implement slightly odd semantics. In such a
324 block, the name is treated as global until it is assigned to; then it
325 is treated as a local.
327 The symbol table requires two passes to determine the scope of each name.
328 The first pass collects raw facts from the AST: the name is a parameter
329 here, the name is used by not defined here, etc. The second pass analyzes
330 these facts during a pass over the PySTEntryObjects created during pass 1.
332 When a function is entered during the second pass, the parent passes
333 the set of all name bindings visible to its children. These bindings
334 are used to determine if the variable is free or an implicit global.
335 After doing the local analysis, it analyzes each of its child blocks
336 using an updated set of name bindings.
338 The children update the free variable set. If a local variable is free
339 in a child, the variable is marked as a cell. The current function must
340 provide runtime storage for the variable that may outlive the function's
341 frame. Cell variables are removed from the free set before the analyze
342 function returns to its parent.
344 The sets of bound and free variables are implemented as dictionaries
345 mapping strings to None.
348 #define SET_SCOPE(DICT, NAME, I) { \
349 PyObject *o = PyInt_FromLong(I); \
350 if (!o) \
351 return 0; \
352 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
353 Py_DECREF(o); \
354 return 0; \
356 Py_DECREF(o); \
359 /* Decide on scope of name, given flags.
361 The namespace dictionaries may be modified to record information
362 about the new name. For example, a new global will add an entry to
363 global. A name that was global can be changed to local.
366 static int
367 analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
368 PyObject *bound, PyObject *local, PyObject *free,
369 PyObject *global)
371 if (flags & DEF_GLOBAL) {
372 if (flags & DEF_PARAM) {
373 PyErr_Format(PyExc_SyntaxError,
374 "name '%s' is local and global",
375 PyString_AS_STRING(name));
376 PyErr_SyntaxLocation(ste->ste_table->st_filename,
377 ste->ste_lineno);
379 return 0;
381 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
382 if (PyDict_SetItem(global, name, Py_None) < 0)
383 return 0;
384 if (bound && PyDict_GetItem(bound, name)) {
385 if (PyDict_DelItem(bound, name) < 0)
386 return 0;
388 return 1;
390 if (flags & DEF_BOUND) {
391 SET_SCOPE(dict, name, LOCAL);
392 if (PyDict_SetItem(local, name, Py_None) < 0)
393 return 0;
394 if (PyDict_GetItem(global, name)) {
395 if (PyDict_DelItem(global, name) < 0)
396 return 0;
398 return 1;
400 /* If an enclosing block has a binding for this name, it
401 is a free variable rather than a global variable.
402 Note that having a non-NULL bound implies that the block
403 is nested.
405 if (bound && PyDict_GetItem(bound, name)) {
406 SET_SCOPE(dict, name, FREE);
407 ste->ste_free = 1;
408 if (PyDict_SetItem(free, name, Py_None) < 0)
409 return 0;
410 return 1;
412 /* If a parent has a global statement, then call it global
413 explicit? It could also be global implicit.
415 else if (global && PyDict_GetItem(global, name)) {
416 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
417 return 1;
419 else {
420 if (ste->ste_nested)
421 ste->ste_free = 1;
422 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
423 return 1;
425 /* Should never get here. */
426 PyErr_Format(PyExc_SystemError, "failed to set scope for %s",
427 PyString_AS_STRING(name));
428 return 0;
431 #undef SET_SCOPE
433 /* If a name is defined in free and also in locals, then this block
434 provides the binding for the free variable. The name should be
435 marked CELL in this block and removed from the free list.
437 Note that the current block's free variables are included in free.
438 That's safe because no name can be free and local in the same scope.
441 static int
442 analyze_cells(PyObject *scope, PyObject *free)
444 PyObject *name, *v, *w;
445 int success = 0;
446 Py_ssize_t pos = 0;
448 w = PyInt_FromLong(CELL);
449 if (!w)
450 return 0;
451 while (PyDict_Next(scope, &pos, &name, &v)) {
452 long flags;
453 assert(PyInt_Check(v));
454 flags = PyInt_AS_LONG(v);
455 if (flags != LOCAL)
456 continue;
457 if (!PyDict_GetItem(free, name))
458 continue;
459 /* Replace LOCAL with CELL for this name, and remove
460 from free. It is safe to replace the value of name
461 in the dict, because it will not cause a resize.
463 if (PyDict_SetItem(scope, name, w) < 0)
464 goto error;
465 if (!PyDict_DelItem(free, name) < 0)
466 goto error;
468 success = 1;
469 error:
470 Py_DECREF(w);
471 return success;
474 /* Check for illegal statements in unoptimized namespaces */
475 static int
476 check_unoptimized(const PySTEntryObject* ste) {
477 char buf[300];
478 const char* trailer;
480 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
481 || !(ste->ste_free || ste->ste_child_free))
482 return 1;
484 trailer = (ste->ste_child_free ?
485 "contains a nested function with free variables" :
486 "is a nested function");
488 switch (ste->ste_unoptimized) {
489 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
490 case OPT_EXEC: /* qualified exec is fine */
491 return 1;
492 case OPT_IMPORT_STAR:
493 PyOS_snprintf(buf, sizeof(buf),
494 "import * is not allowed in function '%.100s' "
495 "because it %s",
496 PyString_AS_STRING(ste->ste_name), trailer);
497 break;
498 case OPT_BARE_EXEC:
499 PyOS_snprintf(buf, sizeof(buf),
500 "unqualified exec is not allowed in function "
501 "'%.100s' it %s",
502 PyString_AS_STRING(ste->ste_name), trailer);
503 break;
504 default:
505 PyOS_snprintf(buf, sizeof(buf),
506 "function '%.100s' uses import * and bare exec, "
507 "which are illegal because it %s",
508 PyString_AS_STRING(ste->ste_name), trailer);
509 break;
512 PyErr_SetString(PyExc_SyntaxError, buf);
513 PyErr_SyntaxLocation(ste->ste_table->st_filename,
514 ste->ste_opt_lineno);
515 return 0;
518 /* Enter the final scope information into the st_symbols dict.
520 * All arguments are dicts. Modifies symbols, others are read-only.
522 static int
523 update_symbols(PyObject *symbols, PyObject *scope,
524 PyObject *bound, PyObject *free, int classflag)
526 PyObject *name, *v, *u, *w, *free_value = NULL;
527 Py_ssize_t pos = 0;
529 while (PyDict_Next(symbols, &pos, &name, &v)) {
530 long i, flags;
531 assert(PyInt_Check(v));
532 flags = PyInt_AS_LONG(v);
533 w = PyDict_GetItem(scope, name);
534 assert(w && PyInt_Check(w));
535 i = PyInt_AS_LONG(w);
536 flags |= (i << SCOPE_OFF);
537 u = PyInt_FromLong(flags);
538 if (!u)
539 return 0;
540 if (PyDict_SetItem(symbols, name, u) < 0) {
541 Py_DECREF(u);
542 return 0;
544 Py_DECREF(u);
547 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
548 if (!free_value)
549 return 0;
551 /* add a free variable when it's only use is for creating a closure */
552 pos = 0;
553 while (PyDict_Next(free, &pos, &name, &v)) {
554 PyObject *o = PyDict_GetItem(symbols, name);
556 if (o) {
557 /* It could be a free variable in a method of
558 the class that has the same name as a local
559 or global in the class scope.
561 if (classflag &&
562 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
563 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
564 o = PyInt_FromLong(i);
565 if (!o) {
566 Py_DECREF(free_value);
567 return 0;
569 if (PyDict_SetItem(symbols, name, o) < 0) {
570 Py_DECREF(o);
571 Py_DECREF(free_value);
572 return 0;
574 Py_DECREF(o);
576 /* else it's not free, probably a cell */
577 continue;
579 if (!PyDict_GetItem(bound, name))
580 continue; /* it's a global */
582 if (PyDict_SetItem(symbols, name, free_value) < 0) {
583 Py_DECREF(free_value);
584 return 0;
587 Py_DECREF(free_value);
588 return 1;
591 /* Make final symbol table decisions for block of ste.
593 Arguments:
594 ste -- current symtable entry (input/output)
595 bound -- set of variables bound in enclosing scopes (input). bound
596 is NULL for module blocks.
597 free -- set of free variables in enclosed scopes (output)
598 globals -- set of declared global variables in enclosing scopes (input)
600 The implementation uses two mutually recursive functions,
601 analyze_block() and analyze_child_block(). analyze_block() is
602 responsible for analyzing the individual names defined in a block.
603 analyze_child_block() prepares temporary namespace dictionaries
604 used to evaluated nested blocks.
606 The two functions exist because a child block should see the name
607 bindings of its enclosing blocks, but those bindings should not
608 propagate back to a parent block.
611 static int
612 analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
613 PyObject *global, PyObject* child_free);
615 static int
616 analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
617 PyObject *global)
619 PyObject *name, *v, *local = NULL, *scope = NULL;
620 PyObject *newbound = NULL, *newglobal = NULL;
621 PyObject *newfree = NULL, *allfree = NULL;
622 int i, success = 0;
623 Py_ssize_t pos = 0;
625 local = PyDict_New(); /* collect new names bound in block */
626 if (!local)
627 goto error;
628 scope = PyDict_New(); /* collect scopes defined for each name */
629 if (!scope)
630 goto error;
632 /* Allocate new global and bound variable dictionaries. These
633 dictionaries hold the names visible in nested blocks. For
634 ClassBlocks, the bound and global names are initialized
635 before analyzing names, because class bindings aren't
636 visible in methods. For other blocks, they are initialized
637 after names are analyzed.
640 /* TODO(jhylton): Package these dicts in a struct so that we
641 can write reasonable helper functions?
643 newglobal = PyDict_New();
644 if (!newglobal)
645 goto error;
646 newbound = PyDict_New();
647 if (!newbound)
648 goto error;
649 newfree = PyDict_New();
650 if (!newfree)
651 goto error;
653 if (ste->ste_type == ClassBlock) {
654 if (PyDict_Update(newglobal, global) < 0)
655 goto error;
656 if (bound)
657 if (PyDict_Update(newbound, bound) < 0)
658 goto error;
661 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
662 long flags = PyInt_AS_LONG(v);
663 if (!analyze_name(ste, scope, name, flags,
664 bound, local, free, global))
665 goto error;
668 if (ste->ste_type != ClassBlock) {
669 if (ste->ste_type == FunctionBlock) {
670 if (PyDict_Update(newbound, local) < 0)
671 goto error;
673 if (bound) {
674 if (PyDict_Update(newbound, bound) < 0)
675 goto error;
677 if (PyDict_Update(newglobal, global) < 0)
678 goto error;
681 /* Recursively call analyze_block() on each child block.
683 newbound, newglobal now contain the names visible in
684 nested blocks. The free variables in the children will
685 be collected in allfree.
687 allfree = PyDict_New();
688 if (!allfree)
689 goto error;
690 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
691 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
692 PySTEntryObject* entry;
693 assert(c && PySTEntry_Check(c));
694 entry = (PySTEntryObject*)c;
695 if (!analyze_child_block(entry, newbound, newfree, newglobal,
696 allfree))
697 goto error;
698 if (entry->ste_free || entry->ste_child_free)
699 ste->ste_child_free = 1;
702 if (PyDict_Update(newfree, allfree) < 0)
703 goto error;
704 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
705 goto error;
706 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
707 ste->ste_type == ClassBlock))
708 goto error;
709 if (!check_unoptimized(ste))
710 goto error;
712 if (PyDict_Update(free, newfree) < 0)
713 goto error;
714 success = 1;
715 error:
716 Py_XDECREF(local);
717 Py_XDECREF(scope);
718 Py_XDECREF(newbound);
719 Py_XDECREF(newglobal);
720 Py_XDECREF(newfree);
721 Py_XDECREF(allfree);
722 if (!success)
723 assert(PyErr_Occurred());
724 return success;
727 static int
728 analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
729 PyObject *global, PyObject* child_free)
731 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
733 /* Copy the bound and global dictionaries.
735 These dictionary are used by all blocks enclosed by the
736 current block. The analyze_block() call modifies these
737 dictionaries.
740 temp_bound = PyDict_New();
741 if (!temp_bound)
742 goto error;
743 if (PyDict_Update(temp_bound, bound) < 0)
744 goto error;
745 temp_free = PyDict_New();
746 if (!temp_free)
747 goto error;
748 if (PyDict_Update(temp_free, free) < 0)
749 goto error;
750 temp_global = PyDict_New();
751 if (!temp_global)
752 goto error;
753 if (PyDict_Update(temp_global, global) < 0)
754 goto error;
756 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
757 goto error;
758 if (PyDict_Update(child_free, temp_free) < 0)
759 goto error;
760 Py_DECREF(temp_bound);
761 Py_DECREF(temp_free);
762 Py_DECREF(temp_global);
763 return 1;
764 error:
765 Py_XDECREF(temp_bound);
766 Py_XDECREF(temp_free);
767 Py_XDECREF(temp_global);
768 return 0;
771 static int
772 symtable_analyze(struct symtable *st)
774 PyObject *free, *global;
775 int r;
777 free = PyDict_New();
778 if (!free)
779 return 0;
780 global = PyDict_New();
781 if (!global) {
782 Py_DECREF(free);
783 return 0;
785 r = analyze_block(st->st_top, NULL, free, global);
786 Py_DECREF(free);
787 Py_DECREF(global);
788 return r;
792 static int
793 symtable_warn(struct symtable *st, char *msg, int lineno)
795 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
796 lineno, NULL, NULL) < 0) {
797 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
798 PyErr_SetString(PyExc_SyntaxError, msg);
799 PyErr_SyntaxLocation(st->st_filename,
800 st->st_cur->ste_lineno);
802 return 0;
804 return 1;
807 /* symtable_enter_block() gets a reference via ste_new.
808 This reference is released when the block is exited, via the DECREF
809 in symtable_exit_block().
812 static int
813 symtable_exit_block(struct symtable *st, void *ast)
815 Py_ssize_t end;
817 Py_CLEAR(st->st_cur);
818 end = PyList_GET_SIZE(st->st_stack) - 1;
819 if (end >= 0) {
820 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
821 end);
822 if (st->st_cur == NULL)
823 return 0;
824 Py_INCREF(st->st_cur);
825 if (PySequence_DelItem(st->st_stack, end) < 0)
826 return 0;
828 return 1;
831 static int
832 symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
833 void *ast, int lineno)
835 PySTEntryObject *prev = NULL;
837 if (st->st_cur) {
838 prev = st->st_cur;
839 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
840 return 0;
842 Py_DECREF(st->st_cur);
844 st->st_cur = ste_new(st, name, block, ast, lineno);
845 if (st->st_cur == NULL)
846 return 0;
847 if (name == GET_IDENTIFIER(top))
848 st->st_global = st->st_cur->ste_symbols;
849 if (prev) {
850 if (PyList_Append(prev->ste_children,
851 (PyObject *)st->st_cur) < 0) {
852 return 0;
855 return 1;
858 static long
859 symtable_lookup(struct symtable *st, PyObject *name)
861 PyObject *o;
862 PyObject *mangled = _Py_Mangle(st->st_private, name);
863 if (!mangled)
864 return 0;
865 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
866 Py_DECREF(mangled);
867 if (!o)
868 return 0;
869 return PyInt_AsLong(o);
872 static int
873 symtable_add_def(struct symtable *st, PyObject *name, int flag)
875 PyObject *o;
876 PyObject *dict;
877 long val;
878 PyObject *mangled = _Py_Mangle(st->st_private, name);
880 if (!mangled)
881 return 0;
882 dict = st->st_cur->ste_symbols;
883 if ((o = PyDict_GetItem(dict, mangled))) {
884 val = PyInt_AS_LONG(o);
885 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
886 /* Is it better to use 'mangled' or 'name' here? */
887 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
888 PyString_AsString(name));
889 PyErr_SyntaxLocation(st->st_filename,
890 st->st_cur->ste_lineno);
891 goto error;
893 val |= flag;
894 } else
895 val = flag;
896 o = PyInt_FromLong(val);
897 if (o == NULL)
898 goto error;
899 if (PyDict_SetItem(dict, mangled, o) < 0) {
900 Py_DECREF(o);
901 goto error;
903 Py_DECREF(o);
905 if (flag & DEF_PARAM) {
906 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
907 goto error;
908 } else if (flag & DEF_GLOBAL) {
909 /* XXX need to update DEF_GLOBAL for other flags too;
910 perhaps only DEF_FREE_GLOBAL */
911 val = flag;
912 if ((o = PyDict_GetItem(st->st_global, mangled))) {
913 val |= PyInt_AS_LONG(o);
915 o = PyInt_FromLong(val);
916 if (o == NULL)
917 goto error;
918 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
919 Py_DECREF(o);
920 goto error;
922 Py_DECREF(o);
924 Py_DECREF(mangled);
925 return 1;
927 error:
928 Py_DECREF(mangled);
929 return 0;
932 /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
933 They use the ASDL name to synthesize the name of the C type and the visit
934 function.
936 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
937 useful if the first node in the sequence requires special treatment.
940 #define VISIT(ST, TYPE, V) \
941 if (!symtable_visit_ ## TYPE((ST), (V))) \
942 return 0;
944 #define VISIT_IN_BLOCK(ST, TYPE, V, S) \
945 if (!symtable_visit_ ## TYPE((ST), (V))) { \
946 symtable_exit_block((ST), (S)); \
947 return 0; \
950 #define VISIT_SEQ(ST, TYPE, SEQ) { \
951 int i; \
952 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
953 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
954 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
955 if (!symtable_visit_ ## TYPE((ST), elt)) \
956 return 0; \
960 #define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
961 int i; \
962 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
963 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
964 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
965 if (!symtable_visit_ ## TYPE((ST), elt)) { \
966 symtable_exit_block((ST), (S)); \
967 return 0; \
972 #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
973 int i; \
974 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
975 for (i = (START); 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 return 0; \
982 #define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
983 int i; \
984 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
985 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
986 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
987 if (!symtable_visit_ ## TYPE((ST), elt)) { \
988 symtable_exit_block((ST), (S)); \
989 return 0; \
994 static int
995 symtable_visit_stmt(struct symtable *st, stmt_ty s)
997 switch (s->kind) {
998 case FunctionDef_kind:
999 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1000 return 0;
1001 if (s->v.FunctionDef.args->defaults)
1002 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1003 if (s->v.FunctionDef.decorator_list)
1004 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1005 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1006 FunctionBlock, (void *)s, s->lineno))
1007 return 0;
1008 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1009 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1010 if (!symtable_exit_block(st, s))
1011 return 0;
1012 break;
1013 case ClassDef_kind: {
1014 PyObject *tmp;
1015 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1016 return 0;
1017 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1018 if (s->v.ClassDef.decorator_list)
1019 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1020 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1021 (void *)s, s->lineno))
1022 return 0;
1023 tmp = st->st_private;
1024 st->st_private = s->v.ClassDef.name;
1025 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1026 st->st_private = tmp;
1027 if (!symtable_exit_block(st, s))
1028 return 0;
1029 break;
1031 case Return_kind:
1032 if (s->v.Return.value) {
1033 VISIT(st, expr, s->v.Return.value);
1034 st->st_cur->ste_returns_value = 1;
1035 if (st->st_cur->ste_generator) {
1036 PyErr_SetString(PyExc_SyntaxError,
1037 RETURN_VAL_IN_GENERATOR);
1038 PyErr_SyntaxLocation(st->st_filename,
1039 s->lineno);
1040 return 0;
1043 break;
1044 case Delete_kind:
1045 VISIT_SEQ(st, expr, s->v.Delete.targets);
1046 break;
1047 case Assign_kind:
1048 VISIT_SEQ(st, expr, s->v.Assign.targets);
1049 VISIT(st, expr, s->v.Assign.value);
1050 break;
1051 case AugAssign_kind:
1052 VISIT(st, expr, s->v.AugAssign.target);
1053 VISIT(st, expr, s->v.AugAssign.value);
1054 break;
1055 case Print_kind:
1056 if (s->v.Print.dest)
1057 VISIT(st, expr, s->v.Print.dest);
1058 VISIT_SEQ(st, expr, s->v.Print.values);
1059 break;
1060 case For_kind:
1061 VISIT(st, expr, s->v.For.target);
1062 VISIT(st, expr, s->v.For.iter);
1063 VISIT_SEQ(st, stmt, s->v.For.body);
1064 if (s->v.For.orelse)
1065 VISIT_SEQ(st, stmt, s->v.For.orelse);
1066 break;
1067 case While_kind:
1068 VISIT(st, expr, s->v.While.test);
1069 VISIT_SEQ(st, stmt, s->v.While.body);
1070 if (s->v.While.orelse)
1071 VISIT_SEQ(st, stmt, s->v.While.orelse);
1072 break;
1073 case If_kind:
1074 /* XXX if 0: and lookup_yield() hacks */
1075 VISIT(st, expr, s->v.If.test);
1076 VISIT_SEQ(st, stmt, s->v.If.body);
1077 if (s->v.If.orelse)
1078 VISIT_SEQ(st, stmt, s->v.If.orelse);
1079 break;
1080 case Raise_kind:
1081 if (s->v.Raise.type) {
1082 VISIT(st, expr, s->v.Raise.type);
1083 if (s->v.Raise.inst) {
1084 VISIT(st, expr, s->v.Raise.inst);
1085 if (s->v.Raise.tback)
1086 VISIT(st, expr, s->v.Raise.tback);
1089 break;
1090 case TryExcept_kind:
1091 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1092 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1093 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1094 break;
1095 case TryFinally_kind:
1096 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1097 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1098 break;
1099 case Assert_kind:
1100 VISIT(st, expr, s->v.Assert.test);
1101 if (s->v.Assert.msg)
1102 VISIT(st, expr, s->v.Assert.msg);
1103 break;
1104 case Import_kind:
1105 VISIT_SEQ(st, alias, s->v.Import.names);
1106 /* XXX Don't have the lineno available inside
1107 visit_alias */
1108 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1109 st->st_cur->ste_opt_lineno = s->lineno;
1110 break;
1111 case ImportFrom_kind:
1112 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1113 /* XXX Don't have the lineno available inside
1114 visit_alias */
1115 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1116 st->st_cur->ste_opt_lineno = s->lineno;
1117 break;
1118 case Exec_kind:
1119 VISIT(st, expr, s->v.Exec.body);
1120 if (!st->st_cur->ste_opt_lineno)
1121 st->st_cur->ste_opt_lineno = s->lineno;
1122 if (s->v.Exec.globals) {
1123 st->st_cur->ste_unoptimized |= OPT_EXEC;
1124 VISIT(st, expr, s->v.Exec.globals);
1125 if (s->v.Exec.locals)
1126 VISIT(st, expr, s->v.Exec.locals);
1127 } else {
1128 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1130 break;
1131 case Global_kind: {
1132 int i;
1133 asdl_seq *seq = s->v.Global.names;
1134 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1135 identifier name = (identifier)asdl_seq_GET(seq, i);
1136 char *c_name = PyString_AS_STRING(name);
1137 long cur = symtable_lookup(st, name);
1138 if (cur < 0)
1139 return 0;
1140 if (cur & (DEF_LOCAL | USE)) {
1141 char buf[256];
1142 if (cur & DEF_LOCAL)
1143 PyOS_snprintf(buf, sizeof(buf),
1144 GLOBAL_AFTER_ASSIGN,
1145 c_name);
1146 else
1147 PyOS_snprintf(buf, sizeof(buf),
1148 GLOBAL_AFTER_USE,
1149 c_name);
1150 if (!symtable_warn(st, buf, s->lineno))
1151 return 0;
1153 if (!symtable_add_def(st, name, DEF_GLOBAL))
1154 return 0;
1156 break;
1158 case Expr_kind:
1159 VISIT(st, expr, s->v.Expr.value);
1160 break;
1161 case Pass_kind:
1162 case Break_kind:
1163 case Continue_kind:
1164 /* nothing to do here */
1165 break;
1166 case With_kind:
1167 VISIT(st, expr, s->v.With.context_expr);
1168 if (s->v.With.optional_vars) {
1169 VISIT(st, expr, s->v.With.optional_vars);
1171 VISIT_SEQ(st, stmt, s->v.With.body);
1172 break;
1174 return 1;
1177 static int
1178 symtable_visit_expr(struct symtable *st, expr_ty e)
1180 switch (e->kind) {
1181 case BoolOp_kind:
1182 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1183 break;
1184 case BinOp_kind:
1185 VISIT(st, expr, e->v.BinOp.left);
1186 VISIT(st, expr, e->v.BinOp.right);
1187 break;
1188 case UnaryOp_kind:
1189 VISIT(st, expr, e->v.UnaryOp.operand);
1190 break;
1191 case Lambda_kind: {
1192 if (!GET_IDENTIFIER(lambda))
1193 return 0;
1194 if (e->v.Lambda.args->defaults)
1195 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1196 /* XXX how to get line numbers for expressions */
1197 if (!symtable_enter_block(st, lambda,
1198 FunctionBlock, (void *)e, 0))
1199 return 0;
1200 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1201 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1202 if (!symtable_exit_block(st, (void *)e))
1203 return 0;
1204 break;
1206 case IfExp_kind:
1207 VISIT(st, expr, e->v.IfExp.test);
1208 VISIT(st, expr, e->v.IfExp.body);
1209 VISIT(st, expr, e->v.IfExp.orelse);
1210 break;
1211 case Dict_kind:
1212 VISIT_SEQ(st, expr, e->v.Dict.keys);
1213 VISIT_SEQ(st, expr, e->v.Dict.values);
1214 break;
1215 case ListComp_kind:
1216 VISIT(st, expr, e->v.ListComp.elt);
1217 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1218 break;
1219 case GeneratorExp_kind:
1220 if (!symtable_visit_genexp(st, e))
1221 return 0;
1222 break;
1223 case Yield_kind:
1224 if (e->v.Yield.value)
1225 VISIT(st, expr, e->v.Yield.value);
1226 st->st_cur->ste_generator = 1;
1227 if (st->st_cur->ste_returns_value) {
1228 PyErr_SetString(PyExc_SyntaxError,
1229 RETURN_VAL_IN_GENERATOR);
1230 PyErr_SyntaxLocation(st->st_filename,
1231 e->lineno);
1232 return 0;
1234 break;
1235 case Compare_kind:
1236 VISIT(st, expr, e->v.Compare.left);
1237 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1238 break;
1239 case Call_kind:
1240 VISIT(st, expr, e->v.Call.func);
1241 VISIT_SEQ(st, expr, e->v.Call.args);
1242 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1243 if (e->v.Call.starargs)
1244 VISIT(st, expr, e->v.Call.starargs);
1245 if (e->v.Call.kwargs)
1246 VISIT(st, expr, e->v.Call.kwargs);
1247 break;
1248 case Repr_kind:
1249 VISIT(st, expr, e->v.Repr.value);
1250 break;
1251 case Num_kind:
1252 case Str_kind:
1253 /* Nothing to do here. */
1254 break;
1255 /* The following exprs can be assignment targets. */
1256 case Attribute_kind:
1257 VISIT(st, expr, e->v.Attribute.value);
1258 break;
1259 case Subscript_kind:
1260 VISIT(st, expr, e->v.Subscript.value);
1261 VISIT(st, slice, e->v.Subscript.slice);
1262 break;
1263 case Name_kind:
1264 if (!symtable_add_def(st, e->v.Name.id,
1265 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1266 return 0;
1267 break;
1268 /* child nodes of List and Tuple will have expr_context set */
1269 case List_kind:
1270 VISIT_SEQ(st, expr, e->v.List.elts);
1271 break;
1272 case Tuple_kind:
1273 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1274 break;
1276 return 1;
1279 static int
1280 symtable_implicit_arg(struct symtable *st, int pos)
1282 PyObject *id = PyString_FromFormat(".%d", pos);
1283 if (id == NULL)
1284 return 0;
1285 if (!symtable_add_def(st, id, DEF_PARAM)) {
1286 Py_DECREF(id);
1287 return 0;
1289 Py_DECREF(id);
1290 return 1;
1293 static int
1294 symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1296 int i;
1298 /* go through all the toplevel arguments first */
1299 for (i = 0; i < asdl_seq_LEN(args); i++) {
1300 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1301 if (arg->kind == Name_kind) {
1302 assert(arg->v.Name.ctx == Param ||
1303 (arg->v.Name.ctx == Store && !toplevel));
1304 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1305 return 0;
1307 else if (arg->kind == Tuple_kind) {
1308 assert(arg->v.Tuple.ctx == Store);
1309 if (toplevel) {
1310 if (!symtable_implicit_arg(st, i))
1311 return 0;
1314 else {
1315 PyErr_SetString(PyExc_SyntaxError,
1316 "invalid expression in parameter list");
1317 PyErr_SyntaxLocation(st->st_filename,
1318 st->st_cur->ste_lineno);
1319 return 0;
1323 if (!toplevel) {
1324 if (!symtable_visit_params_nested(st, args))
1325 return 0;
1328 return 1;
1331 static int
1332 symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1334 int i;
1335 for (i = 0; i < asdl_seq_LEN(args); i++) {
1336 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1337 if (arg->kind == Tuple_kind &&
1338 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1339 return 0;
1342 return 1;
1345 static int
1346 symtable_visit_arguments(struct symtable *st, arguments_ty a)
1348 /* skip default arguments inside function block
1349 XXX should ast be different?
1351 if (a->args && !symtable_visit_params(st, a->args, 1))
1352 return 0;
1353 if (a->vararg) {
1354 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1355 return 0;
1356 st->st_cur->ste_varargs = 1;
1358 if (a->kwarg) {
1359 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1360 return 0;
1361 st->st_cur->ste_varkeywords = 1;
1363 if (a->args && !symtable_visit_params_nested(st, a->args))
1364 return 0;
1365 return 1;
1369 static int
1370 symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1372 if (eh->v.ExceptHandler.type)
1373 VISIT(st, expr, eh->v.ExceptHandler.type);
1374 if (eh->v.ExceptHandler.name)
1375 VISIT(st, expr, eh->v.ExceptHandler.name);
1376 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1377 return 1;
1381 static int
1382 symtable_visit_alias(struct symtable *st, alias_ty a)
1384 /* Compute store_name, the name actually bound by the import
1385 operation. It is diferent than a->name when a->name is a
1386 dotted package name (e.g. spam.eggs)
1388 PyObject *store_name;
1389 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1390 const char *base = PyString_AS_STRING(name);
1391 char *dot = strchr(base, '.');
1392 if (dot) {
1393 store_name = PyString_FromStringAndSize(base, dot - base);
1394 if (!store_name)
1395 return 0;
1397 else {
1398 store_name = name;
1399 Py_INCREF(store_name);
1401 if (strcmp(PyString_AS_STRING(name), "*")) {
1402 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1403 Py_DECREF(store_name);
1404 return r;
1406 else {
1407 if (st->st_cur->ste_type != ModuleBlock) {
1408 int lineno = st->st_cur->ste_lineno;
1409 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
1410 Py_DECREF(store_name);
1411 return 0;
1414 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1415 Py_DECREF(store_name);
1416 return 1;
1421 static int
1422 symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1424 VISIT(st, expr, lc->target);
1425 VISIT(st, expr, lc->iter);
1426 VISIT_SEQ(st, expr, lc->ifs);
1427 return 1;
1431 static int
1432 symtable_visit_keyword(struct symtable *st, keyword_ty k)
1434 VISIT(st, expr, k->value);
1435 return 1;
1439 static int
1440 symtable_visit_slice(struct symtable *st, slice_ty s)
1442 switch (s->kind) {
1443 case Slice_kind:
1444 if (s->v.Slice.lower)
1445 VISIT(st, expr, s->v.Slice.lower)
1446 if (s->v.Slice.upper)
1447 VISIT(st, expr, s->v.Slice.upper)
1448 if (s->v.Slice.step)
1449 VISIT(st, expr, s->v.Slice.step)
1450 break;
1451 case ExtSlice_kind:
1452 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1453 break;
1454 case Index_kind:
1455 VISIT(st, expr, s->v.Index.value)
1456 break;
1457 case Ellipsis_kind:
1458 break;
1460 return 1;
1463 static int
1464 symtable_visit_genexp(struct symtable *st, expr_ty e)
1466 comprehension_ty outermost = ((comprehension_ty)
1467 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1468 /* Outermost iterator is evaluated in current scope */
1469 VISIT(st, expr, outermost->iter);
1470 /* Create generator scope for the rest */
1471 if (!GET_IDENTIFIER(genexpr) ||
1472 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
1473 return 0;
1475 st->st_cur->ste_generator = 1;
1476 /* Outermost iter is received as an argument */
1477 if (!symtable_implicit_arg(st, 0)) {
1478 symtable_exit_block(st, (void *)e);
1479 return 0;
1481 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1482 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1483 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1484 e->v.GeneratorExp.generators, 1, (void*)e);
1485 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
1486 return symtable_exit_block(st, (void *)e);