blob: 98d1e11564f54cf9988ae802a6baaec41a423d33 [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,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000022 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000023{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000024 PySTEntryObject *ste = NULL;
25 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000026
Antoine Pitrouc83ea132010-05-09 14:46:46 +000027 k = PyLong_FromVoidPtr(key);
28 if (k == NULL)
29 goto fail;
30 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
31 if (ste == NULL)
32 goto fail;
33 ste->ste_table = st;
34 ste->ste_id = k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000035
Antoine Pitrouc83ea132010-05-09 14:46:46 +000036 ste->ste_name = name;
37 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000038
Antoine Pitrouc83ea132010-05-09 14:46:46 +000039 ste->ste_symbols = NULL;
40 ste->ste_varnames = NULL;
41 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Antoine Pitrouc83ea132010-05-09 14:46:46 +000043 ste->ste_symbols = PyDict_New();
44 if (ste->ste_symbols == NULL)
45 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000046
Antoine Pitrouc83ea132010-05-09 14:46:46 +000047 ste->ste_varnames = PyList_New(0);
48 if (ste->ste_varnames == NULL)
49 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000050
Antoine Pitrouc83ea132010-05-09 14:46:46 +000051 ste->ste_children = PyList_New(0);
52 if (ste->ste_children == NULL)
53 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000054
Antoine Pitrouc83ea132010-05-09 14:46:46 +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;
61 ste->ste_opt_lineno = 0;
62 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000063
Antoine Pitrouc83ea132010-05-09 14:46:46 +000064 if (st->st_cur != NULL &&
65 (st->st_cur->ste_nested ||
66 st->st_cur->ste_type == FunctionBlock))
67 ste->ste_nested = 1;
68 ste->ste_child_free = 0;
69 ste->ste_generator = 0;
70 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000071
Antoine Pitrouc83ea132010-05-09 14:46:46 +000072 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
73 goto fail;
74
75 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000076 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +000077 Py_XDECREF(ste);
78 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000079}
80
81static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000084 char buf[256];
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085
Antoine Pitrouc83ea132010-05-09 14:46:46 +000086 PyOS_snprintf(buf, sizeof(buf),
87 "<symtable entry %.100s(%ld), line %d>",
88 PyString_AS_STRING(ste->ste_name),
89 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
90 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000096 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);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000103}
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[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +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},
113 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
114 {"nested", T_INT, OFF(ste_nested), READONLY},
115 {"type", T_INT, OFF(ste_type), READONLY},
116 {"lineno", T_INT, OFF(ste_lineno), READONLY},
117 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000118};
119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120PyTypeObject PySTEntry_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000121 PyVarObject_HEAD_INIT(&PyType_Type, 0)
122 "symtable entry",
123 sizeof(PySTEntryObject),
124 0,
125 (destructor)ste_dealloc, /* tp_dealloc */
126 0, /* tp_print */
127 0, /* tp_getattr */
128 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 */
137 PyObject_GenericGetAttr, /* tp_getattro */
138 0, /* tp_setattro */
139 0, /* tp_as_buffer */
140 Py_TPFLAGS_DEFAULT, /* tp_flags */
141 0, /* tp_doc */
142 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);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000163static int symtable_enter_block(struct symtable *st, identifier name,
164 _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);
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000169static int symtable_visit_setcomp(struct symtable *st, expr_ty e);
170static int symtable_visit_dictcomp(struct symtable *st, expr_ty e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171static int symtable_visit_arguments(struct symtable *st, arguments_ty);
172static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
173static int symtable_visit_alias(struct symtable *st, alias_ty);
174static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
175static int symtable_visit_keyword(struct symtable *st, keyword_ty);
176static int symtable_visit_slice(struct symtable *st, slice_ty);
177static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
178static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
179static int symtable_implicit_arg(struct symtable *st, int pos);
180
181
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000182static identifier top = NULL, lambda = NULL, genexpr = NULL, setcomp = NULL,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000183 dictcomp = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184
185#define GET_IDENTIFIER(VAR) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000186 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
188#define DUPLICATE_ARGUMENT \
189"duplicate argument '%s' in function definition"
190
191static struct symtable *
192symtable_new(void)
193{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000194 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000196 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
197 if (st == NULL)
198 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000200 st->st_filename = NULL;
201 st->st_symbols = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000202
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000203 if ((st->st_stack = PyList_New(0)) == NULL)
204 goto fail;
205 if ((st->st_symbols = PyDict_New()) == NULL)
206 goto fail;
207 st->st_cur = NULL;
208 st->st_private = NULL;
209 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000211 PySymtable_Free(st);
212 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213}
214
215struct symtable *
216PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
217{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000218 struct symtable *st = symtable_new();
219 asdl_seq *seq;
220 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000222 if (st == NULL)
223 return st;
224 st->st_filename = filename;
225 st->st_future = future;
226 if (!GET_IDENTIFIER(top) ||
227 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
228 PySymtable_Free(st);
229 return NULL;
230 }
Neal Norwitzd12bd012006-07-21 07:59:47 +0000231
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000232 st->st_top = st->st_cur;
233 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
234 /* Any other top-level initialization? */
235 switch (mod->kind) {
236 case Module_kind:
237 seq = mod->v.Module.body;
238 for (i = 0; i < asdl_seq_LEN(seq); i++)
239 if (!symtable_visit_stmt(st,
240 (stmt_ty)asdl_seq_GET(seq, i)))
241 goto error;
242 break;
243 case Expression_kind:
244 if (!symtable_visit_expr(st, mod->v.Expression.body))
245 goto error;
246 break;
247 case Interactive_kind:
248 seq = mod->v.Interactive.body;
249 for (i = 0; i < asdl_seq_LEN(seq); i++)
250 if (!symtable_visit_stmt(st,
251 (stmt_ty)asdl_seq_GET(seq, i)))
252 goto error;
253 break;
254 case Suite_kind:
255 PyErr_SetString(PyExc_RuntimeError,
256 "this compiler does not handle Suites");
257 goto error;
258 }
259 if (!symtable_exit_block(st, (void *)mod)) {
260 PySymtable_Free(st);
261 return NULL;
262 }
263 if (symtable_analyze(st))
264 return st;
265 PySymtable_Free(st);
266 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000268 (void) symtable_exit_block(st, (void *)mod);
269 PySymtable_Free(st);
270 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271}
272
273void
274PySymtable_Free(struct symtable *st)
275{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000276 Py_XDECREF(st->st_symbols);
277 Py_XDECREF(st->st_stack);
278 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279}
280
281PySTEntryObject *
282PySymtable_Lookup(struct symtable *st, void *key)
283{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000286 k = PyLong_FromVoidPtr(key);
287 if (k == NULL)
288 return NULL;
289 v = PyDict_GetItem(st->st_symbols, k);
290 if (v) {
291 assert(PySTEntry_Check(v));
292 Py_INCREF(v);
293 }
294 else {
295 PyErr_SetString(PyExc_KeyError,
296 "unknown symbol table entry");
297 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000298
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000299 Py_DECREF(k);
300 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301}
302
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000303int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304PyST_GetScope(PySTEntryObject *ste, PyObject *name)
305{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000306 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
307 if (!v)
308 return 0;
309 assert(PyInt_Check(v));
310 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311}
312
313
314/* Analyze raw symbol information to determine scope of each name.
315
316 The next several functions are helpers for PySymtable_Analyze(),
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000317 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318 it determines which local variables are cell variables; they provide
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000319 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000321 There are also two kinds of free variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322 explicit global is declared with the global statement. An implicit
323 global is a free variable for which the compiler has found no binding
324 in an enclosing function scope. The implicit global is either a global
325 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
326 to handle these names to implement slightly odd semantics. In such a
327 block, the name is treated as global until it is assigned to; then it
328 is treated as a local.
329
330 The symbol table requires two passes to determine the scope of each name.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000331 The first pass collects raw facts from the AST: the name is a parameter
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332 here, the name is used by not defined here, etc. The second pass analyzes
333 these facts during a pass over the PySTEntryObjects created during pass 1.
334
335 When a function is entered during the second pass, the parent passes
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000336 the set of all name bindings visible to its children. These bindings
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337 are used to determine if the variable is free or an implicit global.
338 After doing the local analysis, it analyzes each of its child blocks
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000339 using an updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 The children update the free variable set. If a local variable is free
342 in a child, the variable is marked as a cell. The current function must
343 provide runtime storage for the variable that may outlive the function's
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344 frame. Cell variables are removed from the free set before the analyze
345 function returns to its parent.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347 The sets of bound and free variables are implemented as dictionaries
348 mapping strings to None.
349*/
350
351#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000352 PyObject *o = PyInt_FromLong(I); \
353 if (!o) \
354 return 0; \
355 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
356 Py_DECREF(o); \
357 return 0; \
358 } \
359 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360}
361
362/* Decide on scope of name, given flags.
363
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000364 The namespace dictionaries may be modified to record information
365 about the new name. For example, a new global will add an entry to
366 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367*/
368
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000369static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000370analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000371 PyObject *bound, PyObject *local, PyObject *free,
372 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000374 if (flags & DEF_GLOBAL) {
375 if (flags & DEF_PARAM) {
376 PyErr_Format(PyExc_SyntaxError,
377 "name '%s' is local and global",
378 PyString_AS_STRING(name));
379 PyErr_SyntaxLocation(ste->ste_table->st_filename,
380 ste->ste_lineno);
381
382 return 0;
383 }
384 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
385 if (PyDict_SetItem(global, name, Py_None) < 0)
386 return 0;
387 if (bound && PyDict_GetItem(bound, name)) {
388 if (PyDict_DelItem(bound, name) < 0)
389 return 0;
390 }
391 return 1;
392 }
393 if (flags & DEF_BOUND) {
394 SET_SCOPE(dict, name, LOCAL);
395 if (PyDict_SetItem(local, name, Py_None) < 0)
396 return 0;
397 if (PyDict_GetItem(global, name)) {
398 if (PyDict_DelItem(global, name) < 0)
399 return 0;
400 }
401 return 1;
402 }
403 /* If an enclosing block has a binding for this name, it
404 is a free variable rather than a global variable.
405 Note that having a non-NULL bound implies that the block
406 is nested.
407 */
408 if (bound && PyDict_GetItem(bound, name)) {
409 SET_SCOPE(dict, name, FREE);
410 ste->ste_free = 1;
411 if (PyDict_SetItem(free, name, Py_None) < 0)
412 return 0;
413 return 1;
414 }
415 /* If a parent has a global statement, then call it global
416 explicit? It could also be global implicit.
417 */
418 else if (global && PyDict_GetItem(global, name)) {
419 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
420 return 1;
421 }
422 else {
423 if (ste->ste_nested)
424 ste->ste_free = 1;
425 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
426 return 1;
427 }
428 /* Should never get here. */
429 PyErr_Format(PyExc_SystemError, "failed to set scope for %s",
430 PyString_AS_STRING(name));
431 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432}
433
434#undef SET_SCOPE
435
436/* If a name is defined in free and also in locals, then this block
437 provides the binding for the free variable. The name should be
438 marked CELL in this block and removed from the free list.
439
440 Note that the current block's free variables are included in free.
441 That's safe because no name can be free and local in the same scope.
442*/
443
444static int
445analyze_cells(PyObject *scope, PyObject *free)
446{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000447 PyObject *name, *v, *w;
448 int success = 0;
449 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000451 w = PyInt_FromLong(CELL);
452 if (!w)
453 return 0;
454 while (PyDict_Next(scope, &pos, &name, &v)) {
455 long flags;
456 assert(PyInt_Check(v));
457 flags = PyInt_AS_LONG(v);
458 if (flags != LOCAL)
459 continue;
460 if (!PyDict_GetItem(free, name))
461 continue;
462 /* Replace LOCAL with CELL for this name, and remove
463 from free. It is safe to replace the value of name
464 in the dict, because it will not cause a resize.
465 */
466 if (PyDict_SetItem(scope, name, w) < 0)
467 goto error;
468 if (!PyDict_DelItem(free, name) < 0)
469 goto error;
470 }
471 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000473 Py_DECREF(w);
474 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475}
476
477/* Check for illegal statements in unoptimized namespaces */
478static int
479check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000480 char buf[300];
481 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000483 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
484 || !(ste->ste_free || ste->ste_child_free))
485 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000487 trailer = (ste->ste_child_free ?
488 "contains a nested function with free variables" :
489 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000491 switch (ste->ste_unoptimized) {
492 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
493 case OPT_EXEC: /* qualified exec is fine */
494 return 1;
495 case OPT_IMPORT_STAR:
496 PyOS_snprintf(buf, sizeof(buf),
497 "import * is not allowed in function '%.100s' "
498 "because it %s",
499 PyString_AS_STRING(ste->ste_name), trailer);
500 break;
501 case OPT_BARE_EXEC:
502 PyOS_snprintf(buf, sizeof(buf),
503 "unqualified exec is not allowed in function "
504 "'%.100s' it %s",
505 PyString_AS_STRING(ste->ste_name), trailer);
506 break;
507 default:
508 PyOS_snprintf(buf, sizeof(buf),
509 "function '%.100s' uses import * and bare exec, "
510 "which are illegal because it %s",
511 PyString_AS_STRING(ste->ste_name), trailer);
512 break;
513 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000515 PyErr_SetString(PyExc_SyntaxError, buf);
516 PyErr_SyntaxLocation(ste->ste_table->st_filename,
517 ste->ste_opt_lineno);
518 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519}
520
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000521/* Enter the final scope information into the st_symbols dict.
522 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 * All arguments are dicts. Modifies symbols, others are read-only.
524*/
525static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000526update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000527 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000529 PyObject *name, *v, *u, *w, *free_value = NULL;
530 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000532 while (PyDict_Next(symbols, &pos, &name, &v)) {
533 long i, flags;
534 assert(PyInt_Check(v));
535 flags = PyInt_AS_LONG(v);
536 w = PyDict_GetItem(scope, name);
537 assert(w && PyInt_Check(w));
538 i = PyInt_AS_LONG(w);
539 flags |= (i << SCOPE_OFF);
540 u = PyInt_FromLong(flags);
541 if (!u)
542 return 0;
543 if (PyDict_SetItem(symbols, name, u) < 0) {
544 Py_DECREF(u);
545 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000547 Py_DECREF(u);
548 }
549
550 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
551 if (!free_value)
552 return 0;
553
554 /* add a free variable when it's only use is for creating a closure */
555 pos = 0;
556 while (PyDict_Next(free, &pos, &name, &v)) {
557 PyObject *o = PyDict_GetItem(symbols, name);
558
559 if (o) {
560 /* It could be a free variable in a method of
561 the class that has the same name as a local
562 or global in the class scope.
563 */
564 if (classflag &&
565 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
566 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
567 o = PyInt_FromLong(i);
568 if (!o) {
569 Py_DECREF(free_value);
570 return 0;
571 }
572 if (PyDict_SetItem(symbols, name, o) < 0) {
573 Py_DECREF(o);
574 Py_DECREF(free_value);
575 return 0;
576 }
577 Py_DECREF(o);
578 }
579 /* else it's not free, probably a cell */
580 continue;
581 }
582 if (!PyDict_GetItem(bound, name))
583 continue; /* it's a global */
584
585 if (PyDict_SetItem(symbols, name, free_value) < 0) {
586 Py_DECREF(free_value);
587 return 0;
588 }
589 }
590 Py_DECREF(free_value);
591 return 1;
592}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593
594/* Make final symbol table decisions for block of ste.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000595
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596 Arguments:
597 ste -- current symtable entry (input/output)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000598 bound -- set of variables bound in enclosing scopes (input). bound
599 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 free -- set of free variables in enclosed scopes (output)
601 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000602
603 The implementation uses two mutually recursive functions,
604 analyze_block() and analyze_child_block(). analyze_block() is
605 responsible for analyzing the individual names defined in a block.
606 analyze_child_block() prepares temporary namespace dictionaries
607 used to evaluated nested blocks.
608
609 The two functions exist because a child block should see the name
610 bindings of its enclosing blocks, but those bindings should not
611 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612*/
613
614static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000615analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
616 PyObject *global, PyObject* child_free);
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000617
618static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000619analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
620 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622 PyObject *name, *v, *local = NULL, *scope = NULL;
623 PyObject *newbound = NULL, *newglobal = NULL;
624 PyObject *newfree = NULL, *allfree = NULL;
625 int i, success = 0;
626 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000628 local = PyDict_New(); /* collect new names bound in block */
629 if (!local)
630 goto error;
631 scope = PyDict_New(); /* collect scopes defined for each name */
632 if (!scope)
633 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000634
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000635 /* Allocate new global and bound variable dictionaries. These
636 dictionaries hold the names visible in nested blocks. For
637 ClassBlocks, the bound and global names are initialized
638 before analyzing names, because class bindings aren't
639 visible in methods. For other blocks, they are initialized
640 after names are analyzed.
641 */
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000642
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000643 /* TODO(jhylton): Package these dicts in a struct so that we
644 can write reasonable helper functions?
645 */
646 newglobal = PyDict_New();
647 if (!newglobal)
648 goto error;
649 newbound = PyDict_New();
650 if (!newbound)
651 goto error;
652 newfree = PyDict_New();
653 if (!newfree)
654 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000656 if (ste->ste_type == ClassBlock) {
657 if (PyDict_Update(newglobal, global) < 0)
658 goto error;
659 if (bound)
660 if (PyDict_Update(newbound, bound) < 0)
661 goto error;
662 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000664 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
665 long flags = PyInt_AS_LONG(v);
666 if (!analyze_name(ste, scope, name, flags,
667 bound, local, free, global))
668 goto error;
669 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000671 if (ste->ste_type != ClassBlock) {
672 if (ste->ste_type == FunctionBlock) {
673 if (PyDict_Update(newbound, local) < 0)
674 goto error;
675 }
676 if (bound) {
677 if (PyDict_Update(newbound, bound) < 0)
678 goto error;
679 }
680 if (PyDict_Update(newglobal, global) < 0)
681 goto error;
682 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000684 /* Recursively call analyze_block() on each child block.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000685
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000686 newbound, newglobal now contain the names visible in
687 nested blocks. The free variables in the children will
688 be collected in allfree.
689 */
690 allfree = PyDict_New();
691 if (!allfree)
692 goto error;
693 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
694 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
695 PySTEntryObject* entry;
696 assert(c && PySTEntry_Check(c));
697 entry = (PySTEntryObject*)c;
698 if (!analyze_child_block(entry, newbound, newfree, newglobal,
699 allfree))
700 goto error;
701 if (entry->ste_free || entry->ste_child_free)
702 ste->ste_child_free = 1;
703 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000705 if (PyDict_Update(newfree, allfree) < 0)
706 goto error;
707 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
708 goto error;
709 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
710 ste->ste_type == ClassBlock))
711 goto error;
712 if (!check_unoptimized(ste))
713 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000715 if (PyDict_Update(free, newfree) < 0)
716 goto error;
717 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 Py_XDECREF(local);
720 Py_XDECREF(scope);
721 Py_XDECREF(newbound);
722 Py_XDECREF(newglobal);
723 Py_XDECREF(newfree);
724 Py_XDECREF(allfree);
725 if (!success)
726 assert(PyErr_Occurred());
727 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728}
729
730static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000731analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
732 PyObject *global, PyObject* child_free)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000733{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000734 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000735
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000736 /* Copy the bound and global dictionaries.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000737
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000738 These dictionary are used by all blocks enclosed by the
739 current block. The analyze_block() call modifies these
740 dictionaries.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000741
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000742 */
743 temp_bound = PyDict_New();
744 if (!temp_bound)
745 goto error;
746 if (PyDict_Update(temp_bound, bound) < 0)
747 goto error;
748 temp_free = PyDict_New();
749 if (!temp_free)
750 goto error;
751 if (PyDict_Update(temp_free, free) < 0)
752 goto error;
753 temp_global = PyDict_New();
754 if (!temp_global)
755 goto error;
756 if (PyDict_Update(temp_global, global) < 0)
757 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000758
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000759 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
760 goto error;
761 if (PyDict_Update(child_free, temp_free) < 0)
762 goto error;
763 Py_DECREF(temp_bound);
764 Py_DECREF(temp_free);
765 Py_DECREF(temp_global);
766 return 1;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000767 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000768 Py_XDECREF(temp_bound);
769 Py_XDECREF(temp_free);
770 Py_XDECREF(temp_global);
771 return 0;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000772}
773
774static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775symtable_analyze(struct symtable *st)
776{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000777 PyObject *free, *global;
778 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000780 free = PyDict_New();
781 if (!free)
782 return 0;
783 global = PyDict_New();
784 if (!global) {
785 Py_DECREF(free);
786 return 0;
787 }
788 r = analyze_block(st->st_top, NULL, free, global);
789 Py_DECREF(free);
790 Py_DECREF(global);
791 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792}
793
794
795static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000796symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000798 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
799 lineno, NULL, NULL) < 0) {
800 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
801 PyErr_SetString(PyExc_SyntaxError, msg);
802 PyErr_SyntaxLocation(st->st_filename,
803 st->st_cur->ste_lineno);
804 }
805 return 0;
806 }
807 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808}
809
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000810/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 This reference is released when the block is exited, via the DECREF
812 in symtable_exit_block().
813*/
814
815static int
816symtable_exit_block(struct symtable *st, void *ast)
817{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000820 Py_CLEAR(st->st_cur);
821 end = PyList_GET_SIZE(st->st_stack) - 1;
822 if (end >= 0) {
823 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
824 end);
825 if (st->st_cur == NULL)
826 return 0;
827 Py_INCREF(st->st_cur);
828 if (PySequence_DelItem(st->st_stack, end) < 0)
829 return 0;
830 }
831 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832}
833
834static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000835symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
836 void *ast, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000838 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000840 if (st->st_cur) {
841 prev = st->st_cur;
842 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
843 return 0;
844 }
845 Py_DECREF(st->st_cur);
846 }
847 st->st_cur = ste_new(st, name, block, ast, lineno);
848 if (st->st_cur == NULL)
849 return 0;
850 if (name == GET_IDENTIFIER(top))
851 st->st_global = st->st_cur->ste_symbols;
852 if (prev) {
853 if (PyList_Append(prev->ste_children,
854 (PyObject *)st->st_cur) < 0) {
855 return 0;
856 }
857 }
858 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859}
860
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000861static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862symtable_lookup(struct symtable *st, PyObject *name)
863{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000864 PyObject *o;
865 PyObject *mangled = _Py_Mangle(st->st_private, name);
866 if (!mangled)
867 return 0;
868 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
869 Py_DECREF(mangled);
870 if (!o)
871 return 0;
872 return PyInt_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873}
874
875static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000876symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000878 PyObject *o;
879 PyObject *dict;
880 long val;
881 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000883 if (!mangled)
884 return 0;
885 dict = st->st_cur->ste_symbols;
886 if ((o = PyDict_GetItem(dict, mangled))) {
887 val = PyInt_AS_LONG(o);
888 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
889 /* Is it better to use 'mangled' or 'name' here? */
890 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
891 PyString_AsString(name));
892 PyErr_SyntaxLocation(st->st_filename,
893 st->st_cur->ste_lineno);
894 goto error;
895 }
896 val |= flag;
897 } else
898 val = flag;
899 o = PyInt_FromLong(val);
900 if (o == NULL)
901 goto error;
902 if (PyDict_SetItem(dict, mangled, o) < 0) {
903 Py_DECREF(o);
904 goto error;
905 }
906 Py_DECREF(o);
907
908 if (flag & DEF_PARAM) {
909 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
910 goto error;
911 } else if (flag & DEF_GLOBAL) {
912 /* XXX need to update DEF_GLOBAL for other flags too;
913 perhaps only DEF_FREE_GLOBAL */
914 val = flag;
915 if ((o = PyDict_GetItem(st->st_global, mangled))) {
916 val |= PyInt_AS_LONG(o);
917 }
918 o = PyInt_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919 if (o == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000920 goto error;
921 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
922 Py_DECREF(o);
923 goto error;
924 }
925 Py_DECREF(o);
926 }
927 Py_DECREF(mangled);
928 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000929
930error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000931 Py_DECREF(mangled);
932 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933}
934
935/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
936 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000937 function.
938
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
940 useful if the first node in the sequence requires special treatment.
941*/
942
943#define VISIT(ST, TYPE, V) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000944 if (!symtable_visit_ ## TYPE((ST), (V))) \
945 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000946
947#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 if (!symtable_visit_ ## TYPE((ST), (V))) { \
949 symtable_exit_block((ST), (S)); \
950 return 0; \
951 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000952
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000954 int i; \
955 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
956 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
957 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
958 if (!symtable_visit_ ## TYPE((ST), elt)) \
959 return 0; \
960 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000962
963#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000964 int i; \
965 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
966 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
967 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
968 if (!symtable_visit_ ## TYPE((ST), elt)) { \
969 symtable_exit_block((ST), (S)); \
970 return 0; \
971 } \
972 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000973}
974
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000976 int i; \
977 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
978 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
979 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
980 if (!symtable_visit_ ## TYPE((ST), elt)) \
981 return 0; \
982 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000984
985#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000986 int i; \
987 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
988 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
989 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
990 if (!symtable_visit_ ## TYPE((ST), elt)) { \
991 symtable_exit_block((ST), (S)); \
992 return 0; \
993 } \
994 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000995}
996
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997static int
998symtable_visit_stmt(struct symtable *st, stmt_ty s)
999{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001000 switch (s->kind) {
1001 case FunctionDef_kind:
1002 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1003 return 0;
1004 if (s->v.FunctionDef.args->defaults)
1005 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1006 if (s->v.FunctionDef.decorator_list)
1007 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1008 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1009 FunctionBlock, (void *)s, s->lineno))
1010 return 0;
1011 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1012 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1013 if (!symtable_exit_block(st, s))
1014 return 0;
1015 break;
1016 case ClassDef_kind: {
1017 PyObject *tmp;
1018 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1019 return 0;
1020 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1021 if (s->v.ClassDef.decorator_list)
1022 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1023 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1024 (void *)s, s->lineno))
1025 return 0;
1026 tmp = st->st_private;
1027 st->st_private = s->v.ClassDef.name;
1028 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1029 st->st_private = tmp;
1030 if (!symtable_exit_block(st, s))
1031 return 0;
1032 break;
1033 }
1034 case Return_kind:
1035 if (s->v.Return.value) {
1036 VISIT(st, expr, s->v.Return.value);
1037 st->st_cur->ste_returns_value = 1;
1038 if (st->st_cur->ste_generator) {
1039 PyErr_SetString(PyExc_SyntaxError,
1040 RETURN_VAL_IN_GENERATOR);
1041 PyErr_SyntaxLocation(st->st_filename,
1042 s->lineno);
1043 return 0;
1044 }
1045 }
1046 break;
1047 case Delete_kind:
1048 VISIT_SEQ(st, expr, s->v.Delete.targets);
1049 break;
1050 case Assign_kind:
1051 VISIT_SEQ(st, expr, s->v.Assign.targets);
1052 VISIT(st, expr, s->v.Assign.value);
1053 break;
1054 case AugAssign_kind:
1055 VISIT(st, expr, s->v.AugAssign.target);
1056 VISIT(st, expr, s->v.AugAssign.value);
1057 break;
1058 case Print_kind:
1059 if (s->v.Print.dest)
1060 VISIT(st, expr, s->v.Print.dest);
1061 VISIT_SEQ(st, expr, s->v.Print.values);
1062 break;
1063 case For_kind:
1064 VISIT(st, expr, s->v.For.target);
1065 VISIT(st, expr, s->v.For.iter);
1066 VISIT_SEQ(st, stmt, s->v.For.body);
1067 if (s->v.For.orelse)
1068 VISIT_SEQ(st, stmt, s->v.For.orelse);
1069 break;
1070 case While_kind:
1071 VISIT(st, expr, s->v.While.test);
1072 VISIT_SEQ(st, stmt, s->v.While.body);
1073 if (s->v.While.orelse)
1074 VISIT_SEQ(st, stmt, s->v.While.orelse);
1075 break;
1076 case If_kind:
1077 /* XXX if 0: and lookup_yield() hacks */
1078 VISIT(st, expr, s->v.If.test);
1079 VISIT_SEQ(st, stmt, s->v.If.body);
1080 if (s->v.If.orelse)
1081 VISIT_SEQ(st, stmt, s->v.If.orelse);
1082 break;
1083 case Raise_kind:
1084 if (s->v.Raise.type) {
1085 VISIT(st, expr, s->v.Raise.type);
1086 if (s->v.Raise.inst) {
1087 VISIT(st, expr, s->v.Raise.inst);
1088 if (s->v.Raise.tback)
1089 VISIT(st, expr, s->v.Raise.tback);
1090 }
1091 }
1092 break;
1093 case TryExcept_kind:
1094 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1095 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1096 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1097 break;
1098 case TryFinally_kind:
1099 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1100 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1101 break;
1102 case Assert_kind:
1103 VISIT(st, expr, s->v.Assert.test);
1104 if (s->v.Assert.msg)
1105 VISIT(st, expr, s->v.Assert.msg);
1106 break;
1107 case Import_kind:
1108 VISIT_SEQ(st, alias, s->v.Import.names);
1109 /* XXX Don't have the lineno available inside
1110 visit_alias */
1111 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1112 st->st_cur->ste_opt_lineno = s->lineno;
1113 break;
1114 case ImportFrom_kind:
1115 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1116 /* XXX Don't have the lineno available inside
1117 visit_alias */
1118 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1119 st->st_cur->ste_opt_lineno = s->lineno;
1120 break;
1121 case Exec_kind:
1122 VISIT(st, expr, s->v.Exec.body);
1123 if (!st->st_cur->ste_opt_lineno)
1124 st->st_cur->ste_opt_lineno = s->lineno;
1125 if (s->v.Exec.globals) {
1126 st->st_cur->ste_unoptimized |= OPT_EXEC;
1127 VISIT(st, expr, s->v.Exec.globals);
1128 if (s->v.Exec.locals)
1129 VISIT(st, expr, s->v.Exec.locals);
1130 } else {
1131 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1132 }
1133 break;
1134 case Global_kind: {
1135 int i;
1136 asdl_seq *seq = s->v.Global.names;
1137 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1138 identifier name = (identifier)asdl_seq_GET(seq, i);
1139 char *c_name = PyString_AS_STRING(name);
1140 long cur = symtable_lookup(st, name);
1141 if (cur < 0)
1142 return 0;
1143 if (cur & (DEF_LOCAL | USE)) {
1144 char buf[256];
1145 if (cur & DEF_LOCAL)
1146 PyOS_snprintf(buf, sizeof(buf),
1147 GLOBAL_AFTER_ASSIGN,
1148 c_name);
1149 else
1150 PyOS_snprintf(buf, sizeof(buf),
1151 GLOBAL_AFTER_USE,
1152 c_name);
1153 if (!symtable_warn(st, buf, s->lineno))
1154 return 0;
1155 }
1156 if (!symtable_add_def(st, name, DEF_GLOBAL))
1157 return 0;
1158 }
1159 break;
1160 }
1161 case Expr_kind:
1162 VISIT(st, expr, s->v.Expr.value);
1163 break;
1164 case Pass_kind:
1165 case Break_kind:
1166 case Continue_kind:
1167 /* nothing to do here */
1168 break;
1169 case With_kind:
1170 VISIT(st, expr, s->v.With.context_expr);
1171 if (s->v.With.optional_vars) {
1172 VISIT(st, expr, s->v.With.optional_vars);
1173 }
1174 VISIT_SEQ(st, stmt, s->v.With.body);
1175 break;
1176 }
1177 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178}
1179
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001180static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181symtable_visit_expr(struct symtable *st, expr_ty e)
1182{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001183 switch (e->kind) {
1184 case BoolOp_kind:
1185 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1186 break;
1187 case BinOp_kind:
1188 VISIT(st, expr, e->v.BinOp.left);
1189 VISIT(st, expr, e->v.BinOp.right);
1190 break;
1191 case UnaryOp_kind:
1192 VISIT(st, expr, e->v.UnaryOp.operand);
1193 break;
1194 case Lambda_kind: {
1195 if (!GET_IDENTIFIER(lambda))
1196 return 0;
1197 if (e->v.Lambda.args->defaults)
1198 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1199 if (!symtable_enter_block(st, lambda,
1200 FunctionBlock, (void *)e, e->lineno))
1201 return 0;
1202 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1203 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1204 if (!symtable_exit_block(st, (void *)e))
1205 return 0;
1206 break;
1207 }
1208 case IfExp_kind:
1209 VISIT(st, expr, e->v.IfExp.test);
1210 VISIT(st, expr, e->v.IfExp.body);
1211 VISIT(st, expr, e->v.IfExp.orelse);
1212 break;
1213 case Dict_kind:
1214 VISIT_SEQ(st, expr, e->v.Dict.keys);
1215 VISIT_SEQ(st, expr, e->v.Dict.values);
1216 break;
1217 case Set_kind:
1218 VISIT_SEQ(st, expr, e->v.Set.elts);
1219 break;
1220 case ListComp_kind:
1221 VISIT(st, expr, e->v.ListComp.elt);
1222 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1223 break;
1224 case GeneratorExp_kind:
1225 if (!symtable_visit_genexp(st, e))
1226 return 0;
1227 break;
1228 case SetComp_kind:
1229 if (!symtable_visit_setcomp(st, e))
1230 return 0;
1231 break;
1232 case DictComp_kind:
1233 if (!symtable_visit_dictcomp(st, e))
1234 return 0;
1235 break;
1236 case Yield_kind:
1237 if (e->v.Yield.value)
1238 VISIT(st, expr, e->v.Yield.value);
1239 st->st_cur->ste_generator = 1;
1240 if (st->st_cur->ste_returns_value) {
1241 PyErr_SetString(PyExc_SyntaxError,
1242 RETURN_VAL_IN_GENERATOR);
1243 PyErr_SyntaxLocation(st->st_filename,
1244 e->lineno);
1245 return 0;
1246 }
1247 break;
1248 case Compare_kind:
1249 VISIT(st, expr, e->v.Compare.left);
1250 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1251 break;
1252 case Call_kind:
1253 VISIT(st, expr, e->v.Call.func);
1254 VISIT_SEQ(st, expr, e->v.Call.args);
1255 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1256 if (e->v.Call.starargs)
1257 VISIT(st, expr, e->v.Call.starargs);
1258 if (e->v.Call.kwargs)
1259 VISIT(st, expr, e->v.Call.kwargs);
1260 break;
1261 case Repr_kind:
1262 VISIT(st, expr, e->v.Repr.value);
1263 break;
1264 case Num_kind:
1265 case Str_kind:
1266 /* Nothing to do here. */
1267 break;
1268 /* The following exprs can be assignment targets. */
1269 case Attribute_kind:
1270 VISIT(st, expr, e->v.Attribute.value);
1271 break;
1272 case Subscript_kind:
1273 VISIT(st, expr, e->v.Subscript.value);
1274 VISIT(st, slice, e->v.Subscript.slice);
1275 break;
1276 case Name_kind:
1277 if (!symtable_add_def(st, e->v.Name.id,
1278 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1279 return 0;
1280 break;
1281 /* child nodes of List and Tuple will have expr_context set */
1282 case List_kind:
1283 VISIT_SEQ(st, expr, e->v.List.elts);
1284 break;
1285 case Tuple_kind:
1286 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1287 break;
1288 }
1289 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290}
1291
1292static int
1293symtable_implicit_arg(struct symtable *st, int pos)
1294{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001295 PyObject *id = PyString_FromFormat(".%d", pos);
1296 if (id == NULL)
1297 return 0;
1298 if (!symtable_add_def(st, id, DEF_PARAM)) {
1299 Py_DECREF(id);
1300 return 0;
1301 }
1302 Py_DECREF(id);
1303 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304}
1305
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001306static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1308{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001309 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001311 /* go through all the toplevel arguments first */
1312 for (i = 0; i < asdl_seq_LEN(args); i++) {
1313 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1314 if (arg->kind == Name_kind) {
1315 assert(arg->v.Name.ctx == Param ||
1316 (arg->v.Name.ctx == Store && !toplevel));
1317 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1318 return 0;
1319 }
1320 else if (arg->kind == Tuple_kind) {
1321 assert(arg->v.Tuple.ctx == Store);
1322 if (toplevel) {
1323 if (!symtable_implicit_arg(st, i))
1324 return 0;
1325 }
1326 }
1327 else {
1328 PyErr_SetString(PyExc_SyntaxError,
1329 "invalid expression in parameter list");
1330 PyErr_SyntaxLocation(st->st_filename,
1331 st->st_cur->ste_lineno);
1332 return 0;
1333 }
1334 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001336 if (!toplevel) {
1337 if (!symtable_visit_params_nested(st, args))
1338 return 0;
1339 }
1340
1341 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342}
1343
1344static int
1345symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1346{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001347 int i;
1348 for (i = 0; i < asdl_seq_LEN(args); i++) {
1349 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1350 if (arg->kind == Tuple_kind &&
1351 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1352 return 0;
1353 }
1354
1355 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356}
1357
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001358static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359symtable_visit_arguments(struct symtable *st, arguments_ty a)
1360{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001361 /* skip default arguments inside function block
1362 XXX should ast be different?
1363 */
1364 if (a->args && !symtable_visit_params(st, a->args, 1))
1365 return 0;
1366 if (a->vararg) {
1367 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1368 return 0;
1369 st->st_cur->ste_varargs = 1;
1370 }
1371 if (a->kwarg) {
1372 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1373 return 0;
1374 st->st_cur->ste_varkeywords = 1;
1375 }
1376 if (a->args && !symtable_visit_params_nested(st, a->args))
1377 return 0;
1378 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379}
1380
1381
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001382static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1384{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001385 if (eh->v.ExceptHandler.type)
1386 VISIT(st, expr, eh->v.ExceptHandler.type);
1387 if (eh->v.ExceptHandler.name)
1388 VISIT(st, expr, eh->v.ExceptHandler.name);
1389 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1390 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391}
1392
1393
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001394static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395symtable_visit_alias(struct symtable *st, alias_ty a)
1396{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001397 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc6660cf2010-06-11 21:40:37 +00001398 operation. It is different than a->name when a->name is a
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001399 dotted package name (e.g. spam.eggs)
1400 */
1401 PyObject *store_name;
1402 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1403 const char *base = PyString_AS_STRING(name);
1404 char *dot = strchr(base, '.');
1405 if (dot) {
1406 store_name = PyString_FromStringAndSize(base, dot - base);
1407 if (!store_name)
1408 return 0;
1409 }
1410 else {
1411 store_name = name;
1412 Py_INCREF(store_name);
1413 }
1414 if (strcmp(PyString_AS_STRING(name), "*")) {
1415 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1416 Py_DECREF(store_name);
1417 return r;
1418 }
1419 else {
1420 if (st->st_cur->ste_type != ModuleBlock) {
1421 int lineno = st->st_cur->ste_lineno;
1422 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
1423 Py_DECREF(store_name);
1424 return 0;
1425 }
1426 }
1427 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1428 Py_DECREF(store_name);
1429 return 1;
1430 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431}
1432
1433
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001434static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1436{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001437 VISIT(st, expr, lc->target);
1438 VISIT(st, expr, lc->iter);
1439 VISIT_SEQ(st, expr, lc->ifs);
1440 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441}
1442
1443
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001444static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445symtable_visit_keyword(struct symtable *st, keyword_ty k)
1446{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001447 VISIT(st, expr, k->value);
1448 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449}
1450
1451
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001452static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453symtable_visit_slice(struct symtable *st, slice_ty s)
1454{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001455 switch (s->kind) {
1456 case Slice_kind:
1457 if (s->v.Slice.lower)
1458 VISIT(st, expr, s->v.Slice.lower)
1459 if (s->v.Slice.upper)
1460 VISIT(st, expr, s->v.Slice.upper)
1461 if (s->v.Slice.step)
1462 VISIT(st, expr, s->v.Slice.step)
1463 break;
1464 case ExtSlice_kind:
1465 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1466 break;
1467 case Index_kind:
1468 VISIT(st, expr, s->v.Index.value)
1469 break;
1470 case Ellipsis_kind:
1471 break;
1472 }
1473 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474}
1475
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001476static int
1477symtable_new_tmpname(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001479 char tmpname[256];
1480 identifier tmp;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001481
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001482 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1483 ++st->st_cur->ste_tmpname);
1484 tmp = PyString_InternFromString(tmpname);
1485 if (!tmp)
1486 return 0;
1487 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1488 return 0;
1489 Py_DECREF(tmp);
1490 return 1;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001491}
1492
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001493static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001494symtable_handle_comprehension(struct symtable *st, expr_ty e,
1495 identifier scope_name, asdl_seq *generators,
1496 expr_ty elt, expr_ty value)
1497{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001498 int is_generator = (e->kind == GeneratorExp_kind);
1499 int needs_tmp = !is_generator;
1500 comprehension_ty outermost = ((comprehension_ty)
1501 asdl_seq_GET(generators, 0));
1502 /* Outermost iterator is evaluated in current scope */
1503 VISIT(st, expr, outermost->iter);
1504 /* Create comprehension scope for the rest */
1505 if (!scope_name ||
1506 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
1507 return 0;
1508 }
1509 st->st_cur->ste_generator = is_generator;
1510 /* Outermost iter is received as an argument */
1511 if (!symtable_implicit_arg(st, 0)) {
1512 symtable_exit_block(st, (void *)e);
1513 return 0;
1514 }
1515 /* Allocate temporary name if needed */
1516 if (needs_tmp && !symtable_new_tmpname(st)) {
1517 symtable_exit_block(st, (void *)e);
1518 return 0;
1519 }
1520 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1521 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1522 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1523 generators, 1, (void*)e);
1524 if (value)
1525 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1526 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1527 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528}
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001529
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001530static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001531symtable_visit_genexp(struct symtable *st, expr_ty e)
1532{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001533 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1534 e->v.GeneratorExp.generators,
1535 e->v.GeneratorExp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001536}
1537
1538static int
1539symtable_visit_setcomp(struct symtable *st, expr_ty e)
1540{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001541 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1542 e->v.SetComp.generators,
1543 e->v.SetComp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001544}
1545
1546static int
1547symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1548{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001549 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1550 e->v.DictComp.generators,
1551 e->v.DictComp.key,
1552 e->v.DictComp.value);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001553}