blob: e9c939158017e0d8abad5545942fdc0c08db8bfa [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
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#define GLOBAL_AFTER_ASSIGN \
9"name '%.400s' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000010
Jeremy Hylton81e95022007-02-27 06:50:52 +000011#define NONLOCAL_AFTER_ASSIGN \
12"name '%.400s' is assigned to before nonlocal declaration"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_USE \
15"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_USE \
18"name '%.400s' is used prior to nonlocal declaration"
19
Neal Norwitz5d0ad502005-12-19 04:27:42 +000020#define IMPORT_STAR_WARNING "import * only allowed at module level"
21
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000022#define RETURN_VAL_IN_GENERATOR \
23 "'return' with argument inside generator"
Neal Norwitz5d0ad502005-12-19 04:27:42 +000024
Neal Norwitz090b3dd2006-02-28 22:36:46 +000025/* XXX(nnorwitz): change name since static? */
26static PySTEntryObject *
Neal Norwitz62c2fac2005-10-24 00:30:44 +000027PySTEntry_New(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000029{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000030 PySTEntryObject *ste = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000031 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000033 k = PyLong_FromVoidPtr(key);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000034 if (k == NULL)
35 goto fail;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036 ste = (PySTEntryObject *)PyObject_New(PySTEntryObject,
37 &PySTEntry_Type);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000038 ste->ste_table = st;
39 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042 ste->ste_name = name;
43 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000045 ste->ste_symbols = NULL;
46 ste->ste_varnames = NULL;
47 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000048
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000049 ste->ste_symbols = PyDict_New();
50 if (ste->ste_symbols == NULL)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000051 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000052
53 ste->ste_varnames = PyList_New(0);
54 if (ste->ste_varnames == NULL)
55 goto fail;
56
57 ste->ste_children = PyList_New(0);
58 if (ste->ste_children == NULL)
59 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000060
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000061 ste->ste_type = block;
62 ste->ste_unoptimized = 0;
63 ste->ste_nested = 0;
64 ste->ste_free = 0;
65 ste->ste_varargs = 0;
66 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000067 ste->ste_opt_lineno = 0;
Jeremy Hylton4d508ad2003-05-21 17:34:50 +000068 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000071 if (st->st_cur != NULL &&
72 (st->st_cur->ste_nested ||
73 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000074 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000075 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000076 ste->ste_generator = 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000077 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000078
79 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
80 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083 fail:
84 Py_XDECREF(ste);
85 return NULL;
86}
87
88static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000090{
91 char buf[256];
92
Barry Warsaw4b4ab202001-11-28 21:36:28 +000093 PyOS_snprintf(buf, sizeof(buf),
94 "<symtable entry %.100s(%ld), line %d>",
95 PyString_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097 return PyString_FromString(buf);
98}
99
100static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000101ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000102{
103 ste->ste_table = NULL;
104 Py_XDECREF(ste->ste_id);
105 Py_XDECREF(ste->ste_name);
106 Py_XDECREF(ste->ste_symbols);
107 Py_XDECREF(ste->ste_varnames);
108 Py_XDECREF(ste->ste_children);
109 PyObject_Del(ste);
110}
111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000113
Guido van Rossum6f799372001-09-20 20:46:19 +0000114static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000115 {"id", T_OBJECT, OFF(ste_id), READONLY},
116 {"name", T_OBJECT, OFF(ste_name), READONLY},
117 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
118 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
119 {"children", T_OBJECT, OFF(ste_children), READONLY},
120 {"type", T_INT, OFF(ste_type), READONLY},
121 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000122 {NULL}
123};
124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125PyTypeObject PySTEntry_Type = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126 PyObject_HEAD_INIT(&PyType_Type)
127 0,
128 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000129 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130 0,
131 (destructor)ste_dealloc, /* tp_dealloc */
132 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000133 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000134 0, /* tp_setattr */
135 0, /* tp_compare */
136 (reprfunc)ste_repr, /* tp_repr */
137 0, /* tp_as_number */
138 0, /* tp_as_sequence */
139 0, /* tp_as_mapping */
140 0, /* tp_hash */
141 0, /* tp_call */
142 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000143 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000144 0, /* tp_setattro */
145 0, /* tp_as_buffer */
146 Py_TPFLAGS_DEFAULT, /* tp_flags */
147 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000148 0, /* tp_traverse */
149 0, /* tp_clear */
150 0, /* tp_richcompare */
151 0, /* tp_weaklistoffset */
152 0, /* tp_iter */
153 0, /* tp_iternext */
154 0, /* tp_methods */
155 ste_memberlist, /* tp_members */
156 0, /* tp_getset */
157 0, /* tp_base */
158 0, /* tp_dict */
159 0, /* tp_descr_get */
160 0, /* tp_descr_set */
161 0, /* tp_dictoffset */
162 0, /* tp_init */
163 0, /* tp_alloc */
164 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000165};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166
167static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000168static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000170 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171static int symtable_exit_block(struct symtable *st, void *ast);
172static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
173static int symtable_visit_expr(struct symtable *st, expr_ty s);
174static int symtable_visit_genexp(struct symtable *st, expr_ty s);
175static int symtable_visit_arguments(struct symtable *st, arguments_ty);
176static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
177static int symtable_visit_alias(struct symtable *st, alias_ty);
178static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
179static int symtable_visit_keyword(struct symtable *st, keyword_ty);
180static int symtable_visit_slice(struct symtable *st, slice_ty);
Neal Norwitzc1505362006-12-28 06:47:50 +0000181static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top,
182 int annotations);
183static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args,
184 int annotations);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000186static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
188
Nick Coghlan99b25332005-11-16 12:45:24 +0000189static identifier top = NULL, lambda = NULL, genexpr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191#define GET_IDENTIFIER(VAR) \
192 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
193
194#define DUPLICATE_ARGUMENT \
195"duplicate argument '%s' in function definition"
196
197static struct symtable *
198symtable_new(void)
199{
200 struct symtable *st;
201
202 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
203 if (st == NULL)
204 return NULL;
205
206 st->st_filename = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000207 st->st_symbols = NULL;
208
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 if ((st->st_stack = PyList_New(0)) == NULL)
210 goto fail;
211 if ((st->st_symbols = PyDict_New()) == NULL)
212 goto fail;
213 st->st_cur = NULL;
214 st->st_tmpname = 0;
215 st->st_private = NULL;
216 return st;
217 fail:
218 PySymtable_Free(st);
219 return NULL;
220}
221
222struct symtable *
223PySymtable_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;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000233 if (!GET_IDENTIFIER(top) ||
234 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000235 PySymtable_Free(st);
236 return NULL;
237 }
238
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 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++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246 if (!symtable_visit_stmt(st,
247 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 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++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257 if (!symtable_visit_stmt(st,
258 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259 goto error;
260 break;
261 case Suite_kind:
262 PyErr_SetString(PyExc_RuntimeError,
263 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000264 goto error;
265 }
266 if (!symtable_exit_block(st, (void *)mod)) {
267 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 return NULL;
269 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 if (symtable_analyze(st))
271 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000272 PySymtable_Free(st);
273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000275 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000276 PySymtable_Free(st);
277 return NULL;
278}
279
280void
281PySymtable_Free(struct symtable *st)
282{
283 Py_XDECREF(st->st_symbols);
284 Py_XDECREF(st->st_stack);
285 PyMem_Free((void *)st);
286}
287
288PySTEntryObject *
289PySymtable_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));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300 }
301 else {
302 PyErr_SetString(PyExc_KeyError,
303 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000305
306 Py_DECREF(k);
307 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308}
309
310int
311PyST_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
Jeremy Hylton81e95022007-02-27 06:50:52 +0000337 TODO(jhylton): Discuss nonlocal
338
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339 The symbol table requires two passes to determine the scope of each name.
340 The first pass collects raw facts from the AST: the name is a parameter
341 here, the name is used by not defined here, etc. The second pass analyzes
342 these facts during a pass over the PySTEntryObjects created during pass 1.
343
344 When a function is entered during the second pass, the parent passes
345 the set of all name bindings visible to its children. These bindings
346 are used to determine if the variable is free or an implicit global.
347 After doing the local analysis, it analyzes each of its child blocks
348 using an updated set of name bindings.
349
350 The children update the free variable set. If a local variable is free
351 in a child, the variable is marked as a cell. The current function must
352 provide runtime storage for the variable that may outlive the function's
353 frame. Cell variables are removed from the free set before the analyze
354 function returns to its parent.
355
356 The sets of bound and free variables are implemented as dictionaries
357 mapping strings to None.
358*/
359
360#define SET_SCOPE(DICT, NAME, I) { \
361 PyObject *o = PyInt_FromLong(I); \
362 if (!o) \
363 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000364 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
365 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000367 } \
368 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369}
370
371/* Decide on scope of name, given flags.
372
373 The dicts passed in as arguments are modified as necessary.
374 ste is passed so that flags can be updated.
375*/
376
377static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000378analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 PyObject *bound, PyObject *local, PyObject *free,
380 PyObject *global)
381{
382 if (flags & DEF_GLOBAL) {
383 if (flags & DEF_PARAM) {
384 PyErr_Format(PyExc_SyntaxError,
385 "name '%s' is local and global",
386 PyString_AS_STRING(name));
387 return 0;
388 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000389 if (flags & DEF_NONLOCAL) {
390 PyErr_Format(PyExc_SyntaxError,
391 "name '%s' is nonlocal and global",
392 PyString_AS_STRING(name));
393 return 0;
394 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
396 if (PyDict_SetItem(global, name, Py_None) < 0)
397 return 0;
398 if (bound && PyDict_GetItem(bound, name)) {
399 if (PyDict_DelItem(bound, name) < 0)
400 return 0;
401 }
402 return 1;
403 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000404 if (flags & DEF_NONLOCAL) {
405 if (flags & DEF_PARAM) {
406 PyErr_Format(PyExc_SyntaxError,
407 "name '%s' is local and nonlocal",
408 PyString_AS_STRING(name));
409 return 0;
410 }
411 if (!PyDict_GetItem(bound, name)) {
412 PyErr_Format(PyExc_SyntaxError,
413 "no binding for nonlocal '%s' found",
414 PyString_AS_STRING(name));
415
416 return 0;
417 }
418 SET_SCOPE(dict, name, FREE);
419 ste->ste_free = 1;
420 return PyDict_SetItem(free, name, Py_None) >= 0;
421 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422 if (flags & DEF_BOUND) {
423 SET_SCOPE(dict, name, LOCAL);
424 if (PyDict_SetItem(local, name, Py_None) < 0)
425 return 0;
426 if (PyDict_GetItem(global, name)) {
427 if (PyDict_DelItem(global, name) < 0)
428 return 0;
429 }
430 return 1;
431 }
432 /* If an enclosing block has a binding for this name, it
433 is a free variable rather than a global variable.
434 Note that having a non-NULL bound implies that the block
435 is nested.
436 */
437 if (bound && PyDict_GetItem(bound, name)) {
438 SET_SCOPE(dict, name, FREE);
439 ste->ste_free = 1;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000440 return PyDict_SetItem(free, name, Py_None) >= 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441 }
442 /* If a parent has a global statement, then call it global
443 explicit? It could also be global implicit.
444 */
Jeremy Hylton81e95022007-02-27 06:50:52 +0000445 if (global && PyDict_GetItem(global, name)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
447 return 1;
448 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000449 if (ste->ste_nested)
450 ste->ste_free = 1;
451 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
452 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453}
454
455#undef SET_SCOPE
456
457/* If a name is defined in free and also in locals, then this block
458 provides the binding for the free variable. The name should be
459 marked CELL in this block and removed from the free list.
460
461 Note that the current block's free variables are included in free.
462 That's safe because no name can be free and local in the same scope.
463*/
464
465static int
466analyze_cells(PyObject *scope, PyObject *free)
467{
468 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000469 int success = 0;
470 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471
472 w = PyInt_FromLong(CELL);
473 if (!w)
474 return 0;
475 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000476 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000478 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479 if (flags != LOCAL)
480 continue;
481 if (!PyDict_GetItem(free, name))
482 continue;
483 /* Replace LOCAL with CELL for this name, and remove
484 from free. It is safe to replace the value of name
485 in the dict, because it will not cause a resize.
486 */
487 if (PyDict_SetItem(scope, name, w) < 0)
488 goto error;
489 if (!PyDict_DelItem(free, name) < 0)
490 goto error;
491 }
492 success = 1;
493 error:
494 Py_DECREF(w);
495 return success;
496}
497
498/* Check for illegal statements in unoptimized namespaces */
499static int
500check_unoptimized(const PySTEntryObject* ste) {
501 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000502 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000503
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000504 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000505 || !(ste->ste_free || ste->ste_child_free))
506 return 1;
507
Armin Rigo31441302005-10-21 12:57:31 +0000508 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 "contains a nested function with free variables" :
510 "is a nested function");
511
512 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000513 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 return 1;
515 case OPT_IMPORT_STAR:
516 PyOS_snprintf(buf, sizeof(buf),
517 "import * is not allowed in function '%.100s' "
518 "because it is %s",
519 PyString_AS_STRING(ste->ste_name), trailer);
520 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 }
522
523 PyErr_SetString(PyExc_SyntaxError, buf);
524 PyErr_SyntaxLocation(ste->ste_table->st_filename,
525 ste->ste_opt_lineno);
526 return 0;
527}
528
529/* Enter the final scope information into the st_symbols dict.
530 *
531 * All arguments are dicts. Modifies symbols, others are read-only.
532*/
533static int
534update_symbols(PyObject *symbols, PyObject *scope,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536{
537 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000538 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
540 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000541 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542 assert(PyInt_Check(v));
543 flags = PyInt_AS_LONG(v);
544 w = PyDict_GetItem(scope, name);
545 assert(w && PyInt_Check(w));
546 i = PyInt_AS_LONG(w);
547 flags |= (i << SCOPE_OFF);
548 u = PyInt_FromLong(flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000549 if (!u)
550 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551 if (PyDict_SetItem(symbols, name, u) < 0) {
552 Py_DECREF(u);
553 return 0;
554 }
555 Py_DECREF(u);
556 }
557
558 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
559 if (!free_value)
560 return 0;
561
562 /* add a free variable when it's only use is for creating a closure */
563 pos = 0;
564 while (PyDict_Next(free, &pos, &name, &v)) {
565 PyObject *o = PyDict_GetItem(symbols, name);
566
567 if (o) {
568 /* It could be a free variable in a method of
569 the class that has the same name as a local
570 or global in the class scope.
571 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000572 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000574 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 o = PyInt_FromLong(i);
576 if (!o) {
577 Py_DECREF(free_value);
578 return 0;
579 }
580 if (PyDict_SetItem(symbols, name, o) < 0) {
581 Py_DECREF(o);
582 Py_DECREF(free_value);
583 return 0;
584 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000585 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586 }
587 /* else it's not free, probably a cell */
588 continue;
589 }
590 if (!PyDict_GetItem(bound, name))
591 continue; /* it's a global */
592
593 if (PyDict_SetItem(symbols, name, free_value) < 0) {
594 Py_DECREF(free_value);
595 return 0;
596 }
597 }
598 Py_DECREF(free_value);
599 return 1;
600}
601
602/* Make final symbol table decisions for block of ste.
603 Arguments:
604 ste -- current symtable entry (input/output)
605 bound -- set of variables bound in enclosing scopes (input)
606 free -- set of free variables in enclosed scopes (output)
607 globals -- set of declared global variables in enclosing scopes (input)
608*/
609
610static int
611analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
612 PyObject *global)
613{
614 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
615 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000616 int i, success = 0;
617 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618
619 local = PyDict_New();
620 if (!local)
621 goto error;
622 scope = PyDict_New();
623 if (!scope)
624 goto error;
625 newglobal = PyDict_New();
626 if (!newglobal)
627 goto error;
628 newfree = PyDict_New();
629 if (!newfree)
630 goto error;
631 newbound = PyDict_New();
632 if (!newbound)
633 goto error;
634
635 if (ste->ste_type == ClassBlock) {
636 /* make a copy of globals before calling analyze_name(),
637 because global statements in the class have no effect
638 on nested functions.
639 */
640 if (PyDict_Update(newglobal, global) < 0)
641 goto error;
642 if (bound)
643 if (PyDict_Update(newbound, bound) < 0)
644 goto error;
645 }
646
647 assert(PySTEntry_Check(ste));
648 assert(PyDict_Check(ste->ste_symbols));
649 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000650 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 if (!analyze_name(ste, scope, name, flags, bound, local, free,
652 global))
653 goto error;
654 }
655
656 if (ste->ste_type != ClassBlock) {
657 if (ste->ste_type == FunctionBlock) {
658 if (PyDict_Update(newbound, local) < 0)
659 goto error;
660 }
661 if (bound) {
662 if (PyDict_Update(newbound, bound) < 0)
663 goto error;
664 }
665 if (PyDict_Update(newglobal, global) < 0)
666 goto error;
667 }
668
669 /* Recursively call analyze_block() on each child block */
670 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
671 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000672 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000674 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 if (!analyze_block(entry, newbound, newfree, newglobal))
676 goto error;
677 if (entry->ste_free || entry->ste_child_free)
678 ste->ste_child_free = 1;
679 }
680
681 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
682 goto error;
683 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
684 ste->ste_type == ClassBlock))
685 goto error;
686 if (!check_unoptimized(ste))
687 goto error;
688
689 if (PyDict_Update(free, newfree) < 0)
690 goto error;
691 success = 1;
692 error:
693 Py_XDECREF(local);
694 Py_XDECREF(scope);
695 Py_XDECREF(newbound);
696 Py_XDECREF(newglobal);
697 Py_XDECREF(newfree);
698 if (!success)
699 assert(PyErr_Occurred());
700 return success;
701}
702
703static int
704symtable_analyze(struct symtable *st)
705{
706 PyObject *free, *global;
707 int r;
708
709 free = PyDict_New();
710 if (!free)
711 return 0;
712 global = PyDict_New();
713 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000714 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715 return 0;
716 }
717 r = analyze_block(st->st_top, NULL, free, global);
718 Py_DECREF(free);
719 Py_DECREF(global);
720 return r;
721}
722
723
724static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000725symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726{
727 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000728 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
730 PyErr_SetString(PyExc_SyntaxError, msg);
731 PyErr_SyntaxLocation(st->st_filename,
732 st->st_cur->ste_lineno);
733 }
734 return 0;
735 }
736 return 1;
737}
738
739/* symtable_enter_block() gets a reference via PySTEntry_New().
740 This reference is released when the block is exited, via the DECREF
741 in symtable_exit_block().
742*/
743
744static int
745symtable_exit_block(struct symtable *st, void *ast)
746{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000747 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000749 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750 end = PyList_GET_SIZE(st->st_stack) - 1;
751 if (end >= 0) {
752 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
753 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000754 if (st->st_cur == NULL)
755 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 Py_INCREF(st->st_cur);
757 if (PySequence_DelItem(st->st_stack, end) < 0)
758 return 0;
759 }
760 return 1;
761}
762
763static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000764symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 void *ast, int lineno)
766{
767 PySTEntryObject *prev = NULL;
768
769 if (st->st_cur) {
770 prev = st->st_cur;
771 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 return 0;
773 }
774 Py_DECREF(st->st_cur);
775 }
776 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000777 if (st->st_cur == NULL)
778 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 if (name == GET_IDENTIFIER(top))
780 st->st_global = st->st_cur->ste_symbols;
781 if (prev) {
782 if (PyList_Append(prev->ste_children,
783 (PyObject *)st->st_cur) < 0) {
784 return 0;
785 }
786 }
787 return 1;
788}
789
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000790static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791symtable_lookup(struct symtable *st, PyObject *name)
792{
793 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000794 PyObject *mangled = _Py_Mangle(st->st_private, name);
795 if (!mangled)
796 return 0;
797 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
798 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 if (!o)
800 return 0;
801 return PyInt_AsLong(o);
802}
803
804static int
805symtable_add_def(struct symtable *st, PyObject *name, int flag)
806{
807 PyObject *o;
808 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000809 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000810 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811
Jeremy Hylton81e95022007-02-27 06:50:52 +0000812
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000813 if (!mangled)
814 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000816 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 val = PyInt_AS_LONG(o);
818 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000819 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
821 PyString_AsString(name));
822 PyErr_SyntaxLocation(st->st_filename,
823 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000824 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 }
826 val |= flag;
827 } else
828 val = flag;
829 o = PyInt_FromLong(val);
830 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000831 goto error;
832 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000834 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 }
836 Py_DECREF(o);
837
838 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000839 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
840 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 } else if (flag & DEF_GLOBAL) {
842 /* XXX need to update DEF_GLOBAL for other flags too;
843 perhaps only DEF_FREE_GLOBAL */
844 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000845 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 val |= PyInt_AS_LONG(o);
847 }
848 o = PyInt_FromLong(val);
849 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000850 goto error;
851 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000853 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 }
855 Py_DECREF(o);
856 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000857 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000859
860error:
861 Py_DECREF(mangled);
862 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863}
864
865/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
866 They use the ASDL name to synthesize the name of the C type and the visit
867 function.
868
869 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
870 useful if the first node in the sequence requires special treatment.
871*/
872
873#define VISIT(ST, TYPE, V) \
874 if (!symtable_visit_ ## TYPE((ST), (V))) \
875 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000876
877#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
878 if (!symtable_visit_ ## TYPE((ST), (V))) { \
879 symtable_exit_block((ST), (S)); \
880 return 0; \
881 }
882
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883#define VISIT_SEQ(ST, TYPE, SEQ) { \
884 int i; \
885 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
886 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000887 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 if (!symtable_visit_ ## TYPE((ST), elt)) \
889 return 0; \
890 } \
891}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000892
893#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
894 int i; \
895 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
896 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000897 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000898 if (!symtable_visit_ ## TYPE((ST), elt)) { \
899 symtable_exit_block((ST), (S)); \
900 return 0; \
901 } \
902 } \
903}
904
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
906 int i; \
907 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
908 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000909 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 if (!symtable_visit_ ## TYPE((ST), elt)) \
911 return 0; \
912 } \
913}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000914
915#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
916 int i; \
917 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
918 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000919 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000920 if (!symtable_visit_ ## TYPE((ST), elt)) { \
921 symtable_exit_block((ST), (S)); \
922 return 0; \
923 } \
924 } \
925}
926
Guido van Rossum4f72a782006-10-27 23:31:49 +0000927#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
928 int i = 0; \
929 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
930 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
931 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
932 if (!elt) continue; /* can be NULL */ \
933 if (!symtable_visit_expr((ST), elt)) \
934 return 0; \
935 } \
936}
937
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000939symtable_new_tmpname(struct symtable *st)
940{
941 char tmpname[256];
942 identifier tmp;
943
944 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
945 ++st->st_cur->ste_tmpname);
946 tmp = PyString_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000947 if (!tmp)
948 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000949 if (!symtable_add_def(st, tmp, DEF_LOCAL))
950 return 0;
951 Py_DECREF(tmp);
952 return 1;
953}
954
Guido van Rossum4f72a782006-10-27 23:31:49 +0000955
956
Guido van Rossumc2e20742006-02-27 22:32:47 +0000957static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958symtable_visit_stmt(struct symtable *st, stmt_ty s)
959{
960 switch (s->kind) {
961 case FunctionDef_kind:
962 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
963 return 0;
964 if (s->v.FunctionDef.args->defaults)
965 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000966 if (s->v.FunctionDef.args->kw_defaults)
967 VISIT_KWONLYDEFAULTS(st,
968 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +0000969 if (!symtable_visit_annotations(st, s))
970 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 if (s->v.FunctionDef.decorators)
972 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
973 if (!symtable_enter_block(st, s->v.FunctionDef.name,
974 FunctionBlock, (void *)s, s->lineno))
975 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000976 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
977 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 if (!symtable_exit_block(st, s))
979 return 0;
980 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000981 case ClassDef_kind: {
982 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
984 return 0;
985 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000986 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
987 if (s->v.ClassDef.starargs)
988 VISIT(st, expr, s->v.ClassDef.starargs);
989 if (s->v.ClassDef.kwargs)
990 VISIT(st, expr, s->v.ClassDef.kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
992 (void *)s, s->lineno))
993 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000994 tmp = st->st_private;
995 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000996 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000997 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 if (!symtable_exit_block(st, s))
999 return 0;
1000 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001001 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001003 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001005 st->st_cur->ste_returns_value = 1;
1006 if (st->st_cur->ste_generator) {
1007 PyErr_SetString(PyExc_SyntaxError,
1008 RETURN_VAL_IN_GENERATOR);
1009 PyErr_SyntaxLocation(st->st_filename,
1010 s->lineno);
1011 return 0;
1012 }
1013 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 break;
1015 case Delete_kind:
1016 VISIT_SEQ(st, expr, s->v.Delete.targets);
1017 break;
1018 case Assign_kind:
1019 VISIT_SEQ(st, expr, s->v.Assign.targets);
1020 VISIT(st, expr, s->v.Assign.value);
1021 break;
1022 case AugAssign_kind:
1023 VISIT(st, expr, s->v.AugAssign.target);
1024 VISIT(st, expr, s->v.AugAssign.value);
1025 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 case For_kind:
1027 VISIT(st, expr, s->v.For.target);
1028 VISIT(st, expr, s->v.For.iter);
1029 VISIT_SEQ(st, stmt, s->v.For.body);
1030 if (s->v.For.orelse)
1031 VISIT_SEQ(st, stmt, s->v.For.orelse);
1032 break;
1033 case While_kind:
1034 VISIT(st, expr, s->v.While.test);
1035 VISIT_SEQ(st, stmt, s->v.While.body);
1036 if (s->v.While.orelse)
1037 VISIT_SEQ(st, stmt, s->v.While.orelse);
1038 break;
1039 case If_kind:
1040 /* XXX if 0: and lookup_yield() hacks */
1041 VISIT(st, expr, s->v.If.test);
1042 VISIT_SEQ(st, stmt, s->v.If.body);
1043 if (s->v.If.orelse)
1044 VISIT_SEQ(st, stmt, s->v.If.orelse);
1045 break;
1046 case Raise_kind:
1047 if (s->v.Raise.type) {
1048 VISIT(st, expr, s->v.Raise.type);
1049 if (s->v.Raise.inst) {
1050 VISIT(st, expr, s->v.Raise.inst);
1051 if (s->v.Raise.tback)
1052 VISIT(st, expr, s->v.Raise.tback);
1053 }
1054 }
1055 break;
1056 case TryExcept_kind:
1057 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1058 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1059 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1060 break;
1061 case TryFinally_kind:
1062 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1063 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1064 break;
1065 case Assert_kind:
1066 VISIT(st, expr, s->v.Assert.test);
1067 if (s->v.Assert.msg)
1068 VISIT(st, expr, s->v.Assert.msg);
1069 break;
1070 case Import_kind:
1071 VISIT_SEQ(st, alias, s->v.Import.names);
1072 /* XXX Don't have the lineno available inside
1073 visit_alias */
1074 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1075 st->st_cur->ste_opt_lineno = s->lineno;
1076 break;
1077 case ImportFrom_kind:
1078 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1079 /* XXX Don't have the lineno available inside
1080 visit_alias */
1081 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1082 st->st_cur->ste_opt_lineno = s->lineno;
1083 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 case Global_kind: {
1085 int i;
1086 asdl_seq *seq = s->v.Global.names;
1087 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001088 identifier name = (identifier)asdl_seq_GET(seq, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001090 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 if (cur < 0)
1092 return 0;
1093 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001094 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 if (cur & DEF_LOCAL)
1096 PyOS_snprintf(buf, sizeof(buf),
1097 GLOBAL_AFTER_ASSIGN,
1098 c_name);
1099 else
1100 PyOS_snprintf(buf, sizeof(buf),
1101 GLOBAL_AFTER_USE,
1102 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001103 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104 return 0;
1105 }
1106 if (!symtable_add_def(st, name, DEF_GLOBAL))
1107 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109 break;
1110 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001111 case Nonlocal_kind: {
1112 int i;
1113 asdl_seq *seq = s->v.Nonlocal.names;
1114 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1115 identifier name = (identifier)asdl_seq_GET(seq, i);
1116 char *c_name = PyString_AS_STRING(name);
1117 long cur = symtable_lookup(st, name);
1118 if (cur < 0)
1119 return 0;
1120 if (cur & (DEF_LOCAL | USE)) {
1121 char buf[256];
1122 if (cur & DEF_LOCAL)
1123 PyOS_snprintf(buf, sizeof(buf),
1124 NONLOCAL_AFTER_ASSIGN,
1125 c_name);
1126 else
1127 PyOS_snprintf(buf, sizeof(buf),
1128 NONLOCAL_AFTER_USE,
1129 c_name);
1130 if (!symtable_warn(st, buf, s->lineno))
1131 return 0;
1132 }
1133 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1134 return 0;
1135 }
1136 break;
1137 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 case Expr_kind:
1139 VISIT(st, expr, s->v.Expr.value);
1140 break;
1141 case Pass_kind:
1142 case Break_kind:
1143 case Continue_kind:
1144 /* nothing to do here */
1145 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001146 case With_kind:
1147 if (!symtable_new_tmpname(st))
1148 return 0;
1149 VISIT(st, expr, s->v.With.context_expr);
1150 if (s->v.With.optional_vars) {
1151 if (!symtable_new_tmpname(st))
1152 return 0;
1153 VISIT(st, expr, s->v.With.optional_vars);
1154 }
1155 VISIT_SEQ(st, stmt, s->v.With.body);
1156 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 }
1158 return 1;
1159}
1160
1161static int
1162symtable_visit_expr(struct symtable *st, expr_ty e)
1163{
1164 switch (e->kind) {
1165 case BoolOp_kind:
1166 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1167 break;
1168 case BinOp_kind:
1169 VISIT(st, expr, e->v.BinOp.left);
1170 VISIT(st, expr, e->v.BinOp.right);
1171 break;
1172 case UnaryOp_kind:
1173 VISIT(st, expr, e->v.UnaryOp.operand);
1174 break;
1175 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001176 if (!GET_IDENTIFIER(lambda) ||
1177 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 return 0;
1179 if (e->v.Lambda.args->defaults)
1180 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1181 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001182 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183 FunctionBlock, (void *)e, 0))
1184 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001185 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1186 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 if (!symtable_exit_block(st, (void *)e))
1188 return 0;
1189 break;
1190 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001191 case IfExp_kind:
1192 VISIT(st, expr, e->v.IfExp.test);
1193 VISIT(st, expr, e->v.IfExp.body);
1194 VISIT(st, expr, e->v.IfExp.orelse);
1195 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 case Dict_kind:
1197 VISIT_SEQ(st, expr, e->v.Dict.keys);
1198 VISIT_SEQ(st, expr, e->v.Dict.values);
1199 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001200 case Set_kind:
1201 VISIT_SEQ(st, expr, e->v.Set.elts);
1202 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001203 case ListComp_kind:
1204 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 return 0;
1206 VISIT(st, expr, e->v.ListComp.elt);
1207 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1208 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001209 case GeneratorExp_kind:
1210 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 case Yield_kind:
1214 if (e->v.Yield.value)
1215 VISIT(st, expr, e->v.Yield.value);
1216 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001217 if (st->st_cur->ste_returns_value) {
1218 PyErr_SetString(PyExc_SyntaxError,
1219 RETURN_VAL_IN_GENERATOR);
1220 PyErr_SyntaxLocation(st->st_filename,
1221 e->lineno);
1222 return 0;
1223 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 break;
1225 case Compare_kind:
1226 VISIT(st, expr, e->v.Compare.left);
1227 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1228 break;
1229 case Call_kind:
1230 VISIT(st, expr, e->v.Call.func);
1231 VISIT_SEQ(st, expr, e->v.Call.args);
1232 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1233 if (e->v.Call.starargs)
1234 VISIT(st, expr, e->v.Call.starargs);
1235 if (e->v.Call.kwargs)
1236 VISIT(st, expr, e->v.Call.kwargs);
1237 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 case Num_kind:
1239 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001240 case Bytes_kind:
1241 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 /* Nothing to do here. */
1243 break;
1244 /* The following exprs can be assignment targets. */
1245 case Attribute_kind:
1246 VISIT(st, expr, e->v.Attribute.value);
1247 break;
1248 case Subscript_kind:
1249 VISIT(st, expr, e->v.Subscript.value);
1250 VISIT(st, slice, e->v.Subscript.slice);
1251 break;
1252 case Name_kind:
1253 if (!symtable_add_def(st, e->v.Name.id,
1254 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1255 return 0;
1256 break;
1257 /* child nodes of List and Tuple will have expr_context set */
1258 case List_kind:
1259 VISIT_SEQ(st, expr, e->v.List.elts);
1260 break;
1261 case Tuple_kind:
1262 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1263 break;
1264 }
1265 return 1;
1266}
1267
1268static int
1269symtable_implicit_arg(struct symtable *st, int pos)
1270{
1271 PyObject *id = PyString_FromFormat(".%d", pos);
1272 if (id == NULL)
1273 return 0;
1274 if (!symtable_add_def(st, id, DEF_PARAM)) {
1275 Py_DECREF(id);
1276 return 0;
1277 }
1278 Py_DECREF(id);
1279 return 1;
1280}
1281
1282static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001283symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel,
1284 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001286 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001287
1288 if (!args)
1289 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290
1291 /* go through all the toplevel arguments first */
1292 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001293 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1294 if (arg->kind == SimpleArg_kind) {
1295 if (!annotations) {
1296 if (!symtable_add_def(st,
1297 arg->v.SimpleArg.arg,
1298 DEF_PARAM))
1299 return 0;
1300 }
1301 else if (arg->v.SimpleArg.annotation)
1302 VISIT(st, expr, arg->v.SimpleArg.annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001304 else if (arg->kind == NestedArgs_kind) {
1305 if (toplevel && !annotations) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 if (!symtable_implicit_arg(st, i))
1307 return 0;
1308 }
1309 }
1310 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001311 PyErr_SetString(PyExc_SyntaxError,
1312 "invalid expression in parameter list");
1313 PyErr_SyntaxLocation(st->st_filename,
1314 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315 return 0;
1316 }
1317 }
1318
1319 if (!toplevel) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001320 if (!symtable_visit_params_nested(st, args, annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 return 0;
1322 }
1323
1324 return 1;
1325}
1326
1327static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001328symtable_visit_params_nested(struct symtable *st, asdl_seq *args,
1329 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330{
1331 int i;
1332 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001333 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1334 if (arg->kind == NestedArgs_kind &&
1335 !symtable_visit_params(st, arg->v.NestedArgs.args, 0,
1336 annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 return 0;
1338 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001339
1340 return 1;
1341}
1342
1343
1344static int
1345symtable_visit_annotations(struct symtable *st, stmt_ty s)
1346{
1347 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348
Neal Norwitzc1505362006-12-28 06:47:50 +00001349 if (a->args && !symtable_visit_params(st, a->args, 1, 1))
1350 return 0;
1351 if (a->varargannotation)
1352 VISIT(st, expr, a->varargannotation);
1353 if (a->kwargannotation)
1354 VISIT(st, expr, a->kwargannotation);
1355 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 1))
1356 return 0;
1357 if (s->v.FunctionDef.returns)
1358 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 return 1;
1360}
1361
1362static int
1363symtable_visit_arguments(struct symtable *st, arguments_ty a)
1364{
1365 /* skip default arguments inside function block
1366 XXX should ast be different?
1367 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001368 if (a->args && !symtable_visit_params(st, a->args, 1, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 0))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001371 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 if (a->vararg) {
1373 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1374 return 0;
1375 st->st_cur->ste_varargs = 1;
1376 }
1377 if (a->kwarg) {
1378 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1379 return 0;
1380 st->st_cur->ste_varkeywords = 1;
1381 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001382 if (a->args && !symtable_visit_params_nested(st, a->args, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 return 0;
1384 return 1;
1385}
1386
1387
1388static int
1389symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1390{
1391 if (eh->type)
1392 VISIT(st, expr, eh->type);
1393 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001394 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1395 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 VISIT_SEQ(st, stmt, eh->body);
1397 return 1;
1398}
1399
1400
1401static int
1402symtable_visit_alias(struct symtable *st, alias_ty a)
1403{
1404 /* Compute store_name, the name actually bound by the import
1405 operation. It is diferent than a->name when a->name is a
1406 dotted package name (e.g. spam.eggs)
1407 */
1408 PyObject *store_name;
1409 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1410 const char *base = PyString_AS_STRING(name);
1411 char *dot = strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001412 if (dot) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 store_name = PyString_FromStringAndSize(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001414 if (!store_name)
1415 return 0;
1416 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 else {
1418 store_name = name;
1419 Py_INCREF(store_name);
1420 }
1421 if (strcmp(PyString_AS_STRING(name), "*")) {
1422 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1423 Py_DECREF(store_name);
1424 return r;
1425 }
1426 else {
1427 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001428 int lineno = st->st_cur->ste_lineno;
1429 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001430 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001432 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 }
1434 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001435 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 return 1;
1437 }
1438}
1439
1440
1441static int
1442symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1443{
1444 VISIT(st, expr, lc->target);
1445 VISIT(st, expr, lc->iter);
1446 VISIT_SEQ(st, expr, lc->ifs);
1447 return 1;
1448}
1449
1450
1451static int
1452symtable_visit_keyword(struct symtable *st, keyword_ty k)
1453{
1454 VISIT(st, expr, k->value);
1455 return 1;
1456}
1457
1458
1459static int
1460symtable_visit_slice(struct symtable *st, slice_ty s)
1461{
1462 switch (s->kind) {
1463 case Slice_kind:
1464 if (s->v.Slice.lower)
1465 VISIT(st, expr, s->v.Slice.lower)
1466 if (s->v.Slice.upper)
1467 VISIT(st, expr, s->v.Slice.upper)
1468 if (s->v.Slice.step)
1469 VISIT(st, expr, s->v.Slice.step)
1470 break;
1471 case ExtSlice_kind:
1472 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1473 break;
1474 case Index_kind:
1475 VISIT(st, expr, s->v.Index.value)
1476 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 }
1478 return 1;
1479}
1480
1481static int
1482symtable_visit_genexp(struct symtable *st, expr_ty e)
1483{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 comprehension_ty outermost = ((comprehension_ty)
1485 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1486 /* Outermost iterator is evaluated in current scope */
1487 VISIT(st, expr, outermost->iter);
1488 /* Create generator scope for the rest */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001489 if (!GET_IDENTIFIER(genexpr) ||
1490 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 return 0;
1492 }
1493 st->st_cur->ste_generator = 1;
1494 /* Outermost iter is received as an argument */
1495 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001496 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 return 0;
1498 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001499 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1500 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1501 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1502 e->v.GeneratorExp.generators, 1, (void*)e);
1503 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001504 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505}