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