blob: 5bac2a2c9e9f1d03b69beb99bb6ac3957af31dd9 [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);
986 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
987 (void *)s, s->lineno))
988 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000989 tmp = st->st_private;
990 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000991 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000992 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 if (!symtable_exit_block(st, s))
994 return 0;
995 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000996 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000998 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001000 st->st_cur->ste_returns_value = 1;
1001 if (st->st_cur->ste_generator) {
1002 PyErr_SetString(PyExc_SyntaxError,
1003 RETURN_VAL_IN_GENERATOR);
1004 PyErr_SyntaxLocation(st->st_filename,
1005 s->lineno);
1006 return 0;
1007 }
1008 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 break;
1010 case Delete_kind:
1011 VISIT_SEQ(st, expr, s->v.Delete.targets);
1012 break;
1013 case Assign_kind:
1014 VISIT_SEQ(st, expr, s->v.Assign.targets);
1015 VISIT(st, expr, s->v.Assign.value);
1016 break;
1017 case AugAssign_kind:
1018 VISIT(st, expr, s->v.AugAssign.target);
1019 VISIT(st, expr, s->v.AugAssign.value);
1020 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 case For_kind:
1022 VISIT(st, expr, s->v.For.target);
1023 VISIT(st, expr, s->v.For.iter);
1024 VISIT_SEQ(st, stmt, s->v.For.body);
1025 if (s->v.For.orelse)
1026 VISIT_SEQ(st, stmt, s->v.For.orelse);
1027 break;
1028 case While_kind:
1029 VISIT(st, expr, s->v.While.test);
1030 VISIT_SEQ(st, stmt, s->v.While.body);
1031 if (s->v.While.orelse)
1032 VISIT_SEQ(st, stmt, s->v.While.orelse);
1033 break;
1034 case If_kind:
1035 /* XXX if 0: and lookup_yield() hacks */
1036 VISIT(st, expr, s->v.If.test);
1037 VISIT_SEQ(st, stmt, s->v.If.body);
1038 if (s->v.If.orelse)
1039 VISIT_SEQ(st, stmt, s->v.If.orelse);
1040 break;
1041 case Raise_kind:
1042 if (s->v.Raise.type) {
1043 VISIT(st, expr, s->v.Raise.type);
1044 if (s->v.Raise.inst) {
1045 VISIT(st, expr, s->v.Raise.inst);
1046 if (s->v.Raise.tback)
1047 VISIT(st, expr, s->v.Raise.tback);
1048 }
1049 }
1050 break;
1051 case TryExcept_kind:
1052 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1053 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1054 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1055 break;
1056 case TryFinally_kind:
1057 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1058 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1059 break;
1060 case Assert_kind:
1061 VISIT(st, expr, s->v.Assert.test);
1062 if (s->v.Assert.msg)
1063 VISIT(st, expr, s->v.Assert.msg);
1064 break;
1065 case Import_kind:
1066 VISIT_SEQ(st, alias, s->v.Import.names);
1067 /* XXX Don't have the lineno available inside
1068 visit_alias */
1069 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1070 st->st_cur->ste_opt_lineno = s->lineno;
1071 break;
1072 case ImportFrom_kind:
1073 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1074 /* XXX Don't have the lineno available inside
1075 visit_alias */
1076 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1077 st->st_cur->ste_opt_lineno = s->lineno;
1078 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 case Global_kind: {
1080 int i;
1081 asdl_seq *seq = s->v.Global.names;
1082 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001083 identifier name = (identifier)asdl_seq_GET(seq, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001085 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 if (cur < 0)
1087 return 0;
1088 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001089 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090 if (cur & DEF_LOCAL)
1091 PyOS_snprintf(buf, sizeof(buf),
1092 GLOBAL_AFTER_ASSIGN,
1093 c_name);
1094 else
1095 PyOS_snprintf(buf, sizeof(buf),
1096 GLOBAL_AFTER_USE,
1097 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001098 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 return 0;
1100 }
1101 if (!symtable_add_def(st, name, DEF_GLOBAL))
1102 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104 break;
1105 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001106 case Nonlocal_kind: {
1107 int i;
1108 asdl_seq *seq = s->v.Nonlocal.names;
1109 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1110 identifier name = (identifier)asdl_seq_GET(seq, i);
1111 char *c_name = PyString_AS_STRING(name);
1112 long cur = symtable_lookup(st, name);
1113 if (cur < 0)
1114 return 0;
1115 if (cur & (DEF_LOCAL | USE)) {
1116 char buf[256];
1117 if (cur & DEF_LOCAL)
1118 PyOS_snprintf(buf, sizeof(buf),
1119 NONLOCAL_AFTER_ASSIGN,
1120 c_name);
1121 else
1122 PyOS_snprintf(buf, sizeof(buf),
1123 NONLOCAL_AFTER_USE,
1124 c_name);
1125 if (!symtable_warn(st, buf, s->lineno))
1126 return 0;
1127 }
1128 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1129 return 0;
1130 }
1131 break;
1132 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 case Expr_kind:
1134 VISIT(st, expr, s->v.Expr.value);
1135 break;
1136 case Pass_kind:
1137 case Break_kind:
1138 case Continue_kind:
1139 /* nothing to do here */
1140 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001141 case With_kind:
1142 if (!symtable_new_tmpname(st))
1143 return 0;
1144 VISIT(st, expr, s->v.With.context_expr);
1145 if (s->v.With.optional_vars) {
1146 if (!symtable_new_tmpname(st))
1147 return 0;
1148 VISIT(st, expr, s->v.With.optional_vars);
1149 }
1150 VISIT_SEQ(st, stmt, s->v.With.body);
1151 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 }
1153 return 1;
1154}
1155
1156static int
1157symtable_visit_expr(struct symtable *st, expr_ty e)
1158{
1159 switch (e->kind) {
1160 case BoolOp_kind:
1161 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1162 break;
1163 case BinOp_kind:
1164 VISIT(st, expr, e->v.BinOp.left);
1165 VISIT(st, expr, e->v.BinOp.right);
1166 break;
1167 case UnaryOp_kind:
1168 VISIT(st, expr, e->v.UnaryOp.operand);
1169 break;
1170 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001171 if (!GET_IDENTIFIER(lambda) ||
1172 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 return 0;
1174 if (e->v.Lambda.args->defaults)
1175 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1176 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001177 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 FunctionBlock, (void *)e, 0))
1179 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001180 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1181 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 if (!symtable_exit_block(st, (void *)e))
1183 return 0;
1184 break;
1185 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001186 case IfExp_kind:
1187 VISIT(st, expr, e->v.IfExp.test);
1188 VISIT(st, expr, e->v.IfExp.body);
1189 VISIT(st, expr, e->v.IfExp.orelse);
1190 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 case Dict_kind:
1192 VISIT_SEQ(st, expr, e->v.Dict.keys);
1193 VISIT_SEQ(st, expr, e->v.Dict.values);
1194 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001195 case Set_kind:
1196 VISIT_SEQ(st, expr, e->v.Set.elts);
1197 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001198 case ListComp_kind:
1199 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 return 0;
1201 VISIT(st, expr, e->v.ListComp.elt);
1202 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1203 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001204 case GeneratorExp_kind:
1205 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 case Yield_kind:
1209 if (e->v.Yield.value)
1210 VISIT(st, expr, e->v.Yield.value);
1211 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001212 if (st->st_cur->ste_returns_value) {
1213 PyErr_SetString(PyExc_SyntaxError,
1214 RETURN_VAL_IN_GENERATOR);
1215 PyErr_SyntaxLocation(st->st_filename,
1216 e->lineno);
1217 return 0;
1218 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 break;
1220 case Compare_kind:
1221 VISIT(st, expr, e->v.Compare.left);
1222 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1223 break;
1224 case Call_kind:
1225 VISIT(st, expr, e->v.Call.func);
1226 VISIT_SEQ(st, expr, e->v.Call.args);
1227 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1228 if (e->v.Call.starargs)
1229 VISIT(st, expr, e->v.Call.starargs);
1230 if (e->v.Call.kwargs)
1231 VISIT(st, expr, e->v.Call.kwargs);
1232 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 case Num_kind:
1234 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001235 case Bytes_kind:
1236 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 /* Nothing to do here. */
1238 break;
1239 /* The following exprs can be assignment targets. */
1240 case Attribute_kind:
1241 VISIT(st, expr, e->v.Attribute.value);
1242 break;
1243 case Subscript_kind:
1244 VISIT(st, expr, e->v.Subscript.value);
1245 VISIT(st, slice, e->v.Subscript.slice);
1246 break;
1247 case Name_kind:
1248 if (!symtable_add_def(st, e->v.Name.id,
1249 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1250 return 0;
1251 break;
1252 /* child nodes of List and Tuple will have expr_context set */
1253 case List_kind:
1254 VISIT_SEQ(st, expr, e->v.List.elts);
1255 break;
1256 case Tuple_kind:
1257 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1258 break;
1259 }
1260 return 1;
1261}
1262
1263static int
1264symtable_implicit_arg(struct symtable *st, int pos)
1265{
1266 PyObject *id = PyString_FromFormat(".%d", pos);
1267 if (id == NULL)
1268 return 0;
1269 if (!symtable_add_def(st, id, DEF_PARAM)) {
1270 Py_DECREF(id);
1271 return 0;
1272 }
1273 Py_DECREF(id);
1274 return 1;
1275}
1276
1277static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001278symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel,
1279 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001281 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001282
1283 if (!args)
1284 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285
1286 /* go through all the toplevel arguments first */
1287 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001288 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1289 if (arg->kind == SimpleArg_kind) {
1290 if (!annotations) {
1291 if (!symtable_add_def(st,
1292 arg->v.SimpleArg.arg,
1293 DEF_PARAM))
1294 return 0;
1295 }
1296 else if (arg->v.SimpleArg.annotation)
1297 VISIT(st, expr, arg->v.SimpleArg.annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001299 else if (arg->kind == NestedArgs_kind) {
1300 if (toplevel && !annotations) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 if (!symtable_implicit_arg(st, i))
1302 return 0;
1303 }
1304 }
1305 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001306 PyErr_SetString(PyExc_SyntaxError,
1307 "invalid expression in parameter list");
1308 PyErr_SyntaxLocation(st->st_filename,
1309 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 return 0;
1311 }
1312 }
1313
1314 if (!toplevel) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001315 if (!symtable_visit_params_nested(st, args, annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 return 0;
1317 }
1318
1319 return 1;
1320}
1321
1322static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001323symtable_visit_params_nested(struct symtable *st, asdl_seq *args,
1324 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325{
1326 int i;
1327 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001328 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1329 if (arg->kind == NestedArgs_kind &&
1330 !symtable_visit_params(st, arg->v.NestedArgs.args, 0,
1331 annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 return 0;
1333 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001334
1335 return 1;
1336}
1337
1338
1339static int
1340symtable_visit_annotations(struct symtable *st, stmt_ty s)
1341{
1342 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343
Neal Norwitzc1505362006-12-28 06:47:50 +00001344 if (a->args && !symtable_visit_params(st, a->args, 1, 1))
1345 return 0;
1346 if (a->varargannotation)
1347 VISIT(st, expr, a->varargannotation);
1348 if (a->kwargannotation)
1349 VISIT(st, expr, a->kwargannotation);
1350 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 1))
1351 return 0;
1352 if (s->v.FunctionDef.returns)
1353 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 return 1;
1355}
1356
1357static int
1358symtable_visit_arguments(struct symtable *st, arguments_ty a)
1359{
1360 /* skip default arguments inside function block
1361 XXX should ast be different?
1362 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001363 if (a->args && !symtable_visit_params(st, a->args, 1, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001365 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 0))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001366 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367 if (a->vararg) {
1368 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1369 return 0;
1370 st->st_cur->ste_varargs = 1;
1371 }
1372 if (a->kwarg) {
1373 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1374 return 0;
1375 st->st_cur->ste_varkeywords = 1;
1376 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001377 if (a->args && !symtable_visit_params_nested(st, a->args, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 return 0;
1379 return 1;
1380}
1381
1382
1383static int
1384symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1385{
1386 if (eh->type)
1387 VISIT(st, expr, eh->type);
1388 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001389 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1390 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391 VISIT_SEQ(st, stmt, eh->body);
1392 return 1;
1393}
1394
1395
1396static int
1397symtable_visit_alias(struct symtable *st, alias_ty a)
1398{
1399 /* Compute store_name, the name actually bound by the import
1400 operation. It is diferent than a->name when a->name is a
1401 dotted package name (e.g. spam.eggs)
1402 */
1403 PyObject *store_name;
1404 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1405 const char *base = PyString_AS_STRING(name);
1406 char *dot = strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001407 if (dot) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 store_name = PyString_FromStringAndSize(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001409 if (!store_name)
1410 return 0;
1411 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 else {
1413 store_name = name;
1414 Py_INCREF(store_name);
1415 }
1416 if (strcmp(PyString_AS_STRING(name), "*")) {
1417 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1418 Py_DECREF(store_name);
1419 return r;
1420 }
1421 else {
1422 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001423 int lineno = st->st_cur->ste_lineno;
1424 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001425 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001427 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 }
1429 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001430 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 return 1;
1432 }
1433}
1434
1435
1436static int
1437symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1438{
1439 VISIT(st, expr, lc->target);
1440 VISIT(st, expr, lc->iter);
1441 VISIT_SEQ(st, expr, lc->ifs);
1442 return 1;
1443}
1444
1445
1446static int
1447symtable_visit_keyword(struct symtable *st, keyword_ty k)
1448{
1449 VISIT(st, expr, k->value);
1450 return 1;
1451}
1452
1453
1454static int
1455symtable_visit_slice(struct symtable *st, slice_ty s)
1456{
1457 switch (s->kind) {
1458 case Slice_kind:
1459 if (s->v.Slice.lower)
1460 VISIT(st, expr, s->v.Slice.lower)
1461 if (s->v.Slice.upper)
1462 VISIT(st, expr, s->v.Slice.upper)
1463 if (s->v.Slice.step)
1464 VISIT(st, expr, s->v.Slice.step)
1465 break;
1466 case ExtSlice_kind:
1467 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1468 break;
1469 case Index_kind:
1470 VISIT(st, expr, s->v.Index.value)
1471 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 }
1473 return 1;
1474}
1475
1476static int
1477symtable_visit_genexp(struct symtable *st, expr_ty e)
1478{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 comprehension_ty outermost = ((comprehension_ty)
1480 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1481 /* Outermost iterator is evaluated in current scope */
1482 VISIT(st, expr, outermost->iter);
1483 /* Create generator scope for the rest */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001484 if (!GET_IDENTIFIER(genexpr) ||
1485 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486 return 0;
1487 }
1488 st->st_cur->ste_generator = 1;
1489 /* Outermost iter is received as an argument */
1490 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001491 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492 return 0;
1493 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001494 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1495 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1496 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1497 e->v.GeneratorExp.generators, 1, (void*)e);
1498 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001499 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500}