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