blob: 51b59f0310d8d75916e4bbe055a72d9d8af9a612 [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;
Benjamin Peterson045bbcd2010-10-20 21:28:09 +000062 ste->ste_tmpname = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +000063 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000064
Antoine Pitrouc83ea132010-05-09 14:46:46 +000065 if (st->st_cur != NULL &&
66 (st->st_cur->ste_nested ||
67 st->st_cur->ste_type == FunctionBlock))
68 ste->ste_nested = 1;
69 ste->ste_child_free = 0;
70 ste->ste_generator = 0;
71 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000072
Antoine Pitrouc83ea132010-05-09 14:46:46 +000073 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
74 goto fail;
75
76 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000077 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +000078 Py_XDECREF(ste);
79 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000080}
81
82static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000085 char buf[256];
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000086
Antoine Pitrouc83ea132010-05-09 14:46:46 +000087 PyOS_snprintf(buf, sizeof(buf),
88 "<symtable entry %.100s(%ld), line %d>",
89 PyString_AS_STRING(ste->ste_name),
90 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
91 return PyString_FromString(buf);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092}
93
94static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000097 ste->ste_table = NULL;
98 Py_XDECREF(ste->ste_id);
99 Py_XDECREF(ste->ste_name);
100 Py_XDECREF(ste->ste_symbols);
101 Py_XDECREF(ste->ste_varnames);
102 Py_XDECREF(ste->ste_children);
103 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000104}
105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000107
Guido van Rossum6f799372001-09-20 20:46:19 +0000108static PyMemberDef ste_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000109 {"id", T_OBJECT, OFF(ste_id), READONLY},
110 {"name", T_OBJECT, OFF(ste_name), READONLY},
111 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
112 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
113 {"children", T_OBJECT, OFF(ste_children), READONLY},
114 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
115 {"nested", T_INT, OFF(ste_nested), READONLY},
116 {"type", T_INT, OFF(ste_type), READONLY},
117 {"lineno", T_INT, OFF(ste_lineno), READONLY},
118 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119};
120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121PyTypeObject PySTEntry_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000122 PyVarObject_HEAD_INIT(&PyType_Type, 0)
123 "symtable entry",
124 sizeof(PySTEntryObject),
125 0,
126 (destructor)ste_dealloc, /* tp_dealloc */
127 0, /* tp_print */
128 0, /* tp_getattr */
129 0, /* tp_setattr */
130 0, /* tp_compare */
131 (reprfunc)ste_repr, /* tp_repr */
132 0, /* tp_as_number */
133 0, /* tp_as_sequence */
134 0, /* tp_as_mapping */
135 0, /* tp_hash */
136 0, /* tp_call */
137 0, /* tp_str */
138 PyObject_GenericGetAttr, /* tp_getattro */
139 0, /* tp_setattro */
140 0, /* tp_as_buffer */
141 Py_TPFLAGS_DEFAULT, /* tp_flags */
142 0, /* tp_doc */
143 0, /* tp_traverse */
144 0, /* tp_clear */
145 0, /* tp_richcompare */
146 0, /* tp_weaklistoffset */
147 0, /* tp_iter */
148 0, /* tp_iternext */
149 0, /* tp_methods */
150 ste_memberlist, /* tp_members */
151 0, /* tp_getset */
152 0, /* tp_base */
153 0, /* tp_dict */
154 0, /* tp_descr_get */
155 0, /* tp_descr_set */
156 0, /* tp_dictoffset */
157 0, /* tp_init */
158 0, /* tp_alloc */
159 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000160};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161
162static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000163static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000164static int symtable_enter_block(struct symtable *st, identifier name,
165 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166static int symtable_exit_block(struct symtable *st, void *ast);
167static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
168static int symtable_visit_expr(struct symtable *st, expr_ty s);
169static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000170static int symtable_visit_setcomp(struct symtable *st, expr_ty e);
171static int symtable_visit_dictcomp(struct symtable *st, expr_ty e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000172static int symtable_visit_arguments(struct symtable *st, arguments_ty);
173static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
174static int symtable_visit_alias(struct symtable *st, alias_ty);
175static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
176static int symtable_visit_keyword(struct symtable *st, keyword_ty);
177static int symtable_visit_slice(struct symtable *st, slice_ty);
178static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
179static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
180static int symtable_implicit_arg(struct symtable *st, int pos);
181
182
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000183static identifier top = NULL, lambda = NULL, genexpr = NULL, setcomp = NULL,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000184 dictcomp = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185
186#define GET_IDENTIFIER(VAR) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000187 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188
189#define DUPLICATE_ARGUMENT \
190"duplicate argument '%s' in function definition"
191
192static struct symtable *
193symtable_new(void)
194{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000195 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
198 if (st == NULL)
199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000201 st->st_filename = NULL;
202 st->st_symbols = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000203
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000204 if ((st->st_stack = PyList_New(0)) == NULL)
205 goto fail;
206 if ((st->st_symbols = PyDict_New()) == NULL)
207 goto fail;
208 st->st_cur = NULL;
209 st->st_private = NULL;
210 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000212 PySymtable_Free(st);
213 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214}
215
216struct symtable *
217PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
218{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000219 struct symtable *st = symtable_new();
220 asdl_seq *seq;
221 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 if (st == NULL)
224 return st;
225 st->st_filename = filename;
226 st->st_future = future;
227 if (!GET_IDENTIFIER(top) ||
228 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
229 PySymtable_Free(st);
230 return NULL;
231 }
Neal Norwitzd12bd012006-07-21 07:59:47 +0000232
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000233 st->st_top = st->st_cur;
234 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
235 /* Any other top-level initialization? */
236 switch (mod->kind) {
237 case Module_kind:
238 seq = mod->v.Module.body;
239 for (i = 0; i < asdl_seq_LEN(seq); i++)
240 if (!symtable_visit_stmt(st,
241 (stmt_ty)asdl_seq_GET(seq, i)))
242 goto error;
243 break;
244 case Expression_kind:
245 if (!symtable_visit_expr(st, mod->v.Expression.body))
246 goto error;
247 break;
248 case Interactive_kind:
249 seq = mod->v.Interactive.body;
250 for (i = 0; i < asdl_seq_LEN(seq); i++)
251 if (!symtable_visit_stmt(st,
252 (stmt_ty)asdl_seq_GET(seq, i)))
253 goto error;
254 break;
255 case Suite_kind:
256 PyErr_SetString(PyExc_RuntimeError,
257 "this compiler does not handle Suites");
258 goto error;
259 }
260 if (!symtable_exit_block(st, (void *)mod)) {
261 PySymtable_Free(st);
262 return NULL;
263 }
264 if (symtable_analyze(st))
265 return st;
266 PySymtable_Free(st);
267 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000269 (void) symtable_exit_block(st, (void *)mod);
270 PySymtable_Free(st);
271 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272}
273
274void
275PySymtable_Free(struct symtable *st)
276{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000277 Py_XDECREF(st->st_symbols);
278 Py_XDECREF(st->st_stack);
279 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280}
281
282PySTEntryObject *
283PySymtable_Lookup(struct symtable *st, void *key)
284{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000285 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000287 k = PyLong_FromVoidPtr(key);
288 if (k == NULL)
289 return NULL;
290 v = PyDict_GetItem(st->st_symbols, k);
291 if (v) {
292 assert(PySTEntry_Check(v));
293 Py_INCREF(v);
294 }
295 else {
296 PyErr_SetString(PyExc_KeyError,
297 "unknown symbol table entry");
298 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000299
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000300 Py_DECREF(k);
301 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302}
303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305PyST_GetScope(PySTEntryObject *ste, PyObject *name)
306{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000307 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
308 if (!v)
309 return 0;
310 assert(PyInt_Check(v));
311 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312}
313
314
315/* Analyze raw symbol information to determine scope of each name.
316
317 The next several functions are helpers for PySymtable_Analyze(),
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000318 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319 it determines which local variables are cell variables; they provide
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000320 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000322 There are also two kinds of free variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323 explicit global is declared with the global statement. An implicit
324 global is a free variable for which the compiler has found no binding
325 in an enclosing function scope. The implicit global is either a global
326 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
327 to handle these names to implement slightly odd semantics. In such a
328 block, the name is treated as global until it is assigned to; then it
329 is treated as a local.
330
331 The symbol table requires two passes to determine the scope of each name.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000332 The first pass collects raw facts from the AST: the name is a parameter
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000333 here, the name is used by not defined here, etc. The second pass analyzes
334 these facts during a pass over the PySTEntryObjects created during pass 1.
335
336 When a function is entered during the second pass, the parent passes
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000337 the set of all name bindings visible to its children. These bindings
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338 are used to determine if the variable is free or an implicit global.
339 After doing the local analysis, it analyzes each of its child blocks
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000340 using an updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000342 The children update the free variable set. If a local variable is free
343 in a child, the variable is marked as a cell. The current function must
344 provide runtime storage for the variable that may outlive the function's
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345 frame. Cell variables are removed from the free set before the analyze
346 function returns to its parent.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000347
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348 The sets of bound and free variables are implemented as dictionaries
349 mapping strings to None.
350*/
351
352#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000353 PyObject *o = PyInt_FromLong(I); \
354 if (!o) \
355 return 0; \
356 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
357 Py_DECREF(o); \
358 return 0; \
359 } \
360 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361}
362
363/* Decide on scope of name, given flags.
364
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000365 The namespace dictionaries may be modified to record information
366 about the new name. For example, a new global will add an entry to
367 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368*/
369
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000370static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000371analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000372 PyObject *bound, PyObject *local, PyObject *free,
373 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000375 if (flags & DEF_GLOBAL) {
376 if (flags & DEF_PARAM) {
377 PyErr_Format(PyExc_SyntaxError,
378 "name '%s' is local and global",
379 PyString_AS_STRING(name));
380 PyErr_SyntaxLocation(ste->ste_table->st_filename,
381 ste->ste_lineno);
382
383 return 0;
384 }
385 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
386 if (PyDict_SetItem(global, name, Py_None) < 0)
387 return 0;
388 if (bound && PyDict_GetItem(bound, name)) {
389 if (PyDict_DelItem(bound, name) < 0)
390 return 0;
391 }
392 return 1;
393 }
394 if (flags & DEF_BOUND) {
395 SET_SCOPE(dict, name, LOCAL);
396 if (PyDict_SetItem(local, name, Py_None) < 0)
397 return 0;
398 if (PyDict_GetItem(global, name)) {
399 if (PyDict_DelItem(global, name) < 0)
400 return 0;
401 }
402 return 1;
403 }
404 /* If an enclosing block has a binding for this name, it
405 is a free variable rather than a global variable.
406 Note that having a non-NULL bound implies that the block
407 is nested.
408 */
409 if (bound && PyDict_GetItem(bound, name)) {
410 SET_SCOPE(dict, name, FREE);
411 ste->ste_free = 1;
412 if (PyDict_SetItem(free, name, Py_None) < 0)
413 return 0;
414 return 1;
415 }
416 /* If a parent has a global statement, then call it global
417 explicit? It could also be global implicit.
418 */
419 else if (global && PyDict_GetItem(global, name)) {
420 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
421 return 1;
422 }
423 else {
424 if (ste->ste_nested)
425 ste->ste_free = 1;
426 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
427 return 1;
428 }
429 /* Should never get here. */
430 PyErr_Format(PyExc_SystemError, "failed to set scope for %s",
431 PyString_AS_STRING(name));
432 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433}
434
435#undef SET_SCOPE
436
437/* If a name is defined in free and also in locals, then this block
438 provides the binding for the free variable. The name should be
439 marked CELL in this block and removed from the free list.
440
441 Note that the current block's free variables are included in free.
442 That's safe because no name can be free and local in the same scope.
443*/
444
445static int
446analyze_cells(PyObject *scope, PyObject *free)
447{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000448 PyObject *name, *v, *w;
449 int success = 0;
450 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000452 w = PyInt_FromLong(CELL);
453 if (!w)
454 return 0;
455 while (PyDict_Next(scope, &pos, &name, &v)) {
456 long flags;
457 assert(PyInt_Check(v));
458 flags = PyInt_AS_LONG(v);
459 if (flags != LOCAL)
460 continue;
461 if (!PyDict_GetItem(free, name))
462 continue;
463 /* Replace LOCAL with CELL for this name, and remove
464 from free. It is safe to replace the value of name
465 in the dict, because it will not cause a resize.
466 */
467 if (PyDict_SetItem(scope, name, w) < 0)
468 goto error;
469 if (!PyDict_DelItem(free, name) < 0)
470 goto error;
471 }
472 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000474 Py_DECREF(w);
475 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476}
477
478/* Check for illegal statements in unoptimized namespaces */
479static int
480check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000481 char buf[300];
482 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000484 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
485 || !(ste->ste_free || ste->ste_child_free))
486 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000488 trailer = (ste->ste_child_free ?
489 "contains a nested function with free variables" :
490 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000491
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000492 switch (ste->ste_unoptimized) {
493 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
494 case OPT_EXEC: /* qualified exec is fine */
495 return 1;
496 case OPT_IMPORT_STAR:
497 PyOS_snprintf(buf, sizeof(buf),
498 "import * is not allowed in function '%.100s' "
499 "because it %s",
500 PyString_AS_STRING(ste->ste_name), trailer);
501 break;
502 case OPT_BARE_EXEC:
503 PyOS_snprintf(buf, sizeof(buf),
504 "unqualified exec is not allowed in function "
505 "'%.100s' it %s",
506 PyString_AS_STRING(ste->ste_name), trailer);
507 break;
508 default:
509 PyOS_snprintf(buf, sizeof(buf),
510 "function '%.100s' uses import * and bare exec, "
511 "which are illegal because it %s",
512 PyString_AS_STRING(ste->ste_name), trailer);
513 break;
514 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516 PyErr_SetString(PyExc_SyntaxError, buf);
517 PyErr_SyntaxLocation(ste->ste_table->st_filename,
518 ste->ste_opt_lineno);
519 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520}
521
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000522/* Enter the final scope information into the st_symbols dict.
523 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 * All arguments are dicts. Modifies symbols, others are read-only.
525*/
526static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000527update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000528 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000530 PyObject *name, *v, *u, *w, *free_value = NULL;
531 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000533 while (PyDict_Next(symbols, &pos, &name, &v)) {
534 long i, flags;
535 assert(PyInt_Check(v));
536 flags = PyInt_AS_LONG(v);
537 w = PyDict_GetItem(scope, name);
538 assert(w && PyInt_Check(w));
539 i = PyInt_AS_LONG(w);
540 flags |= (i << SCOPE_OFF);
541 u = PyInt_FromLong(flags);
542 if (!u)
543 return 0;
544 if (PyDict_SetItem(symbols, name, u) < 0) {
545 Py_DECREF(u);
546 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000548 Py_DECREF(u);
549 }
550
551 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
552 if (!free_value)
553 return 0;
554
555 /* add a free variable when it's only use is for creating a closure */
556 pos = 0;
557 while (PyDict_Next(free, &pos, &name, &v)) {
558 PyObject *o = PyDict_GetItem(symbols, name);
559
560 if (o) {
561 /* It could be a free variable in a method of
562 the class that has the same name as a local
563 or global in the class scope.
564 */
565 if (classflag &&
566 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
567 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
568 o = PyInt_FromLong(i);
569 if (!o) {
570 Py_DECREF(free_value);
571 return 0;
572 }
573 if (PyDict_SetItem(symbols, name, o) < 0) {
574 Py_DECREF(o);
575 Py_DECREF(free_value);
576 return 0;
577 }
578 Py_DECREF(o);
579 }
580 /* else it's not free, probably a cell */
581 continue;
582 }
583 if (!PyDict_GetItem(bound, name))
584 continue; /* it's a global */
585
586 if (PyDict_SetItem(symbols, name, free_value) < 0) {
587 Py_DECREF(free_value);
588 return 0;
589 }
590 }
591 Py_DECREF(free_value);
592 return 1;
593}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594
595/* Make final symbol table decisions for block of ste.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000596
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 Arguments:
598 ste -- current symtable entry (input/output)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000599 bound -- set of variables bound in enclosing scopes (input). bound
600 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 free -- set of free variables in enclosed scopes (output)
602 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000603
604 The implementation uses two mutually recursive functions,
605 analyze_block() and analyze_child_block(). analyze_block() is
606 responsible for analyzing the individual names defined in a block.
607 analyze_child_block() prepares temporary namespace dictionaries
608 used to evaluated nested blocks.
609
610 The two functions exist because a child block should see the name
611 bindings of its enclosing blocks, but those bindings should not
612 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613*/
614
615static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000616analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
617 PyObject *global, PyObject* child_free);
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000618
619static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000620analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
621 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000623 PyObject *name, *v, *local = NULL, *scope = NULL;
624 PyObject *newbound = NULL, *newglobal = NULL;
625 PyObject *newfree = NULL, *allfree = NULL;
626 int i, success = 0;
627 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000629 local = PyDict_New(); /* collect new names bound in block */
630 if (!local)
631 goto error;
632 scope = PyDict_New(); /* collect scopes defined for each name */
633 if (!scope)
634 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000635
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000636 /* Allocate new global and bound variable dictionaries. These
637 dictionaries hold the names visible in nested blocks. For
638 ClassBlocks, the bound and global names are initialized
639 before analyzing names, because class bindings aren't
640 visible in methods. For other blocks, they are initialized
641 after names are analyzed.
642 */
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000643
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000644 /* TODO(jhylton): Package these dicts in a struct so that we
645 can write reasonable helper functions?
646 */
647 newglobal = PyDict_New();
648 if (!newglobal)
649 goto error;
650 newbound = PyDict_New();
651 if (!newbound)
652 goto error;
653 newfree = PyDict_New();
654 if (!newfree)
655 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000657 if (ste->ste_type == ClassBlock) {
658 if (PyDict_Update(newglobal, global) < 0)
659 goto error;
660 if (bound)
661 if (PyDict_Update(newbound, bound) < 0)
662 goto error;
663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000665 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
666 long flags = PyInt_AS_LONG(v);
667 if (!analyze_name(ste, scope, name, flags,
668 bound, local, free, global))
669 goto error;
670 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000672 if (ste->ste_type != ClassBlock) {
673 if (ste->ste_type == FunctionBlock) {
674 if (PyDict_Update(newbound, local) < 0)
675 goto error;
676 }
677 if (bound) {
678 if (PyDict_Update(newbound, bound) < 0)
679 goto error;
680 }
681 if (PyDict_Update(newglobal, global) < 0)
682 goto error;
683 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000685 /* Recursively call analyze_block() on each child block.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000686
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000687 newbound, newglobal now contain the names visible in
688 nested blocks. The free variables in the children will
689 be collected in allfree.
690 */
691 allfree = PyDict_New();
692 if (!allfree)
693 goto error;
694 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
695 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
696 PySTEntryObject* entry;
697 assert(c && PySTEntry_Check(c));
698 entry = (PySTEntryObject*)c;
699 if (!analyze_child_block(entry, newbound, newfree, newglobal,
700 allfree))
701 goto error;
702 if (entry->ste_free || entry->ste_child_free)
703 ste->ste_child_free = 1;
704 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000706 if (PyDict_Update(newfree, allfree) < 0)
707 goto error;
708 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
709 goto error;
710 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
711 ste->ste_type == ClassBlock))
712 goto error;
713 if (!check_unoptimized(ste))
714 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000716 if (PyDict_Update(free, newfree) < 0)
717 goto error;
718 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000720 Py_XDECREF(local);
721 Py_XDECREF(scope);
722 Py_XDECREF(newbound);
723 Py_XDECREF(newglobal);
724 Py_XDECREF(newfree);
725 Py_XDECREF(allfree);
726 if (!success)
727 assert(PyErr_Occurred());
728 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729}
730
731static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000732analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
733 PyObject *global, PyObject* child_free)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000734{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000735 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000736
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000737 /* Copy the bound and global dictionaries.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000738
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000739 These dictionary are used by all blocks enclosed by the
740 current block. The analyze_block() call modifies these
741 dictionaries.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000742
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000743 */
744 temp_bound = PyDict_New();
745 if (!temp_bound)
746 goto error;
747 if (PyDict_Update(temp_bound, bound) < 0)
748 goto error;
749 temp_free = PyDict_New();
750 if (!temp_free)
751 goto error;
752 if (PyDict_Update(temp_free, free) < 0)
753 goto error;
754 temp_global = PyDict_New();
755 if (!temp_global)
756 goto error;
757 if (PyDict_Update(temp_global, global) < 0)
758 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000759
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000760 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
761 goto error;
762 if (PyDict_Update(child_free, temp_free) < 0)
763 goto error;
764 Py_DECREF(temp_bound);
765 Py_DECREF(temp_free);
766 Py_DECREF(temp_global);
767 return 1;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000768 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000769 Py_XDECREF(temp_bound);
770 Py_XDECREF(temp_free);
771 Py_XDECREF(temp_global);
772 return 0;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000773}
774
775static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776symtable_analyze(struct symtable *st)
777{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000778 PyObject *free, *global;
779 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000781 free = PyDict_New();
782 if (!free)
783 return 0;
784 global = PyDict_New();
785 if (!global) {
786 Py_DECREF(free);
787 return 0;
788 }
789 r = analyze_block(st->st_top, NULL, free, global);
790 Py_DECREF(free);
791 Py_DECREF(global);
792 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793}
794
795
796static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000797symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000799 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
800 lineno, NULL, NULL) < 0) {
801 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
802 PyErr_SetString(PyExc_SyntaxError, msg);
803 PyErr_SyntaxLocation(st->st_filename,
804 st->st_cur->ste_lineno);
805 }
806 return 0;
807 }
808 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809}
810
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000811/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 This reference is released when the block is exited, via the DECREF
813 in symtable_exit_block().
814*/
815
816static int
817symtable_exit_block(struct symtable *st, void *ast)
818{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000819 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000821 Py_CLEAR(st->st_cur);
822 end = PyList_GET_SIZE(st->st_stack) - 1;
823 if (end >= 0) {
824 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
825 end);
826 if (st->st_cur == NULL)
827 return 0;
828 Py_INCREF(st->st_cur);
829 if (PySequence_DelItem(st->st_stack, end) < 0)
830 return 0;
831 }
832 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833}
834
835static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000836symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
837 void *ast, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000839 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000841 if (st->st_cur) {
842 prev = st->st_cur;
843 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
844 return 0;
845 }
846 Py_DECREF(st->st_cur);
847 }
848 st->st_cur = ste_new(st, name, block, ast, lineno);
849 if (st->st_cur == NULL)
850 return 0;
Benjamin Petersonf76942d2010-10-16 03:51:38 +0000851 if (block == ModuleBlock)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000852 st->st_global = st->st_cur->ste_symbols;
853 if (prev) {
854 if (PyList_Append(prev->ste_children,
855 (PyObject *)st->st_cur) < 0) {
856 return 0;
857 }
858 }
859 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860}
861
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000862static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863symtable_lookup(struct symtable *st, PyObject *name)
864{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000865 PyObject *o;
866 PyObject *mangled = _Py_Mangle(st->st_private, name);
867 if (!mangled)
868 return 0;
869 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
870 Py_DECREF(mangled);
871 if (!o)
872 return 0;
873 return PyInt_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874}
875
876static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000877symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000879 PyObject *o;
880 PyObject *dict;
881 long val;
882 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000884 if (!mangled)
885 return 0;
886 dict = st->st_cur->ste_symbols;
887 if ((o = PyDict_GetItem(dict, mangled))) {
888 val = PyInt_AS_LONG(o);
889 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
890 /* Is it better to use 'mangled' or 'name' here? */
891 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
892 PyString_AsString(name));
893 PyErr_SyntaxLocation(st->st_filename,
894 st->st_cur->ste_lineno);
895 goto error;
896 }
897 val |= flag;
898 } else
899 val = flag;
900 o = PyInt_FromLong(val);
901 if (o == NULL)
902 goto error;
903 if (PyDict_SetItem(dict, mangled, o) < 0) {
904 Py_DECREF(o);
905 goto error;
906 }
907 Py_DECREF(o);
908
909 if (flag & DEF_PARAM) {
910 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
911 goto error;
912 } else if (flag & DEF_GLOBAL) {
913 /* XXX need to update DEF_GLOBAL for other flags too;
914 perhaps only DEF_FREE_GLOBAL */
915 val = flag;
916 if ((o = PyDict_GetItem(st->st_global, mangled))) {
917 val |= PyInt_AS_LONG(o);
918 }
919 o = PyInt_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920 if (o == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000921 goto error;
922 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
923 Py_DECREF(o);
924 goto error;
925 }
926 Py_DECREF(o);
927 }
928 Py_DECREF(mangled);
929 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000930
931error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000932 Py_DECREF(mangled);
933 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934}
935
936/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
937 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000938 function.
939
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
941 useful if the first node in the sequence requires special treatment.
942*/
943
944#define VISIT(ST, TYPE, V) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000945 if (!symtable_visit_ ## TYPE((ST), (V))) \
946 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000947
948#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000949 if (!symtable_visit_ ## TYPE((ST), (V))) { \
950 symtable_exit_block((ST), (S)); \
951 return 0; \
952 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000953
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000955 int i; \
956 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
957 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
958 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
959 if (!symtable_visit_ ## TYPE((ST), elt)) \
960 return 0; \
961 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000963
964#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000965 int i; \
966 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
967 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
968 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
969 if (!symtable_visit_ ## TYPE((ST), elt)) { \
970 symtable_exit_block((ST), (S)); \
971 return 0; \
972 } \
973 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000974}
975
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000977 int i; \
978 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
979 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
980 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
981 if (!symtable_visit_ ## TYPE((ST), elt)) \
982 return 0; \
983 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000985
986#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000987 int i; \
988 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
989 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
990 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
991 if (!symtable_visit_ ## TYPE((ST), elt)) { \
992 symtable_exit_block((ST), (S)); \
993 return 0; \
994 } \
995 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000996}
997
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998static int
999symtable_visit_stmt(struct symtable *st, stmt_ty s)
1000{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001001 switch (s->kind) {
1002 case FunctionDef_kind:
1003 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1004 return 0;
1005 if (s->v.FunctionDef.args->defaults)
1006 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1007 if (s->v.FunctionDef.decorator_list)
1008 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1009 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1010 FunctionBlock, (void *)s, s->lineno))
1011 return 0;
1012 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1013 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1014 if (!symtable_exit_block(st, s))
1015 return 0;
1016 break;
1017 case ClassDef_kind: {
1018 PyObject *tmp;
1019 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1020 return 0;
1021 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1022 if (s->v.ClassDef.decorator_list)
1023 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1024 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1025 (void *)s, s->lineno))
1026 return 0;
1027 tmp = st->st_private;
1028 st->st_private = s->v.ClassDef.name;
1029 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1030 st->st_private = tmp;
1031 if (!symtable_exit_block(st, s))
1032 return 0;
1033 break;
1034 }
1035 case Return_kind:
1036 if (s->v.Return.value) {
1037 VISIT(st, expr, s->v.Return.value);
1038 st->st_cur->ste_returns_value = 1;
1039 if (st->st_cur->ste_generator) {
1040 PyErr_SetString(PyExc_SyntaxError,
1041 RETURN_VAL_IN_GENERATOR);
1042 PyErr_SyntaxLocation(st->st_filename,
1043 s->lineno);
1044 return 0;
1045 }
1046 }
1047 break;
1048 case Delete_kind:
1049 VISIT_SEQ(st, expr, s->v.Delete.targets);
1050 break;
1051 case Assign_kind:
1052 VISIT_SEQ(st, expr, s->v.Assign.targets);
1053 VISIT(st, expr, s->v.Assign.value);
1054 break;
1055 case AugAssign_kind:
1056 VISIT(st, expr, s->v.AugAssign.target);
1057 VISIT(st, expr, s->v.AugAssign.value);
1058 break;
1059 case Print_kind:
1060 if (s->v.Print.dest)
1061 VISIT(st, expr, s->v.Print.dest);
1062 VISIT_SEQ(st, expr, s->v.Print.values);
1063 break;
1064 case For_kind:
1065 VISIT(st, expr, s->v.For.target);
1066 VISIT(st, expr, s->v.For.iter);
1067 VISIT_SEQ(st, stmt, s->v.For.body);
1068 if (s->v.For.orelse)
1069 VISIT_SEQ(st, stmt, s->v.For.orelse);
1070 break;
1071 case While_kind:
1072 VISIT(st, expr, s->v.While.test);
1073 VISIT_SEQ(st, stmt, s->v.While.body);
1074 if (s->v.While.orelse)
1075 VISIT_SEQ(st, stmt, s->v.While.orelse);
1076 break;
1077 case If_kind:
1078 /* XXX if 0: and lookup_yield() hacks */
1079 VISIT(st, expr, s->v.If.test);
1080 VISIT_SEQ(st, stmt, s->v.If.body);
1081 if (s->v.If.orelse)
1082 VISIT_SEQ(st, stmt, s->v.If.orelse);
1083 break;
1084 case Raise_kind:
1085 if (s->v.Raise.type) {
1086 VISIT(st, expr, s->v.Raise.type);
1087 if (s->v.Raise.inst) {
1088 VISIT(st, expr, s->v.Raise.inst);
1089 if (s->v.Raise.tback)
1090 VISIT(st, expr, s->v.Raise.tback);
1091 }
1092 }
1093 break;
1094 case TryExcept_kind:
1095 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1096 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1097 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1098 break;
1099 case TryFinally_kind:
1100 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1101 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1102 break;
1103 case Assert_kind:
1104 VISIT(st, expr, s->v.Assert.test);
1105 if (s->v.Assert.msg)
1106 VISIT(st, expr, s->v.Assert.msg);
1107 break;
1108 case Import_kind:
1109 VISIT_SEQ(st, alias, s->v.Import.names);
1110 /* XXX Don't have the lineno available inside
1111 visit_alias */
1112 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1113 st->st_cur->ste_opt_lineno = s->lineno;
1114 break;
1115 case ImportFrom_kind:
1116 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1117 /* XXX Don't have the lineno available inside
1118 visit_alias */
1119 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1120 st->st_cur->ste_opt_lineno = s->lineno;
1121 break;
1122 case Exec_kind:
1123 VISIT(st, expr, s->v.Exec.body);
1124 if (!st->st_cur->ste_opt_lineno)
1125 st->st_cur->ste_opt_lineno = s->lineno;
1126 if (s->v.Exec.globals) {
1127 st->st_cur->ste_unoptimized |= OPT_EXEC;
1128 VISIT(st, expr, s->v.Exec.globals);
1129 if (s->v.Exec.locals)
1130 VISIT(st, expr, s->v.Exec.locals);
1131 } else {
1132 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1133 }
1134 break;
1135 case Global_kind: {
1136 int i;
1137 asdl_seq *seq = s->v.Global.names;
1138 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1139 identifier name = (identifier)asdl_seq_GET(seq, i);
1140 char *c_name = PyString_AS_STRING(name);
1141 long cur = symtable_lookup(st, name);
1142 if (cur < 0)
1143 return 0;
1144 if (cur & (DEF_LOCAL | USE)) {
1145 char buf[256];
1146 if (cur & DEF_LOCAL)
1147 PyOS_snprintf(buf, sizeof(buf),
1148 GLOBAL_AFTER_ASSIGN,
1149 c_name);
1150 else
1151 PyOS_snprintf(buf, sizeof(buf),
1152 GLOBAL_AFTER_USE,
1153 c_name);
1154 if (!symtable_warn(st, buf, s->lineno))
1155 return 0;
1156 }
1157 if (!symtable_add_def(st, name, DEF_GLOBAL))
1158 return 0;
1159 }
1160 break;
1161 }
1162 case Expr_kind:
1163 VISIT(st, expr, s->v.Expr.value);
1164 break;
1165 case Pass_kind:
1166 case Break_kind:
1167 case Continue_kind:
1168 /* nothing to do here */
1169 break;
1170 case With_kind:
1171 VISIT(st, expr, s->v.With.context_expr);
1172 if (s->v.With.optional_vars) {
1173 VISIT(st, expr, s->v.With.optional_vars);
1174 }
1175 VISIT_SEQ(st, stmt, s->v.With.body);
1176 break;
1177 }
1178 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179}
1180
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001181static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182symtable_visit_expr(struct symtable *st, expr_ty e)
1183{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001184 switch (e->kind) {
1185 case BoolOp_kind:
1186 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1187 break;
1188 case BinOp_kind:
1189 VISIT(st, expr, e->v.BinOp.left);
1190 VISIT(st, expr, e->v.BinOp.right);
1191 break;
1192 case UnaryOp_kind:
1193 VISIT(st, expr, e->v.UnaryOp.operand);
1194 break;
1195 case Lambda_kind: {
1196 if (!GET_IDENTIFIER(lambda))
1197 return 0;
1198 if (e->v.Lambda.args->defaults)
1199 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1200 if (!symtable_enter_block(st, lambda,
1201 FunctionBlock, (void *)e, e->lineno))
1202 return 0;
1203 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1204 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1205 if (!symtable_exit_block(st, (void *)e))
1206 return 0;
1207 break;
1208 }
1209 case IfExp_kind:
1210 VISIT(st, expr, e->v.IfExp.test);
1211 VISIT(st, expr, e->v.IfExp.body);
1212 VISIT(st, expr, e->v.IfExp.orelse);
1213 break;
1214 case Dict_kind:
1215 VISIT_SEQ(st, expr, e->v.Dict.keys);
1216 VISIT_SEQ(st, expr, e->v.Dict.values);
1217 break;
1218 case Set_kind:
1219 VISIT_SEQ(st, expr, e->v.Set.elts);
1220 break;
1221 case ListComp_kind:
1222 VISIT(st, expr, e->v.ListComp.elt);
1223 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1224 break;
1225 case GeneratorExp_kind:
1226 if (!symtable_visit_genexp(st, e))
1227 return 0;
1228 break;
1229 case SetComp_kind:
1230 if (!symtable_visit_setcomp(st, e))
1231 return 0;
1232 break;
1233 case DictComp_kind:
1234 if (!symtable_visit_dictcomp(st, e))
1235 return 0;
1236 break;
1237 case Yield_kind:
1238 if (e->v.Yield.value)
1239 VISIT(st, expr, e->v.Yield.value);
1240 st->st_cur->ste_generator = 1;
1241 if (st->st_cur->ste_returns_value) {
1242 PyErr_SetString(PyExc_SyntaxError,
1243 RETURN_VAL_IN_GENERATOR);
1244 PyErr_SyntaxLocation(st->st_filename,
1245 e->lineno);
1246 return 0;
1247 }
1248 break;
1249 case Compare_kind:
1250 VISIT(st, expr, e->v.Compare.left);
1251 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1252 break;
1253 case Call_kind:
1254 VISIT(st, expr, e->v.Call.func);
1255 VISIT_SEQ(st, expr, e->v.Call.args);
1256 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1257 if (e->v.Call.starargs)
1258 VISIT(st, expr, e->v.Call.starargs);
1259 if (e->v.Call.kwargs)
1260 VISIT(st, expr, e->v.Call.kwargs);
1261 break;
1262 case Repr_kind:
1263 VISIT(st, expr, e->v.Repr.value);
1264 break;
1265 case Num_kind:
1266 case Str_kind:
1267 /* Nothing to do here. */
1268 break;
1269 /* The following exprs can be assignment targets. */
1270 case Attribute_kind:
1271 VISIT(st, expr, e->v.Attribute.value);
1272 break;
1273 case Subscript_kind:
1274 VISIT(st, expr, e->v.Subscript.value);
1275 VISIT(st, slice, e->v.Subscript.slice);
1276 break;
1277 case Name_kind:
1278 if (!symtable_add_def(st, e->v.Name.id,
1279 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1280 return 0;
1281 break;
1282 /* child nodes of List and Tuple will have expr_context set */
1283 case List_kind:
1284 VISIT_SEQ(st, expr, e->v.List.elts);
1285 break;
1286 case Tuple_kind:
1287 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1288 break;
1289 }
1290 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291}
1292
1293static int
1294symtable_implicit_arg(struct symtable *st, int pos)
1295{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001296 PyObject *id = PyString_FromFormat(".%d", pos);
1297 if (id == NULL)
1298 return 0;
1299 if (!symtable_add_def(st, id, DEF_PARAM)) {
1300 Py_DECREF(id);
1301 return 0;
1302 }
1303 Py_DECREF(id);
1304 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305}
1306
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001307static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1309{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001310 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001312 /* go through all the toplevel arguments first */
1313 for (i = 0; i < asdl_seq_LEN(args); i++) {
1314 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1315 if (arg->kind == Name_kind) {
1316 assert(arg->v.Name.ctx == Param ||
1317 (arg->v.Name.ctx == Store && !toplevel));
1318 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1319 return 0;
1320 }
1321 else if (arg->kind == Tuple_kind) {
1322 assert(arg->v.Tuple.ctx == Store);
1323 if (toplevel) {
1324 if (!symtable_implicit_arg(st, i))
1325 return 0;
1326 }
1327 }
1328 else {
1329 PyErr_SetString(PyExc_SyntaxError,
1330 "invalid expression in parameter list");
1331 PyErr_SyntaxLocation(st->st_filename,
1332 st->st_cur->ste_lineno);
1333 return 0;
1334 }
1335 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001337 if (!toplevel) {
1338 if (!symtable_visit_params_nested(st, args))
1339 return 0;
1340 }
1341
1342 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343}
1344
1345static int
1346symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1347{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001348 int i;
1349 for (i = 0; i < asdl_seq_LEN(args); i++) {
1350 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1351 if (arg->kind == Tuple_kind &&
1352 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1353 return 0;
1354 }
1355
1356 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357}
1358
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001359static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360symtable_visit_arguments(struct symtable *st, arguments_ty a)
1361{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001362 /* skip default arguments inside function block
1363 XXX should ast be different?
1364 */
1365 if (a->args && !symtable_visit_params(st, a->args, 1))
1366 return 0;
1367 if (a->vararg) {
1368 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1369 return 0;
1370 st->st_cur->ste_varargs = 1;
1371 }
1372 if (a->kwarg) {
1373 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1374 return 0;
1375 st->st_cur->ste_varkeywords = 1;
1376 }
1377 if (a->args && !symtable_visit_params_nested(st, a->args))
1378 return 0;
1379 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380}
1381
1382
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001383static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1385{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001386 if (eh->v.ExceptHandler.type)
1387 VISIT(st, expr, eh->v.ExceptHandler.type);
1388 if (eh->v.ExceptHandler.name)
1389 VISIT(st, expr, eh->v.ExceptHandler.name);
1390 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1391 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392}
1393
1394
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001395static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396symtable_visit_alias(struct symtable *st, alias_ty a)
1397{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001398 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc6660cf2010-06-11 21:40:37 +00001399 operation. It is different than a->name when a->name is a
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001400 dotted package name (e.g. spam.eggs)
1401 */
1402 PyObject *store_name;
1403 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1404 const char *base = PyString_AS_STRING(name);
1405 char *dot = strchr(base, '.');
1406 if (dot) {
1407 store_name = PyString_FromStringAndSize(base, dot - base);
1408 if (!store_name)
1409 return 0;
1410 }
1411 else {
1412 store_name = name;
1413 Py_INCREF(store_name);
1414 }
1415 if (strcmp(PyString_AS_STRING(name), "*")) {
1416 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1417 Py_DECREF(store_name);
1418 return r;
1419 }
1420 else {
1421 if (st->st_cur->ste_type != ModuleBlock) {
1422 int lineno = st->st_cur->ste_lineno;
1423 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
1424 Py_DECREF(store_name);
1425 return 0;
1426 }
1427 }
1428 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1429 Py_DECREF(store_name);
1430 return 1;
1431 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432}
1433
1434
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001435static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1437{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001438 VISIT(st, expr, lc->target);
1439 VISIT(st, expr, lc->iter);
1440 VISIT_SEQ(st, expr, lc->ifs);
1441 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442}
1443
1444
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001445static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446symtable_visit_keyword(struct symtable *st, keyword_ty k)
1447{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001448 VISIT(st, expr, k->value);
1449 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450}
1451
1452
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001453static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454symtable_visit_slice(struct symtable *st, slice_ty s)
1455{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001456 switch (s->kind) {
1457 case Slice_kind:
1458 if (s->v.Slice.lower)
1459 VISIT(st, expr, s->v.Slice.lower)
1460 if (s->v.Slice.upper)
1461 VISIT(st, expr, s->v.Slice.upper)
1462 if (s->v.Slice.step)
1463 VISIT(st, expr, s->v.Slice.step)
1464 break;
1465 case ExtSlice_kind:
1466 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1467 break;
1468 case Index_kind:
1469 VISIT(st, expr, s->v.Index.value)
1470 break;
1471 case Ellipsis_kind:
1472 break;
1473 }
1474 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475}
1476
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001477static int
1478symtable_new_tmpname(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001480 char tmpname[256];
1481 identifier tmp;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001482
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001483 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1484 ++st->st_cur->ste_tmpname);
1485 tmp = PyString_InternFromString(tmpname);
1486 if (!tmp)
1487 return 0;
1488 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1489 return 0;
1490 Py_DECREF(tmp);
1491 return 1;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001492}
1493
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001494static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001495symtable_handle_comprehension(struct symtable *st, expr_ty e,
1496 identifier scope_name, asdl_seq *generators,
1497 expr_ty elt, expr_ty value)
1498{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001499 int is_generator = (e->kind == GeneratorExp_kind);
1500 int needs_tmp = !is_generator;
1501 comprehension_ty outermost = ((comprehension_ty)
1502 asdl_seq_GET(generators, 0));
1503 /* Outermost iterator is evaluated in current scope */
1504 VISIT(st, expr, outermost->iter);
1505 /* Create comprehension scope for the rest */
1506 if (!scope_name ||
1507 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
1508 return 0;
1509 }
1510 st->st_cur->ste_generator = is_generator;
1511 /* Outermost iter is received as an argument */
1512 if (!symtable_implicit_arg(st, 0)) {
1513 symtable_exit_block(st, (void *)e);
1514 return 0;
1515 }
1516 /* Allocate temporary name if needed */
1517 if (needs_tmp && !symtable_new_tmpname(st)) {
1518 symtable_exit_block(st, (void *)e);
1519 return 0;
1520 }
1521 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1522 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1523 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1524 generators, 1, (void*)e);
1525 if (value)
1526 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1527 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1528 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529}
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001530
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001531static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001532symtable_visit_genexp(struct symtable *st, expr_ty e)
1533{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001534 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1535 e->v.GeneratorExp.generators,
1536 e->v.GeneratorExp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001537}
1538
1539static int
1540symtable_visit_setcomp(struct symtable *st, expr_ty e)
1541{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001542 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1543 e->v.SetComp.generators,
1544 e->v.SetComp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001545}
1546
1547static int
1548symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1549{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001550 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1551 e->v.DictComp.generators,
1552 e->v.DictComp.key,
1553 e->v.DictComp.value);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001554}