blob: 5e735ccd28731988d3081a4c2a6423675ff0b38b [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 Hylton3e0055f2005-10-20 19:59:25 +000011#define GLOBAL_AFTER_USE \
12"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000013
Neal Norwitz5d0ad502005-12-19 04:27:42 +000014#define IMPORT_STAR_WARNING "import * only allowed at module level"
15
Georg Brandlddbaa662006-06-04 21:56:52 +000016#define RETURN_VAL_IN_GENERATOR \
17 "'return' with argument inside generator"
Neal Norwitz5d0ad502005-12-19 04:27:42 +000018
Benjamin Petersone0d12eb2008-08-16 23:29:40 +000019
Neal Norwitz090b3dd2006-02-28 22:36:46 +000020static PySTEntryObject *
Benjamin Petersone0d12eb2008-08-16 23:29:40 +000021ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000023{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024 PySTEntryObject *ste = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000025 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027 k = PyLong_FromVoidPtr(key);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000028 if (k == NULL)
29 goto fail;
Neal Norwitz5becac52008-03-15 22:36:01 +000030 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
31 if (ste == NULL)
32 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000033 ste->ste_table = st;
34 ste->ste_id = k;
35
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036 ste->ste_name = name;
37 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000038
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000039 ste->ste_symbols = NULL;
40 ste->ste_varnames = NULL;
41 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000043 ste->ste_symbols = PyDict_New();
44 if (ste->ste_symbols == NULL)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000045 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000046
47 ste->ste_varnames = PyList_New(0);
48 if (ste->ste_varnames == NULL)
49 goto fail;
50
51 ste->ste_children = PyList_New(0);
52 if (ste->ste_children == NULL)
53 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000055 ste->ste_type = block;
56 ste->ste_unoptimized = 0;
57 ste->ste_nested = 0;
58 ste->ste_free = 0;
59 ste->ste_varargs = 0;
60 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000061 ste->ste_opt_lineno = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000062 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000063
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000064 if (st->st_cur != NULL &&
65 (st->st_cur->ste_nested ||
66 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000067 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000068 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000069 ste->ste_generator = 0;
Georg Brandlddbaa662006-06-04 21:56:52 +000070 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000071
72 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
73 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000074
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000075 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000076 fail:
77 Py_XDECREF(ste);
78 return NULL;
79}
80
81static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083{
84 char buf[256];
85
Barry Warsaw4b4ab202001-11-28 21:36:28 +000086 PyOS_snprintf(buf, sizeof(buf),
87 "<symtable entry %.100s(%ld), line %d>",
Gregory P. Smithdd96db62008-06-09 04:58:54 +000088 PyString_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Gregory P. Smithdd96db62008-06-09 04:58:54 +000090 return PyString_FromString(buf);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000091}
92
93static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000094ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000095{
96 ste->ste_table = NULL;
97 Py_XDECREF(ste->ste_id);
98 Py_XDECREF(ste->ste_name);
99 Py_XDECREF(ste->ste_symbols);
100 Py_XDECREF(ste->ste_varnames);
101 Py_XDECREF(ste->ste_children);
102 PyObject_Del(ste);
103}
104
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000106
Guido van Rossum6f799372001-09-20 20:46:19 +0000107static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108 {"id", T_OBJECT, OFF(ste_id), READONLY},
109 {"name", T_OBJECT, OFF(ste_name), READONLY},
110 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
111 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
112 {"children", T_OBJECT, OFF(ste_children), READONLY},
Benjamin Peterson25f2d892008-08-17 02:23:43 +0000113 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
Benjamin Petersone0d4c7b2008-08-17 01:09:17 +0000114 {"nested", T_INT, OFF(ste_nested), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000115 {"type", T_INT, OFF(ste_type), READONLY},
116 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000117 {NULL}
118};
119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120PyTypeObject PySTEntry_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000121 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000122 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124 0,
125 (destructor)ste_dealloc, /* tp_dealloc */
126 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000127 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000128 0, /* tp_setattr */
129 0, /* tp_compare */
130 (reprfunc)ste_repr, /* tp_repr */
131 0, /* tp_as_number */
132 0, /* tp_as_sequence */
133 0, /* tp_as_mapping */
134 0, /* tp_hash */
135 0, /* tp_call */
136 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000137 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000138 0, /* tp_setattro */
139 0, /* tp_as_buffer */
140 Py_TPFLAGS_DEFAULT, /* tp_flags */
141 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000142 0, /* tp_traverse */
143 0, /* tp_clear */
144 0, /* tp_richcompare */
145 0, /* tp_weaklistoffset */
146 0, /* tp_iter */
147 0, /* tp_iternext */
148 0, /* tp_methods */
149 ste_memberlist, /* tp_members */
150 0, /* tp_getset */
151 0, /* tp_base */
152 0, /* tp_dict */
153 0, /* tp_descr_get */
154 0, /* tp_descr_set */
155 0, /* tp_dictoffset */
156 0, /* tp_init */
157 0, /* tp_alloc */
158 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000159};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160
161static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000162static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000164 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165static int symtable_exit_block(struct symtable *st, void *ast);
166static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
167static int symtable_visit_expr(struct symtable *st, expr_ty s);
168static int symtable_visit_genexp(struct symtable *st, expr_ty s);
169static int symtable_visit_arguments(struct symtable *st, arguments_ty);
170static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
171static int symtable_visit_alias(struct symtable *st, alias_ty);
172static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
173static int symtable_visit_keyword(struct symtable *st, keyword_ty);
174static int symtable_visit_slice(struct symtable *st, slice_ty);
175static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
176static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
177static int symtable_implicit_arg(struct symtable *st, int pos);
178
179
Nick Coghlan99b25332005-11-16 12:45:24 +0000180static identifier top = NULL, lambda = NULL, genexpr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181
182#define GET_IDENTIFIER(VAR) \
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000183 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184
185#define DUPLICATE_ARGUMENT \
186"duplicate argument '%s' in function definition"
187
188static struct symtable *
189symtable_new(void)
190{
191 struct symtable *st;
192
193 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
194 if (st == NULL)
195 return NULL;
196
197 st->st_filename = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000198 st->st_symbols = NULL;
199
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 if ((st->st_stack = PyList_New(0)) == NULL)
201 goto fail;
202 if ((st->st_symbols = PyDict_New()) == NULL)
203 goto fail;
204 st->st_cur = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205 st->st_private = NULL;
206 return st;
207 fail:
208 PySymtable_Free(st);
209 return NULL;
210}
211
212struct symtable *
213PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
214{
215 struct symtable *st = symtable_new();
216 asdl_seq *seq;
217 int i;
218
219 if (st == NULL)
220 return st;
221 st->st_filename = filename;
222 st->st_future = future;
Neal Norwitz76059362006-08-19 04:52:03 +0000223 if (!GET_IDENTIFIER(top) ||
224 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000225 PySymtable_Free(st);
226 return NULL;
227 }
228
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229 st->st_top = st->st_cur;
230 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
231 /* Any other top-level initialization? */
232 switch (mod->kind) {
233 case Module_kind:
234 seq = mod->v.Module.body;
235 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000236 if (!symtable_visit_stmt(st,
237 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 goto error;
239 break;
240 case Expression_kind:
241 if (!symtable_visit_expr(st, mod->v.Expression.body))
242 goto error;
243 break;
244 case Interactive_kind:
245 seq = mod->v.Interactive.body;
246 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000247 if (!symtable_visit_stmt(st,
248 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249 goto error;
250 break;
251 case Suite_kind:
252 PyErr_SetString(PyExc_RuntimeError,
253 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000254 goto error;
255 }
256 if (!symtable_exit_block(st, (void *)mod)) {
257 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 return NULL;
259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260 if (symtable_analyze(st))
261 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000262 PySymtable_Free(st);
263 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000265 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266 PySymtable_Free(st);
267 return NULL;
268}
269
270void
271PySymtable_Free(struct symtable *st)
272{
273 Py_XDECREF(st->st_symbols);
274 Py_XDECREF(st->st_stack);
275 PyMem_Free((void *)st);
276}
277
278PySTEntryObject *
279PySymtable_Lookup(struct symtable *st, void *key)
280{
281 PyObject *k, *v;
282
283 k = PyLong_FromVoidPtr(key);
284 if (k == NULL)
285 return NULL;
286 v = PyDict_GetItem(st->st_symbols, k);
287 if (v) {
288 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290 }
291 else {
292 PyErr_SetString(PyExc_KeyError,
293 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000295
296 Py_DECREF(k);
297 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298}
299
300int
301PyST_GetScope(PySTEntryObject *ste, PyObject *name)
302{
303 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
304 if (!v)
305 return 0;
306 assert(PyInt_Check(v));
307 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
308}
309
310
311/* Analyze raw symbol information to determine scope of each name.
312
313 The next several functions are helpers for PySymtable_Analyze(),
314 which determines whether a name is local, global, or free. In addition,
315 it determines which local variables are cell variables; they provide
316 bindings that are used for free variables in enclosed blocks.
317
318 There are also two kinds of free variables, implicit and explicit. An
319 explicit global is declared with the global statement. An implicit
320 global is a free variable for which the compiler has found no binding
321 in an enclosing function scope. The implicit global is either a global
322 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
323 to handle these names to implement slightly odd semantics. In such a
324 block, the name is treated as global until it is assigned to; then it
325 is treated as a local.
326
327 The symbol table requires two passes to determine the scope of each name.
328 The first pass collects raw facts from the AST: the name is a parameter
329 here, the name is used by not defined here, etc. The second pass analyzes
330 these facts during a pass over the PySTEntryObjects created during pass 1.
331
332 When a function is entered during the second pass, the parent passes
333 the set of all name bindings visible to its children. These bindings
334 are used to determine if the variable is free or an implicit global.
335 After doing the local analysis, it analyzes each of its child blocks
336 using an updated set of name bindings.
337
338 The children update the free variable set. If a local variable is free
339 in a child, the variable is marked as a cell. The current function must
340 provide runtime storage for the variable that may outlive the function's
341 frame. Cell variables are removed from the free set before the analyze
342 function returns to its parent.
343
344 The sets of bound and free variables are implemented as dictionaries
345 mapping strings to None.
346*/
347
348#define SET_SCOPE(DICT, NAME, I) { \
349 PyObject *o = PyInt_FromLong(I); \
350 if (!o) \
351 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000352 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
353 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000355 } \
356 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357}
358
359/* Decide on scope of name, given flags.
360
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000361 The namespace dictionaries may be modified to record information
362 about the new name. For example, a new global will add an entry to
363 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364*/
365
366static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000367analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368 PyObject *bound, PyObject *local, PyObject *free,
369 PyObject *global)
370{
371 if (flags & DEF_GLOBAL) {
372 if (flags & DEF_PARAM) {
373 PyErr_Format(PyExc_SyntaxError,
374 "name '%s' is local and global",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000375 PyString_AS_STRING(name));
Benjamin Peterson08473322008-08-16 22:11:33 +0000376 PyErr_SyntaxLocation(ste->ste_table->st_filename,
377 ste->ste_lineno);
378
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 return 0;
380 }
381 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
382 if (PyDict_SetItem(global, name, Py_None) < 0)
383 return 0;
384 if (bound && PyDict_GetItem(bound, name)) {
385 if (PyDict_DelItem(bound, name) < 0)
386 return 0;
387 }
388 return 1;
389 }
390 if (flags & DEF_BOUND) {
391 SET_SCOPE(dict, name, LOCAL);
392 if (PyDict_SetItem(local, name, Py_None) < 0)
393 return 0;
394 if (PyDict_GetItem(global, name)) {
395 if (PyDict_DelItem(global, name) < 0)
396 return 0;
397 }
398 return 1;
399 }
400 /* If an enclosing block has a binding for this name, it
401 is a free variable rather than a global variable.
402 Note that having a non-NULL bound implies that the block
403 is nested.
404 */
405 if (bound && PyDict_GetItem(bound, name)) {
406 SET_SCOPE(dict, name, FREE);
407 ste->ste_free = 1;
408 if (PyDict_SetItem(free, name, Py_None) < 0)
409 return 0;
410 return 1;
411 }
412 /* If a parent has a global statement, then call it global
413 explicit? It could also be global implicit.
414 */
415 else if (global && PyDict_GetItem(global, name)) {
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000416 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000417 return 1;
418 }
419 else {
420 if (ste->ste_nested)
421 ste->ste_free = 1;
422 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
423 return 1;
424 }
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000425 /* Should never get here. */
426 PyErr_Format(PyExc_SystemError, "failed to set scope for %s",
427 PyString_AS_STRING(name));
428 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429}
430
431#undef SET_SCOPE
432
433/* If a name is defined in free and also in locals, then this block
434 provides the binding for the free variable. The name should be
435 marked CELL in this block and removed from the free list.
436
437 Note that the current block's free variables are included in free.
438 That's safe because no name can be free and local in the same scope.
439*/
440
441static int
442analyze_cells(PyObject *scope, PyObject *free)
443{
444 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000445 int success = 0;
446 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000447
448 w = PyInt_FromLong(CELL);
449 if (!w)
450 return 0;
451 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000452 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000454 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455 if (flags != LOCAL)
456 continue;
457 if (!PyDict_GetItem(free, name))
458 continue;
459 /* Replace LOCAL with CELL for this name, and remove
460 from free. It is safe to replace the value of name
461 in the dict, because it will not cause a resize.
462 */
463 if (PyDict_SetItem(scope, name, w) < 0)
464 goto error;
465 if (!PyDict_DelItem(free, name) < 0)
466 goto error;
467 }
468 success = 1;
469 error:
470 Py_DECREF(w);
471 return success;
472}
473
474/* Check for illegal statements in unoptimized namespaces */
475static int
476check_unoptimized(const PySTEntryObject* ste) {
477 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000478 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000480 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481 || !(ste->ste_free || ste->ste_child_free))
482 return 1;
483
Armin Rigo31441302005-10-21 12:57:31 +0000484 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485 "contains a nested function with free variables" :
486 "is a nested function");
487
488 switch (ste->ste_unoptimized) {
489 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
490 case OPT_EXEC: /* qualified exec is fine */
491 return 1;
492 case OPT_IMPORT_STAR:
493 PyOS_snprintf(buf, sizeof(buf),
494 "import * is not allowed in function '%.100s' "
Benjamin Peterson1a4ceb22009-06-23 01:18:57 +0000495 "because it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000496 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497 break;
498 case OPT_BARE_EXEC:
499 PyOS_snprintf(buf, sizeof(buf),
500 "unqualified exec is not allowed in function "
501 "'%.100s' it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000502 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000503 break;
504 default:
505 PyOS_snprintf(buf, sizeof(buf),
506 "function '%.100s' uses import * and bare exec, "
507 "which are illegal because it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000508 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 break;
510 }
511
512 PyErr_SetString(PyExc_SyntaxError, buf);
513 PyErr_SyntaxLocation(ste->ste_table->st_filename,
514 ste->ste_opt_lineno);
515 return 0;
516}
517
518/* Enter the final scope information into the st_symbols dict.
519 *
520 * All arguments are dicts. Modifies symbols, others are read-only.
521*/
522static int
523update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000524 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525{
526 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000527 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528
529 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000530 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 assert(PyInt_Check(v));
532 flags = PyInt_AS_LONG(v);
533 w = PyDict_GetItem(scope, name);
534 assert(w && PyInt_Check(w));
535 i = PyInt_AS_LONG(w);
536 flags |= (i << SCOPE_OFF);
537 u = PyInt_FromLong(flags);
Neal Norwitz18b6adf2006-07-23 07:50:36 +0000538 if (!u)
539 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540 if (PyDict_SetItem(symbols, name, u) < 0) {
541 Py_DECREF(u);
542 return 0;
543 }
544 Py_DECREF(u);
545 }
546
547 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
548 if (!free_value)
549 return 0;
550
551 /* add a free variable when it's only use is for creating a closure */
552 pos = 0;
553 while (PyDict_Next(free, &pos, &name, &v)) {
554 PyObject *o = PyDict_GetItem(symbols, name);
555
556 if (o) {
557 /* It could be a free variable in a method of
558 the class that has the same name as a local
559 or global in the class scope.
560 */
Anthony Baxter019aec62006-04-12 04:00:50 +0000561 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000563 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 o = PyInt_FromLong(i);
565 if (!o) {
566 Py_DECREF(free_value);
567 return 0;
568 }
569 if (PyDict_SetItem(symbols, name, o) < 0) {
570 Py_DECREF(o);
571 Py_DECREF(free_value);
572 return 0;
573 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000574 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 }
576 /* else it's not free, probably a cell */
577 continue;
578 }
579 if (!PyDict_GetItem(bound, name))
580 continue; /* it's a global */
581
582 if (PyDict_SetItem(symbols, name, free_value) < 0) {
583 Py_DECREF(free_value);
584 return 0;
585 }
586 }
587 Py_DECREF(free_value);
588 return 1;
589}
590
591/* Make final symbol table decisions for block of ste.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000592
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 Arguments:
594 ste -- current symtable entry (input/output)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000595 bound -- set of variables bound in enclosing scopes (input). bound
596 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 free -- set of free variables in enclosed scopes (output)
598 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000599
600 The implementation uses two mutually recursive functions,
601 analyze_block() and analyze_child_block(). analyze_block() is
602 responsible for analyzing the individual names defined in a block.
603 analyze_child_block() prepares temporary namespace dictionaries
604 used to evaluated nested blocks.
605
606 The two functions exist because a child block should see the name
607 bindings of its enclosing blocks, but those bindings should not
608 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609*/
610
611static int
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000612analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
613 PyObject *global, PyObject* child_free);
614
615static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
617 PyObject *global)
618{
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000619 PyObject *name, *v, *local = NULL, *scope = NULL;
620 PyObject *newbound = NULL, *newglobal = NULL;
621 PyObject *newfree = NULL, *allfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000622 int i, success = 0;
623 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000625 local = PyDict_New(); /* collect new names bound in block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626 if (!local)
627 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000628 scope = PyDict_New(); /* collect scopes defined for each name */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629 if (!scope)
630 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000631
632 /* Allocate new global and bound variable dictionaries. These
633 dictionaries hold the names visible in nested blocks. For
634 ClassBlocks, the bound and global names are initialized
635 before analyzing names, because class bindings aren't
636 visible in methods. For other blocks, they are initialized
637 after names are analyzed.
638 */
639
640 /* TODO(jhylton): Package these dicts in a struct so that we
641 can write reasonable helper functions?
642 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 newglobal = PyDict_New();
644 if (!newglobal)
645 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 newbound = PyDict_New();
647 if (!newbound)
648 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000649 newfree = PyDict_New();
650 if (!newfree)
651 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652
653 if (ste->ste_type == ClassBlock) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654 if (PyDict_Update(newglobal, global) < 0)
655 goto error;
656 if (bound)
657 if (PyDict_Update(newbound, bound) < 0)
658 goto error;
659 }
660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000662 long flags = PyInt_AS_LONG(v);
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000663 if (!analyze_name(ste, scope, name, flags,
664 bound, local, free, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 goto error;
666 }
667
668 if (ste->ste_type != ClassBlock) {
669 if (ste->ste_type == FunctionBlock) {
670 if (PyDict_Update(newbound, local) < 0)
671 goto error;
672 }
673 if (bound) {
674 if (PyDict_Update(newbound, bound) < 0)
675 goto error;
676 }
677 if (PyDict_Update(newglobal, global) < 0)
678 goto error;
679 }
680
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000681 /* Recursively call analyze_block() on each child block.
682
683 newbound, newglobal now contain the names visible in
684 nested blocks. The free variables in the children will
685 be collected in allfree.
686 */
687 allfree = PyDict_New();
688 if (!allfree)
689 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
691 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000692 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000694 entry = (PySTEntryObject*)c;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000695 if (!analyze_child_block(entry, newbound, newfree, newglobal,
696 allfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 goto error;
698 if (entry->ste_free || entry->ste_child_free)
699 ste->ste_child_free = 1;
700 }
701
Jeremy Hylton26962752009-03-31 15:04:15 +0000702 if (PyDict_Update(newfree, allfree) < 0)
703 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
705 goto error;
706 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
707 ste->ste_type == ClassBlock))
708 goto error;
709 if (!check_unoptimized(ste))
710 goto error;
711
712 if (PyDict_Update(free, newfree) < 0)
713 goto error;
714 success = 1;
715 error:
716 Py_XDECREF(local);
717 Py_XDECREF(scope);
718 Py_XDECREF(newbound);
719 Py_XDECREF(newglobal);
720 Py_XDECREF(newfree);
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000721 Py_XDECREF(allfree);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 if (!success)
723 assert(PyErr_Occurred());
724 return success;
725}
726
727static int
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000728analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
729 PyObject *global, PyObject* child_free)
730{
731 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000732
733 /* Copy the bound and global dictionaries.
734
735 These dictionary are used by all blocks enclosed by the
736 current block. The analyze_block() call modifies these
737 dictionaries.
738
739 */
740 temp_bound = PyDict_New();
741 if (!temp_bound)
742 goto error;
743 if (PyDict_Update(temp_bound, bound) < 0)
744 goto error;
745 temp_free = PyDict_New();
746 if (!temp_free)
747 goto error;
748 if (PyDict_Update(temp_free, free) < 0)
749 goto error;
750 temp_global = PyDict_New();
751 if (!temp_global)
752 goto error;
753 if (PyDict_Update(temp_global, global) < 0)
754 goto error;
755
756 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
757 goto error;
Benjamin Peterson985951d2009-04-02 02:52:46 +0000758 if (PyDict_Update(child_free, temp_free) < 0)
759 goto error;
760 Py_DECREF(temp_bound);
761 Py_DECREF(temp_free);
762 Py_DECREF(temp_global);
763 return 1;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000764 error:
765 Py_XDECREF(temp_bound);
766 Py_XDECREF(temp_free);
767 Py_XDECREF(temp_global);
Benjamin Peterson985951d2009-04-02 02:52:46 +0000768 return 0;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000769}
770
771static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772symtable_analyze(struct symtable *st)
773{
774 PyObject *free, *global;
775 int r;
776
777 free = PyDict_New();
778 if (!free)
779 return 0;
780 global = PyDict_New();
781 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000782 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 return 0;
784 }
785 r = analyze_block(st->st_top, NULL, free, global);
786 Py_DECREF(free);
787 Py_DECREF(global);
788 return r;
789}
790
791
792static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000793symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794{
795 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000796 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
798 PyErr_SetString(PyExc_SyntaxError, msg);
799 PyErr_SyntaxLocation(st->st_filename,
800 st->st_cur->ste_lineno);
801 }
802 return 0;
803 }
804 return 1;
805}
806
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000807/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 This reference is released when the block is exited, via the DECREF
809 in symtable_exit_block().
810*/
811
812static int
813symtable_exit_block(struct symtable *st, void *ast)
814{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000815 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000817 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 end = PyList_GET_SIZE(st->st_stack) - 1;
819 if (end >= 0) {
820 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
821 end);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000822 if (st->st_cur == NULL)
823 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 Py_INCREF(st->st_cur);
825 if (PySequence_DelItem(st->st_stack, end) < 0)
826 return 0;
827 }
828 return 1;
829}
830
831static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000832symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 void *ast, int lineno)
834{
835 PySTEntryObject *prev = NULL;
836
837 if (st->st_cur) {
838 prev = st->st_cur;
839 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 return 0;
841 }
842 Py_DECREF(st->st_cur);
843 }
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000844 st->st_cur = ste_new(st, name, block, ast, lineno);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000845 if (st->st_cur == NULL)
846 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 if (name == GET_IDENTIFIER(top))
848 st->st_global = st->st_cur->ste_symbols;
849 if (prev) {
850 if (PyList_Append(prev->ste_children,
851 (PyObject *)st->st_cur) < 0) {
852 return 0;
853 }
854 }
855 return 1;
856}
857
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000858static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859symtable_lookup(struct symtable *st, PyObject *name)
860{
861 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000862 PyObject *mangled = _Py_Mangle(st->st_private, name);
863 if (!mangled)
864 return 0;
865 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
866 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 if (!o)
868 return 0;
869 return PyInt_AsLong(o);
870}
871
872static int
873symtable_add_def(struct symtable *st, PyObject *name, int flag)
874{
875 PyObject *o;
876 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000877 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000878 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000880 if (!mangled)
881 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000883 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 val = PyInt_AS_LONG(o);
885 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000886 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000888 PyString_AsString(name));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889 PyErr_SyntaxLocation(st->st_filename,
890 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000891 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 }
893 val |= flag;
894 } else
895 val = flag;
896 o = PyInt_FromLong(val);
897 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000898 goto error;
899 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000901 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902 }
903 Py_DECREF(o);
904
905 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000906 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
907 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908 } else if (flag & DEF_GLOBAL) {
909 /* XXX need to update DEF_GLOBAL for other flags too;
910 perhaps only DEF_FREE_GLOBAL */
911 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000912 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913 val |= PyInt_AS_LONG(o);
914 }
915 o = PyInt_FromLong(val);
916 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000917 goto error;
918 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000920 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921 }
922 Py_DECREF(o);
923 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000924 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000926
927error:
928 Py_DECREF(mangled);
929 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930}
931
932/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
933 They use the ASDL name to synthesize the name of the C type and the visit
934 function.
935
936 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
937 useful if the first node in the sequence requires special treatment.
938*/
939
940#define VISIT(ST, TYPE, V) \
941 if (!symtable_visit_ ## TYPE((ST), (V))) \
942 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000943
944#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
945 if (!symtable_visit_ ## TYPE((ST), (V))) { \
946 symtable_exit_block((ST), (S)); \
947 return 0; \
948 }
949
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950#define VISIT_SEQ(ST, TYPE, SEQ) { \
951 int i; \
952 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
953 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000954 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 if (!symtable_visit_ ## TYPE((ST), elt)) \
956 return 0; \
957 } \
958}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000959
960#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
961 int i; \
962 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
963 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000964 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000965 if (!symtable_visit_ ## TYPE((ST), elt)) { \
966 symtable_exit_block((ST), (S)); \
967 return 0; \
968 } \
969 } \
970}
971
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
973 int i; \
974 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
975 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000976 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977 if (!symtable_visit_ ## TYPE((ST), elt)) \
978 return 0; \
979 } \
980}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000981
982#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
983 int i; \
984 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
985 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000986 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000987 if (!symtable_visit_ ## TYPE((ST), elt)) { \
988 symtable_exit_block((ST), (S)); \
989 return 0; \
990 } \
991 } \
992}
993
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994static int
995symtable_visit_stmt(struct symtable *st, stmt_ty s)
996{
997 switch (s->kind) {
998 case FunctionDef_kind:
999 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1000 return 0;
1001 if (s->v.FunctionDef.args->defaults)
1002 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Christian Heimes5224d282008-02-23 15:01:05 +00001003 if (s->v.FunctionDef.decorator_list)
1004 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1006 FunctionBlock, (void *)s, s->lineno))
1007 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001008 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1009 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 if (!symtable_exit_block(st, s))
1011 return 0;
1012 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001013 case ClassDef_kind: {
1014 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1016 return 0;
1017 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Christian Heimes5224d282008-02-23 15:01:05 +00001018 if (s->v.ClassDef.decorator_list)
1019 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1021 (void *)s, s->lineno))
1022 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001023 tmp = st->st_private;
1024 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001025 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001026 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 if (!symtable_exit_block(st, s))
1028 return 0;
1029 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001030 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031 case Return_kind:
Georg Brandlddbaa662006-06-04 21:56:52 +00001032 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 VISIT(st, expr, s->v.Return.value);
Georg Brandlddbaa662006-06-04 21:56:52 +00001034 st->st_cur->ste_returns_value = 1;
1035 if (st->st_cur->ste_generator) {
1036 PyErr_SetString(PyExc_SyntaxError,
1037 RETURN_VAL_IN_GENERATOR);
1038 PyErr_SyntaxLocation(st->st_filename,
1039 s->lineno);
1040 return 0;
1041 }
1042 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 break;
1044 case Delete_kind:
1045 VISIT_SEQ(st, expr, s->v.Delete.targets);
1046 break;
1047 case Assign_kind:
1048 VISIT_SEQ(st, expr, s->v.Assign.targets);
1049 VISIT(st, expr, s->v.Assign.value);
1050 break;
1051 case AugAssign_kind:
1052 VISIT(st, expr, s->v.AugAssign.target);
1053 VISIT(st, expr, s->v.AugAssign.value);
1054 break;
1055 case Print_kind:
1056 if (s->v.Print.dest)
1057 VISIT(st, expr, s->v.Print.dest);
1058 VISIT_SEQ(st, expr, s->v.Print.values);
1059 break;
1060 case For_kind:
1061 VISIT(st, expr, s->v.For.target);
1062 VISIT(st, expr, s->v.For.iter);
1063 VISIT_SEQ(st, stmt, s->v.For.body);
1064 if (s->v.For.orelse)
1065 VISIT_SEQ(st, stmt, s->v.For.orelse);
1066 break;
1067 case While_kind:
1068 VISIT(st, expr, s->v.While.test);
1069 VISIT_SEQ(st, stmt, s->v.While.body);
1070 if (s->v.While.orelse)
1071 VISIT_SEQ(st, stmt, s->v.While.orelse);
1072 break;
1073 case If_kind:
1074 /* XXX if 0: and lookup_yield() hacks */
1075 VISIT(st, expr, s->v.If.test);
1076 VISIT_SEQ(st, stmt, s->v.If.body);
1077 if (s->v.If.orelse)
1078 VISIT_SEQ(st, stmt, s->v.If.orelse);
1079 break;
1080 case Raise_kind:
1081 if (s->v.Raise.type) {
1082 VISIT(st, expr, s->v.Raise.type);
1083 if (s->v.Raise.inst) {
1084 VISIT(st, expr, s->v.Raise.inst);
1085 if (s->v.Raise.tback)
1086 VISIT(st, expr, s->v.Raise.tback);
1087 }
1088 }
1089 break;
1090 case TryExcept_kind:
1091 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1092 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1093 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1094 break;
1095 case TryFinally_kind:
1096 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1097 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1098 break;
1099 case Assert_kind:
1100 VISIT(st, expr, s->v.Assert.test);
1101 if (s->v.Assert.msg)
1102 VISIT(st, expr, s->v.Assert.msg);
1103 break;
1104 case Import_kind:
1105 VISIT_SEQ(st, alias, s->v.Import.names);
1106 /* XXX Don't have the lineno available inside
1107 visit_alias */
1108 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1109 st->st_cur->ste_opt_lineno = s->lineno;
1110 break;
1111 case ImportFrom_kind:
1112 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1113 /* XXX Don't have the lineno available inside
1114 visit_alias */
1115 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1116 st->st_cur->ste_opt_lineno = s->lineno;
1117 break;
1118 case Exec_kind:
1119 VISIT(st, expr, s->v.Exec.body);
1120 if (!st->st_cur->ste_opt_lineno)
1121 st->st_cur->ste_opt_lineno = s->lineno;
1122 if (s->v.Exec.globals) {
1123 st->st_cur->ste_unoptimized |= OPT_EXEC;
1124 VISIT(st, expr, s->v.Exec.globals);
1125 if (s->v.Exec.locals)
1126 VISIT(st, expr, s->v.Exec.locals);
1127 } else {
1128 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1129 }
1130 break;
1131 case Global_kind: {
1132 int i;
1133 asdl_seq *seq = s->v.Global.names;
1134 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001135 identifier name = (identifier)asdl_seq_GET(seq, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001136 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001137 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 if (cur < 0)
1139 return 0;
1140 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001141 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 if (cur & DEF_LOCAL)
1143 PyOS_snprintf(buf, sizeof(buf),
1144 GLOBAL_AFTER_ASSIGN,
1145 c_name);
1146 else
1147 PyOS_snprintf(buf, sizeof(buf),
1148 GLOBAL_AFTER_USE,
1149 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001150 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 return 0;
1152 }
1153 if (!symtable_add_def(st, name, DEF_GLOBAL))
1154 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 break;
1157 }
1158 case Expr_kind:
1159 VISIT(st, expr, s->v.Expr.value);
1160 break;
1161 case Pass_kind:
1162 case Break_kind:
1163 case Continue_kind:
1164 /* nothing to do here */
1165 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001166 case With_kind:
Guido van Rossumc2e20742006-02-27 22:32:47 +00001167 VISIT(st, expr, s->v.With.context_expr);
1168 if (s->v.With.optional_vars) {
Guido van Rossumc2e20742006-02-27 22:32:47 +00001169 VISIT(st, expr, s->v.With.optional_vars);
1170 }
1171 VISIT_SEQ(st, stmt, s->v.With.body);
1172 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 }
1174 return 1;
1175}
1176
1177static int
1178symtable_visit_expr(struct symtable *st, expr_ty e)
1179{
1180 switch (e->kind) {
1181 case BoolOp_kind:
1182 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1183 break;
1184 case BinOp_kind:
1185 VISIT(st, expr, e->v.BinOp.left);
1186 VISIT(st, expr, e->v.BinOp.right);
1187 break;
1188 case UnaryOp_kind:
1189 VISIT(st, expr, e->v.UnaryOp.operand);
1190 break;
1191 case Lambda_kind: {
Benjamin Petersonf67caf82009-06-21 23:01:07 +00001192 if (!GET_IDENTIFIER(lambda))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 return 0;
1194 if (e->v.Lambda.args->defaults)
1195 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Neal Norwitz76059362006-08-19 04:52:03 +00001196 if (!symtable_enter_block(st, lambda,
Benjamin Petersond16d0ab2009-11-20 01:15:53 +00001197 FunctionBlock, (void *)e, e->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001199 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1200 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 if (!symtable_exit_block(st, (void *)e))
1202 return 0;
1203 break;
1204 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001205 case IfExp_kind:
1206 VISIT(st, expr, e->v.IfExp.test);
1207 VISIT(st, expr, e->v.IfExp.body);
1208 VISIT(st, expr, e->v.IfExp.orelse);
1209 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 case Dict_kind:
1211 VISIT_SEQ(st, expr, e->v.Dict.keys);
1212 VISIT_SEQ(st, expr, e->v.Dict.values);
1213 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001214 case Set_kind:
1215 VISIT_SEQ(st, expr, e->v.Set.elts);
1216 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001217 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 VISIT(st, expr, e->v.ListComp.elt);
1219 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1220 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001221 case GeneratorExp_kind:
1222 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 case Yield_kind:
1226 if (e->v.Yield.value)
1227 VISIT(st, expr, e->v.Yield.value);
1228 st->st_cur->ste_generator = 1;
Georg Brandlddbaa662006-06-04 21:56:52 +00001229 if (st->st_cur->ste_returns_value) {
1230 PyErr_SetString(PyExc_SyntaxError,
1231 RETURN_VAL_IN_GENERATOR);
1232 PyErr_SyntaxLocation(st->st_filename,
1233 e->lineno);
1234 return 0;
1235 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236 break;
1237 case Compare_kind:
1238 VISIT(st, expr, e->v.Compare.left);
1239 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1240 break;
1241 case Call_kind:
1242 VISIT(st, expr, e->v.Call.func);
1243 VISIT_SEQ(st, expr, e->v.Call.args);
1244 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1245 if (e->v.Call.starargs)
1246 VISIT(st, expr, e->v.Call.starargs);
1247 if (e->v.Call.kwargs)
1248 VISIT(st, expr, e->v.Call.kwargs);
1249 break;
1250 case Repr_kind:
1251 VISIT(st, expr, e->v.Repr.value);
1252 break;
1253 case Num_kind:
1254 case Str_kind:
1255 /* Nothing to do here. */
1256 break;
1257 /* The following exprs can be assignment targets. */
1258 case Attribute_kind:
1259 VISIT(st, expr, e->v.Attribute.value);
1260 break;
1261 case Subscript_kind:
1262 VISIT(st, expr, e->v.Subscript.value);
1263 VISIT(st, slice, e->v.Subscript.slice);
1264 break;
1265 case Name_kind:
1266 if (!symtable_add_def(st, e->v.Name.id,
1267 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1268 return 0;
1269 break;
1270 /* child nodes of List and Tuple will have expr_context set */
1271 case List_kind:
1272 VISIT_SEQ(st, expr, e->v.List.elts);
1273 break;
1274 case Tuple_kind:
1275 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1276 break;
1277 }
1278 return 1;
1279}
1280
1281static int
1282symtable_implicit_arg(struct symtable *st, int pos)
1283{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001284 PyObject *id = PyString_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 if (id == NULL)
1286 return 0;
1287 if (!symtable_add_def(st, id, DEF_PARAM)) {
1288 Py_DECREF(id);
1289 return 0;
1290 }
1291 Py_DECREF(id);
1292 return 1;
1293}
1294
1295static int
1296symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1297{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001298 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299
1300 /* go through all the toplevel arguments first */
1301 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001302 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 if (arg->kind == Name_kind) {
1304 assert(arg->v.Name.ctx == Param ||
1305 (arg->v.Name.ctx == Store && !toplevel));
1306 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1307 return 0;
1308 }
1309 else if (arg->kind == Tuple_kind) {
1310 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 if (toplevel) {
1312 if (!symtable_implicit_arg(st, i))
1313 return 0;
1314 }
1315 }
1316 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001317 PyErr_SetString(PyExc_SyntaxError,
1318 "invalid expression in parameter list");
1319 PyErr_SyntaxLocation(st->st_filename,
1320 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 return 0;
1322 }
1323 }
1324
1325 if (!toplevel) {
1326 if (!symtable_visit_params_nested(st, args))
1327 return 0;
1328 }
1329
1330 return 1;
1331}
1332
1333static int
1334symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1335{
1336 int i;
1337 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001338 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 if (arg->kind == Tuple_kind &&
1340 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1341 return 0;
1342 }
1343
1344 return 1;
1345}
1346
1347static int
1348symtable_visit_arguments(struct symtable *st, arguments_ty a)
1349{
1350 /* skip default arguments inside function block
1351 XXX should ast be different?
1352 */
1353 if (a->args && !symtable_visit_params(st, a->args, 1))
1354 return 0;
1355 if (a->vararg) {
1356 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1357 return 0;
1358 st->st_cur->ste_varargs = 1;
1359 }
1360 if (a->kwarg) {
1361 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1362 return 0;
1363 st->st_cur->ste_varkeywords = 1;
1364 }
1365 if (a->args && !symtable_visit_params_nested(st, a->args))
1366 return 0;
1367 return 1;
1368}
1369
1370
1371static int
1372symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1373{
Georg Brandla48f3ab2008-03-30 06:40:17 +00001374 if (eh->v.ExceptHandler.type)
1375 VISIT(st, expr, eh->v.ExceptHandler.type);
1376 if (eh->v.ExceptHandler.name)
1377 VISIT(st, expr, eh->v.ExceptHandler.name);
1378 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 return 1;
1380}
1381
1382
1383static int
1384symtable_visit_alias(struct symtable *st, alias_ty a)
1385{
1386 /* Compute store_name, the name actually bound by the import
1387 operation. It is diferent than a->name when a->name is a
1388 dotted package name (e.g. spam.eggs)
1389 */
1390 PyObject *store_name;
1391 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001392 const char *base = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 char *dot = strchr(base, '.');
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001394 if (dot) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001395 store_name = PyString_FromStringAndSize(base, dot - base);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001396 if (!store_name)
1397 return 0;
1398 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 else {
1400 store_name = name;
1401 Py_INCREF(store_name);
1402 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001403 if (strcmp(PyString_AS_STRING(name), "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1405 Py_DECREF(store_name);
1406 return r;
1407 }
1408 else {
1409 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001410 int lineno = st->st_cur->ste_lineno;
1411 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001412 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001414 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 }
1416 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001417 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 return 1;
1419 }
1420}
1421
1422
1423static int
1424symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1425{
1426 VISIT(st, expr, lc->target);
1427 VISIT(st, expr, lc->iter);
1428 VISIT_SEQ(st, expr, lc->ifs);
1429 return 1;
1430}
1431
1432
1433static int
1434symtable_visit_keyword(struct symtable *st, keyword_ty k)
1435{
1436 VISIT(st, expr, k->value);
1437 return 1;
1438}
1439
1440
1441static int
1442symtable_visit_slice(struct symtable *st, slice_ty s)
1443{
1444 switch (s->kind) {
1445 case Slice_kind:
1446 if (s->v.Slice.lower)
1447 VISIT(st, expr, s->v.Slice.lower)
1448 if (s->v.Slice.upper)
1449 VISIT(st, expr, s->v.Slice.upper)
1450 if (s->v.Slice.step)
1451 VISIT(st, expr, s->v.Slice.step)
1452 break;
1453 case ExtSlice_kind:
1454 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1455 break;
1456 case Index_kind:
1457 VISIT(st, expr, s->v.Index.value)
1458 break;
1459 case Ellipsis_kind:
1460 break;
1461 }
1462 return 1;
1463}
1464
1465static int
1466symtable_visit_genexp(struct symtable *st, expr_ty e)
1467{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 comprehension_ty outermost = ((comprehension_ty)
1469 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1470 /* Outermost iterator is evaluated in current scope */
1471 VISIT(st, expr, outermost->iter);
1472 /* Create generator scope for the rest */
Neal Norwitz76059362006-08-19 04:52:03 +00001473 if (!GET_IDENTIFIER(genexpr) ||
Benjamin Peterson009b89d2009-11-20 01:16:58 +00001474 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, e->lineno)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 return 0;
1476 }
1477 st->st_cur->ste_generator = 1;
1478 /* Outermost iter is received as an argument */
1479 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001480 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 return 0;
1482 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001483 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1484 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1485 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1486 e->v.GeneratorExp.generators, 1, (void*)e);
1487 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Neal Norwitz76059362006-08-19 04:52:03 +00001488 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489}