Debian Codegraph @

cpython27/Python/symtable.c

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