blob: f0f1d4d07dc2492a0d001b9407827b59c31a77b3 [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;
Christian Heimes8c1bce02012-09-10 03:08:46 +020025 PyObject *k = NULL;
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:
Christian Heimes8c1bce02012-09-10 03:08:46 +020078 Py_XDECREF(k);
Antoine Pitrouc83ea132010-05-09 14:46:46 +000079 Py_XDECREF(ste);
80 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000081}
82
83static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000084ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000086 char buf[256];
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000087
Antoine Pitrouc83ea132010-05-09 14:46:46 +000088 PyOS_snprintf(buf, sizeof(buf),
89 "<symtable entry %.100s(%ld), line %d>",
90 PyString_AS_STRING(ste->ste_name),
91 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
92 return PyString_FromString(buf);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093}
94
95static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000098 ste->ste_table = NULL;
99 Py_XDECREF(ste->ste_id);
100 Py_XDECREF(ste->ste_name);
101 Py_XDECREF(ste->ste_symbols);
102 Py_XDECREF(ste->ste_varnames);
103 Py_XDECREF(ste->ste_children);
104 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000105}
106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108
Guido van Rossum6f799372001-09-20 20:46:19 +0000109static PyMemberDef ste_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000110 {"id", T_OBJECT, OFF(ste_id), READONLY},
111 {"name", T_OBJECT, OFF(ste_name), READONLY},
112 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
113 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
114 {"children", T_OBJECT, OFF(ste_children), READONLY},
115 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
116 {"nested", T_INT, OFF(ste_nested), READONLY},
117 {"type", T_INT, OFF(ste_type), READONLY},
118 {"lineno", T_INT, OFF(ste_lineno), READONLY},
119 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120};
121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122PyTypeObject PySTEntry_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000123 PyVarObject_HEAD_INIT(&PyType_Type, 0)
124 "symtable entry",
125 sizeof(PySTEntryObject),
126 0,
127 (destructor)ste_dealloc, /* tp_dealloc */
128 0, /* tp_print */
129 0, /* tp_getattr */
130 0, /* tp_setattr */
131 0, /* tp_compare */
132 (reprfunc)ste_repr, /* tp_repr */
133 0, /* tp_as_number */
134 0, /* tp_as_sequence */
135 0, /* tp_as_mapping */
136 0, /* tp_hash */
137 0, /* tp_call */
138 0, /* tp_str */
139 PyObject_GenericGetAttr, /* tp_getattro */
140 0, /* tp_setattro */
141 0, /* tp_as_buffer */
142 Py_TPFLAGS_DEFAULT, /* tp_flags */
143 0, /* tp_doc */
144 0, /* tp_traverse */
145 0, /* tp_clear */
146 0, /* tp_richcompare */
147 0, /* tp_weaklistoffset */
148 0, /* tp_iter */
149 0, /* tp_iternext */
150 0, /* tp_methods */
151 ste_memberlist, /* tp_members */
152 0, /* tp_getset */
153 0, /* tp_base */
154 0, /* tp_dict */
155 0, /* tp_descr_get */
156 0, /* tp_descr_set */
157 0, /* tp_dictoffset */
158 0, /* tp_init */
159 0, /* tp_alloc */
160 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000161};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162
163static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000164static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000165static int symtable_enter_block(struct symtable *st, identifier name,
166 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_exit_block(struct symtable *st, void *ast);
168static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
169static int symtable_visit_expr(struct symtable *st, expr_ty s);
170static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000171static int symtable_visit_setcomp(struct symtable *st, expr_ty e);
172static int symtable_visit_dictcomp(struct symtable *st, expr_ty e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173static int symtable_visit_arguments(struct symtable *st, arguments_ty);
174static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
175static int symtable_visit_alias(struct symtable *st, alias_ty);
176static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
177static int symtable_visit_keyword(struct symtable *st, keyword_ty);
178static int symtable_visit_slice(struct symtable *st, slice_ty);
179static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
180static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
181static int symtable_implicit_arg(struct symtable *st, int pos);
182
183
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000184static identifier top = NULL, lambda = NULL, genexpr = NULL, setcomp = NULL,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000185 dictcomp = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186
187#define GET_IDENTIFIER(VAR) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000188 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189
190#define DUPLICATE_ARGUMENT \
191"duplicate argument '%s' in function definition"
192
193static struct symtable *
194symtable_new(void)
195{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000196 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000198 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
199 if (st == NULL)
200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000202 st->st_filename = NULL;
203 st->st_symbols = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000204
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000205 if ((st->st_stack = PyList_New(0)) == NULL)
206 goto fail;
207 if ((st->st_symbols = PyDict_New()) == NULL)
208 goto fail;
209 st->st_cur = NULL;
210 st->st_private = NULL;
211 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000213 PySymtable_Free(st);
214 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215}
216
217struct symtable *
218PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
219{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000220 struct symtable *st = symtable_new();
221 asdl_seq *seq;
222 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000224 if (st == NULL)
225 return st;
226 st->st_filename = filename;
227 st->st_future = future;
228 if (!GET_IDENTIFIER(top) ||
229 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
230 PySymtable_Free(st);
231 return NULL;
232 }
Neal Norwitzd12bd012006-07-21 07:59:47 +0000233
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000234 st->st_top = st->st_cur;
235 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
236 /* Any other top-level initialization? */
237 switch (mod->kind) {
238 case Module_kind:
239 seq = mod->v.Module.body;
240 for (i = 0; i < asdl_seq_LEN(seq); i++)
241 if (!symtable_visit_stmt(st,
242 (stmt_ty)asdl_seq_GET(seq, i)))
243 goto error;
244 break;
245 case Expression_kind:
246 if (!symtable_visit_expr(st, mod->v.Expression.body))
247 goto error;
248 break;
249 case Interactive_kind:
250 seq = mod->v.Interactive.body;
251 for (i = 0; i < asdl_seq_LEN(seq); i++)
252 if (!symtable_visit_stmt(st,
253 (stmt_ty)asdl_seq_GET(seq, i)))
254 goto error;
255 break;
256 case Suite_kind:
257 PyErr_SetString(PyExc_RuntimeError,
258 "this compiler does not handle Suites");
259 goto error;
260 }
261 if (!symtable_exit_block(st, (void *)mod)) {
262 PySymtable_Free(st);
263 return NULL;
264 }
265 if (symtable_analyze(st))
266 return st;
267 PySymtable_Free(st);
268 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000270 (void) symtable_exit_block(st, (void *)mod);
271 PySymtable_Free(st);
272 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273}
274
275void
276PySymtable_Free(struct symtable *st)
277{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000278 Py_XDECREF(st->st_symbols);
279 Py_XDECREF(st->st_stack);
280 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281}
282
283PySTEntryObject *
284PySymtable_Lookup(struct symtable *st, void *key)
285{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000286 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000288 k = PyLong_FromVoidPtr(key);
289 if (k == NULL)
290 return NULL;
291 v = PyDict_GetItem(st->st_symbols, k);
292 if (v) {
293 assert(PySTEntry_Check(v));
294 Py_INCREF(v);
295 }
296 else {
297 PyErr_SetString(PyExc_KeyError,
298 "unknown symbol table entry");
299 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000300
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 Py_DECREF(k);
302 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303}
304
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000305int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306PyST_GetScope(PySTEntryObject *ste, PyObject *name)
307{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000308 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
309 if (!v)
310 return 0;
311 assert(PyInt_Check(v));
312 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313}
314
315
316/* Analyze raw symbol information to determine scope of each name.
317
318 The next several functions are helpers for PySymtable_Analyze(),
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000319 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 it determines which local variables are cell variables; they provide
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000321 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000323 There are also two kinds of free variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324 explicit global is declared with the global statement. An implicit
325 global is a free variable for which the compiler has found no binding
326 in an enclosing function scope. The implicit global is either a global
327 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
328 to handle these names to implement slightly odd semantics. In such a
329 block, the name is treated as global until it is assigned to; then it
330 is treated as a local.
331
332 The symbol table requires two passes to determine the scope of each name.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000333 The first pass collects raw facts from the AST: the name is a parameter
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334 here, the name is used by not defined here, etc. The second pass analyzes
335 these facts during a pass over the PySTEntryObjects created during pass 1.
336
337 When a function is entered during the second pass, the parent passes
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000338 the set of all name bindings visible to its children. These bindings
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339 are used to determine if the variable is free or an implicit global.
340 After doing the local analysis, it analyzes each of its child blocks
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 using an updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000343 The children update the free variable set. If a local variable is free
344 in a child, the variable is marked as a cell. The current function must
345 provide runtime storage for the variable that may outlive the function's
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346 frame. Cell variables are removed from the free set before the analyze
347 function returns to its parent.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000348
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349 The sets of bound and free variables are implemented as dictionaries
350 mapping strings to None.
351*/
352
353#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000354 PyObject *o = PyInt_FromLong(I); \
355 if (!o) \
356 return 0; \
357 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
358 Py_DECREF(o); \
359 return 0; \
360 } \
361 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362}
363
364/* Decide on scope of name, given flags.
365
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000366 The namespace dictionaries may be modified to record information
367 about the new name. For example, a new global will add an entry to
368 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369*/
370
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000371static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000372analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000373 PyObject *bound, PyObject *local, PyObject *free,
374 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000376 if (flags & DEF_GLOBAL) {
377 if (flags & DEF_PARAM) {
378 PyErr_Format(PyExc_SyntaxError,
379 "name '%s' is local and global",
380 PyString_AS_STRING(name));
381 PyErr_SyntaxLocation(ste->ste_table->st_filename,
382 ste->ste_lineno);
383
384 return 0;
385 }
386 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
387 if (PyDict_SetItem(global, name, Py_None) < 0)
388 return 0;
389 if (bound && PyDict_GetItem(bound, name)) {
390 if (PyDict_DelItem(bound, name) < 0)
391 return 0;
392 }
393 return 1;
394 }
395 if (flags & DEF_BOUND) {
396 SET_SCOPE(dict, name, LOCAL);
397 if (PyDict_SetItem(local, name, Py_None) < 0)
398 return 0;
399 if (PyDict_GetItem(global, name)) {
400 if (PyDict_DelItem(global, name) < 0)
401 return 0;
402 }
403 return 1;
404 }
405 /* If an enclosing block has a binding for this name, it
406 is a free variable rather than a global variable.
407 Note that having a non-NULL bound implies that the block
408 is nested.
409 */
410 if (bound && PyDict_GetItem(bound, name)) {
411 SET_SCOPE(dict, name, FREE);
412 ste->ste_free = 1;
413 if (PyDict_SetItem(free, name, Py_None) < 0)
414 return 0;
415 return 1;
416 }
417 /* If a parent has a global statement, then call it global
418 explicit? It could also be global implicit.
419 */
420 else if (global && PyDict_GetItem(global, name)) {
421 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
422 return 1;
423 }
424 else {
425 if (ste->ste_nested)
426 ste->ste_free = 1;
427 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
428 return 1;
429 }
430 /* Should never get here. */
431 PyErr_Format(PyExc_SystemError, "failed to set scope for %s",
432 PyString_AS_STRING(name));
433 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434}
435
436#undef SET_SCOPE
437
438/* If a name is defined in free and also in locals, then this block
439 provides the binding for the free variable. The name should be
440 marked CELL in this block and removed from the free list.
441
442 Note that the current block's free variables are included in free.
443 That's safe because no name can be free and local in the same scope.
444*/
445
446static int
447analyze_cells(PyObject *scope, PyObject *free)
448{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000449 PyObject *name, *v, *w;
450 int success = 0;
451 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000453 w = PyInt_FromLong(CELL);
454 if (!w)
455 return 0;
456 while (PyDict_Next(scope, &pos, &name, &v)) {
457 long flags;
458 assert(PyInt_Check(v));
459 flags = PyInt_AS_LONG(v);
460 if (flags != LOCAL)
461 continue;
462 if (!PyDict_GetItem(free, name))
463 continue;
464 /* Replace LOCAL with CELL for this name, and remove
465 from free. It is safe to replace the value of name
466 in the dict, because it will not cause a resize.
467 */
468 if (PyDict_SetItem(scope, name, w) < 0)
469 goto error;
470 if (!PyDict_DelItem(free, name) < 0)
471 goto error;
472 }
473 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000475 Py_DECREF(w);
476 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477}
478
479/* Check for illegal statements in unoptimized namespaces */
480static int
481check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000482 char buf[300];
483 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000485 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
486 || !(ste->ste_free || ste->ste_child_free))
487 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000489 trailer = (ste->ste_child_free ?
490 "contains a nested function with free variables" :
491 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000493 switch (ste->ste_unoptimized) {
494 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
495 case OPT_EXEC: /* qualified exec is fine */
496 return 1;
497 case OPT_IMPORT_STAR:
498 PyOS_snprintf(buf, sizeof(buf),
499 "import * is not allowed in function '%.100s' "
500 "because it %s",
501 PyString_AS_STRING(ste->ste_name), trailer);
502 break;
503 case OPT_BARE_EXEC:
504 PyOS_snprintf(buf, sizeof(buf),
505 "unqualified exec is not allowed in function "
506 "'%.100s' it %s",
507 PyString_AS_STRING(ste->ste_name), trailer);
508 break;
509 default:
510 PyOS_snprintf(buf, sizeof(buf),
511 "function '%.100s' uses import * and bare exec, "
512 "which are illegal because it %s",
513 PyString_AS_STRING(ste->ste_name), trailer);
514 break;
515 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000517 PyErr_SetString(PyExc_SyntaxError, buf);
518 PyErr_SyntaxLocation(ste->ste_table->st_filename,
519 ste->ste_opt_lineno);
520 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521}
522
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000523/* Enter the final scope information into the st_symbols dict.
524 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525 * All arguments are dicts. Modifies symbols, others are read-only.
526*/
527static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000528update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000529 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000531 PyObject *name, *v, *u, *w, *free_value = NULL;
532 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000534 while (PyDict_Next(symbols, &pos, &name, &v)) {
535 long i, flags;
536 assert(PyInt_Check(v));
537 flags = PyInt_AS_LONG(v);
538 w = PyDict_GetItem(scope, name);
539 assert(w && PyInt_Check(w));
540 i = PyInt_AS_LONG(w);
541 flags |= (i << SCOPE_OFF);
542 u = PyInt_FromLong(flags);
543 if (!u)
544 return 0;
545 if (PyDict_SetItem(symbols, name, u) < 0) {
546 Py_DECREF(u);
547 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000549 Py_DECREF(u);
550 }
551
552 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
553 if (!free_value)
554 return 0;
555
556 /* add a free variable when it's only use is for creating a closure */
557 pos = 0;
558 while (PyDict_Next(free, &pos, &name, &v)) {
559 PyObject *o = PyDict_GetItem(symbols, name);
560
561 if (o) {
562 /* It could be a free variable in a method of
563 the class that has the same name as a local
564 or global in the class scope.
565 */
566 if (classflag &&
567 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
568 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
569 o = PyInt_FromLong(i);
570 if (!o) {
571 Py_DECREF(free_value);
572 return 0;
573 }
574 if (PyDict_SetItem(symbols, name, o) < 0) {
575 Py_DECREF(o);
576 Py_DECREF(free_value);
577 return 0;
578 }
579 Py_DECREF(o);
580 }
581 /* else it's not free, probably a cell */
582 continue;
583 }
584 if (!PyDict_GetItem(bound, name))
585 continue; /* it's a global */
586
587 if (PyDict_SetItem(symbols, name, free_value) < 0) {
588 Py_DECREF(free_value);
589 return 0;
590 }
591 }
592 Py_DECREF(free_value);
593 return 1;
594}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
596/* Make final symbol table decisions for block of ste.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000597
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 Arguments:
599 ste -- current symtable entry (input/output)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000600 bound -- set of variables bound in enclosing scopes (input). bound
601 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 free -- set of free variables in enclosed scopes (output)
603 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000604
605 The implementation uses two mutually recursive functions,
606 analyze_block() and analyze_child_block(). analyze_block() is
607 responsible for analyzing the individual names defined in a block.
608 analyze_child_block() prepares temporary namespace dictionaries
609 used to evaluated nested blocks.
610
611 The two functions exist because a child block should see the name
612 bindings of its enclosing blocks, but those bindings should not
613 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614*/
615
616static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000617analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
618 PyObject *global, PyObject* child_free);
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000619
620static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000621analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
622 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000624 PyObject *name, *v, *local = NULL, *scope = NULL;
625 PyObject *newbound = NULL, *newglobal = NULL;
626 PyObject *newfree = NULL, *allfree = NULL;
627 int i, success = 0;
628 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000630 local = PyDict_New(); /* collect new names bound in block */
631 if (!local)
632 goto error;
633 scope = PyDict_New(); /* collect scopes defined for each name */
634 if (!scope)
635 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000636
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000637 /* Allocate new global and bound variable dictionaries. These
638 dictionaries hold the names visible in nested blocks. For
639 ClassBlocks, the bound and global names are initialized
640 before analyzing names, because class bindings aren't
641 visible in methods. For other blocks, they are initialized
642 after names are analyzed.
643 */
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000644
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000645 /* TODO(jhylton): Package these dicts in a struct so that we
646 can write reasonable helper functions?
647 */
648 newglobal = PyDict_New();
649 if (!newglobal)
650 goto error;
651 newbound = PyDict_New();
652 if (!newbound)
653 goto error;
654 newfree = PyDict_New();
655 if (!newfree)
656 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000658 if (ste->ste_type == ClassBlock) {
659 if (PyDict_Update(newglobal, global) < 0)
660 goto error;
661 if (bound)
662 if (PyDict_Update(newbound, bound) < 0)
663 goto error;
664 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000666 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
667 long flags = PyInt_AS_LONG(v);
668 if (!analyze_name(ste, scope, name, flags,
669 bound, local, free, global))
670 goto error;
671 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000673 if (ste->ste_type != ClassBlock) {
674 if (ste->ste_type == FunctionBlock) {
675 if (PyDict_Update(newbound, local) < 0)
676 goto error;
677 }
678 if (bound) {
679 if (PyDict_Update(newbound, bound) < 0)
680 goto error;
681 }
682 if (PyDict_Update(newglobal, global) < 0)
683 goto error;
684 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000686 /* Recursively call analyze_block() on each child block.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000687
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000688 newbound, newglobal now contain the names visible in
689 nested blocks. The free variables in the children will
690 be collected in allfree.
691 */
692 allfree = PyDict_New();
693 if (!allfree)
694 goto error;
695 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
696 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
697 PySTEntryObject* entry;
698 assert(c && PySTEntry_Check(c));
699 entry = (PySTEntryObject*)c;
700 if (!analyze_child_block(entry, newbound, newfree, newglobal,
701 allfree))
702 goto error;
703 if (entry->ste_free || entry->ste_child_free)
704 ste->ste_child_free = 1;
705 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000707 if (PyDict_Update(newfree, allfree) < 0)
708 goto error;
709 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
710 goto error;
711 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
712 ste->ste_type == ClassBlock))
713 goto error;
714 if (!check_unoptimized(ste))
715 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000717 if (PyDict_Update(free, newfree) < 0)
718 goto error;
719 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000721 Py_XDECREF(local);
722 Py_XDECREF(scope);
723 Py_XDECREF(newbound);
724 Py_XDECREF(newglobal);
725 Py_XDECREF(newfree);
726 Py_XDECREF(allfree);
727 if (!success)
728 assert(PyErr_Occurred());
729 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730}
731
732static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000733analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
734 PyObject *global, PyObject* child_free)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000735{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000736 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000737
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000738 /* Copy the bound and global dictionaries.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000739
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000740 These dictionary are used by all blocks enclosed by the
741 current block. The analyze_block() call modifies these
742 dictionaries.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000743
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000744 */
745 temp_bound = PyDict_New();
746 if (!temp_bound)
747 goto error;
748 if (PyDict_Update(temp_bound, bound) < 0)
749 goto error;
750 temp_free = PyDict_New();
751 if (!temp_free)
752 goto error;
753 if (PyDict_Update(temp_free, free) < 0)
754 goto error;
755 temp_global = PyDict_New();
756 if (!temp_global)
757 goto error;
758 if (PyDict_Update(temp_global, global) < 0)
759 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000760
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000761 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
762 goto error;
763 if (PyDict_Update(child_free, temp_free) < 0)
764 goto error;
765 Py_DECREF(temp_bound);
766 Py_DECREF(temp_free);
767 Py_DECREF(temp_global);
768 return 1;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000769 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000770 Py_XDECREF(temp_bound);
771 Py_XDECREF(temp_free);
772 Py_XDECREF(temp_global);
773 return 0;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000774}
775
776static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777symtable_analyze(struct symtable *st)
778{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000779 PyObject *free, *global;
780 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000782 free = PyDict_New();
783 if (!free)
784 return 0;
785 global = PyDict_New();
786 if (!global) {
787 Py_DECREF(free);
788 return 0;
789 }
790 r = analyze_block(st->st_top, NULL, free, global);
791 Py_DECREF(free);
792 Py_DECREF(global);
793 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794}
795
796
797static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000798symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000800 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
801 lineno, NULL, NULL) < 0) {
802 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
803 PyErr_SetString(PyExc_SyntaxError, msg);
804 PyErr_SyntaxLocation(st->st_filename,
805 st->st_cur->ste_lineno);
806 }
807 return 0;
808 }
809 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810}
811
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000812/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 This reference is released when the block is exited, via the DECREF
814 in symtable_exit_block().
815*/
816
817static int
818symtable_exit_block(struct symtable *st, void *ast)
819{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000820 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000822 Py_CLEAR(st->st_cur);
823 end = PyList_GET_SIZE(st->st_stack) - 1;
824 if (end >= 0) {
825 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
826 end);
827 if (st->st_cur == NULL)
828 return 0;
829 Py_INCREF(st->st_cur);
830 if (PySequence_DelItem(st->st_stack, end) < 0)
831 return 0;
832 }
833 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834}
835
836static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000837symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
838 void *ast, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000840 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000842 if (st->st_cur) {
843 prev = st->st_cur;
844 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
845 return 0;
846 }
847 Py_DECREF(st->st_cur);
848 }
849 st->st_cur = ste_new(st, name, block, ast, lineno);
850 if (st->st_cur == NULL)
851 return 0;
Benjamin Petersonf76942d2010-10-16 03:51:38 +0000852 if (block == ModuleBlock)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000853 st->st_global = st->st_cur->ste_symbols;
854 if (prev) {
855 if (PyList_Append(prev->ste_children,
856 (PyObject *)st->st_cur) < 0) {
857 return 0;
858 }
859 }
860 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861}
862
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000863static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864symtable_lookup(struct symtable *st, PyObject *name)
865{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000866 PyObject *o;
867 PyObject *mangled = _Py_Mangle(st->st_private, name);
868 if (!mangled)
869 return 0;
870 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
871 Py_DECREF(mangled);
872 if (!o)
873 return 0;
874 return PyInt_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875}
876
877static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000878symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000880 PyObject *o;
881 PyObject *dict;
882 long val;
883 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000885 if (!mangled)
886 return 0;
887 dict = st->st_cur->ste_symbols;
888 if ((o = PyDict_GetItem(dict, mangled))) {
889 val = PyInt_AS_LONG(o);
890 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
891 /* Is it better to use 'mangled' or 'name' here? */
892 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
893 PyString_AsString(name));
894 PyErr_SyntaxLocation(st->st_filename,
895 st->st_cur->ste_lineno);
896 goto error;
897 }
898 val |= flag;
899 } else
900 val = flag;
901 o = PyInt_FromLong(val);
902 if (o == NULL)
903 goto error;
904 if (PyDict_SetItem(dict, mangled, o) < 0) {
905 Py_DECREF(o);
906 goto error;
907 }
908 Py_DECREF(o);
909
910 if (flag & DEF_PARAM) {
911 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
912 goto error;
913 } else if (flag & DEF_GLOBAL) {
914 /* XXX need to update DEF_GLOBAL for other flags too;
915 perhaps only DEF_FREE_GLOBAL */
916 val = flag;
917 if ((o = PyDict_GetItem(st->st_global, mangled))) {
918 val |= PyInt_AS_LONG(o);
919 }
920 o = PyInt_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921 if (o == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000922 goto error;
923 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
924 Py_DECREF(o);
925 goto error;
926 }
927 Py_DECREF(o);
928 }
929 Py_DECREF(mangled);
930 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000931
932error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000933 Py_DECREF(mangled);
934 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935}
936
937/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
938 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000939 function.
940
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
942 useful if the first node in the sequence requires special treatment.
943*/
944
945#define VISIT(ST, TYPE, V) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000946 if (!symtable_visit_ ## TYPE((ST), (V))) \
947 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000948
949#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000950 if (!symtable_visit_ ## TYPE((ST), (V))) { \
951 symtable_exit_block((ST), (S)); \
952 return 0; \
953 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000956 int i; \
957 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
958 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
959 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
960 if (!symtable_visit_ ## TYPE((ST), elt)) \
961 return 0; \
962 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000964
965#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000966 int i; \
967 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
968 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
969 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
970 if (!symtable_visit_ ## TYPE((ST), elt)) { \
971 symtable_exit_block((ST), (S)); \
972 return 0; \
973 } \
974 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000975}
976
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000978 int i; \
979 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
980 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
981 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
982 if (!symtable_visit_ ## TYPE((ST), elt)) \
983 return 0; \
984 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000986
987#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000988 int i; \
989 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
990 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
991 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
992 if (!symtable_visit_ ## TYPE((ST), elt)) { \
993 symtable_exit_block((ST), (S)); \
994 return 0; \
995 } \
996 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000997}
998
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999static int
1000symtable_visit_stmt(struct symtable *st, stmt_ty s)
1001{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001002 switch (s->kind) {
1003 case FunctionDef_kind:
1004 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1005 return 0;
1006 if (s->v.FunctionDef.args->defaults)
1007 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1008 if (s->v.FunctionDef.decorator_list)
1009 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1010 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1011 FunctionBlock, (void *)s, s->lineno))
1012 return 0;
1013 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1014 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1015 if (!symtable_exit_block(st, s))
1016 return 0;
1017 break;
1018 case ClassDef_kind: {
1019 PyObject *tmp;
1020 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1021 return 0;
1022 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1023 if (s->v.ClassDef.decorator_list)
1024 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1025 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1026 (void *)s, s->lineno))
1027 return 0;
1028 tmp = st->st_private;
1029 st->st_private = s->v.ClassDef.name;
1030 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1031 st->st_private = tmp;
1032 if (!symtable_exit_block(st, s))
1033 return 0;
1034 break;
1035 }
1036 case Return_kind:
1037 if (s->v.Return.value) {
1038 VISIT(st, expr, s->v.Return.value);
1039 st->st_cur->ste_returns_value = 1;
1040 if (st->st_cur->ste_generator) {
1041 PyErr_SetString(PyExc_SyntaxError,
1042 RETURN_VAL_IN_GENERATOR);
1043 PyErr_SyntaxLocation(st->st_filename,
1044 s->lineno);
1045 return 0;
1046 }
1047 }
1048 break;
1049 case Delete_kind:
1050 VISIT_SEQ(st, expr, s->v.Delete.targets);
1051 break;
1052 case Assign_kind:
1053 VISIT_SEQ(st, expr, s->v.Assign.targets);
1054 VISIT(st, expr, s->v.Assign.value);
1055 break;
1056 case AugAssign_kind:
1057 VISIT(st, expr, s->v.AugAssign.target);
1058 VISIT(st, expr, s->v.AugAssign.value);
1059 break;
1060 case Print_kind:
1061 if (s->v.Print.dest)
1062 VISIT(st, expr, s->v.Print.dest);
1063 VISIT_SEQ(st, expr, s->v.Print.values);
1064 break;
1065 case For_kind:
1066 VISIT(st, expr, s->v.For.target);
1067 VISIT(st, expr, s->v.For.iter);
1068 VISIT_SEQ(st, stmt, s->v.For.body);
1069 if (s->v.For.orelse)
1070 VISIT_SEQ(st, stmt, s->v.For.orelse);
1071 break;
1072 case While_kind:
1073 VISIT(st, expr, s->v.While.test);
1074 VISIT_SEQ(st, stmt, s->v.While.body);
1075 if (s->v.While.orelse)
1076 VISIT_SEQ(st, stmt, s->v.While.orelse);
1077 break;
1078 case If_kind:
1079 /* XXX if 0: and lookup_yield() hacks */
1080 VISIT(st, expr, s->v.If.test);
1081 VISIT_SEQ(st, stmt, s->v.If.body);
1082 if (s->v.If.orelse)
1083 VISIT_SEQ(st, stmt, s->v.If.orelse);
1084 break;
1085 case Raise_kind:
1086 if (s->v.Raise.type) {
1087 VISIT(st, expr, s->v.Raise.type);
1088 if (s->v.Raise.inst) {
1089 VISIT(st, expr, s->v.Raise.inst);
1090 if (s->v.Raise.tback)
1091 VISIT(st, expr, s->v.Raise.tback);
1092 }
1093 }
1094 break;
1095 case TryExcept_kind:
1096 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1097 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1098 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1099 break;
1100 case TryFinally_kind:
1101 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1102 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1103 break;
1104 case Assert_kind:
1105 VISIT(st, expr, s->v.Assert.test);
1106 if (s->v.Assert.msg)
1107 VISIT(st, expr, s->v.Assert.msg);
1108 break;
1109 case Import_kind:
1110 VISIT_SEQ(st, alias, s->v.Import.names);
1111 /* XXX Don't have the lineno available inside
1112 visit_alias */
1113 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1114 st->st_cur->ste_opt_lineno = s->lineno;
1115 break;
1116 case ImportFrom_kind:
1117 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1118 /* XXX Don't have the lineno available inside
1119 visit_alias */
1120 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1121 st->st_cur->ste_opt_lineno = s->lineno;
1122 break;
1123 case Exec_kind:
1124 VISIT(st, expr, s->v.Exec.body);
1125 if (!st->st_cur->ste_opt_lineno)
1126 st->st_cur->ste_opt_lineno = s->lineno;
1127 if (s->v.Exec.globals) {
1128 st->st_cur->ste_unoptimized |= OPT_EXEC;
1129 VISIT(st, expr, s->v.Exec.globals);
1130 if (s->v.Exec.locals)
1131 VISIT(st, expr, s->v.Exec.locals);
1132 } else {
1133 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1134 }
1135 break;
1136 case Global_kind: {
1137 int i;
1138 asdl_seq *seq = s->v.Global.names;
1139 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1140 identifier name = (identifier)asdl_seq_GET(seq, i);
1141 char *c_name = PyString_AS_STRING(name);
1142 long cur = symtable_lookup(st, name);
1143 if (cur < 0)
1144 return 0;
1145 if (cur & (DEF_LOCAL | USE)) {
1146 char buf[256];
1147 if (cur & DEF_LOCAL)
1148 PyOS_snprintf(buf, sizeof(buf),
1149 GLOBAL_AFTER_ASSIGN,
1150 c_name);
1151 else
1152 PyOS_snprintf(buf, sizeof(buf),
1153 GLOBAL_AFTER_USE,
1154 c_name);
1155 if (!symtable_warn(st, buf, s->lineno))
1156 return 0;
1157 }
1158 if (!symtable_add_def(st, name, DEF_GLOBAL))
1159 return 0;
1160 }
1161 break;
1162 }
1163 case Expr_kind:
1164 VISIT(st, expr, s->v.Expr.value);
1165 break;
1166 case Pass_kind:
1167 case Break_kind:
1168 case Continue_kind:
1169 /* nothing to do here */
1170 break;
1171 case With_kind:
1172 VISIT(st, expr, s->v.With.context_expr);
1173 if (s->v.With.optional_vars) {
1174 VISIT(st, expr, s->v.With.optional_vars);
1175 }
1176 VISIT_SEQ(st, stmt, s->v.With.body);
1177 break;
1178 }
1179 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180}
1181
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001182static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183symtable_visit_expr(struct symtable *st, expr_ty e)
1184{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001185 switch (e->kind) {
1186 case BoolOp_kind:
1187 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1188 break;
1189 case BinOp_kind:
1190 VISIT(st, expr, e->v.BinOp.left);
1191 VISIT(st, expr, e->v.BinOp.right);
1192 break;
1193 case UnaryOp_kind:
1194 VISIT(st, expr, e->v.UnaryOp.operand);
1195 break;
1196 case Lambda_kind: {
1197 if (!GET_IDENTIFIER(lambda))
1198 return 0;
1199 if (e->v.Lambda.args->defaults)
1200 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1201 if (!symtable_enter_block(st, lambda,
1202 FunctionBlock, (void *)e, e->lineno))
1203 return 0;
1204 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1205 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1206 if (!symtable_exit_block(st, (void *)e))
1207 return 0;
1208 break;
1209 }
1210 case IfExp_kind:
1211 VISIT(st, expr, e->v.IfExp.test);
1212 VISIT(st, expr, e->v.IfExp.body);
1213 VISIT(st, expr, e->v.IfExp.orelse);
1214 break;
1215 case Dict_kind:
1216 VISIT_SEQ(st, expr, e->v.Dict.keys);
1217 VISIT_SEQ(st, expr, e->v.Dict.values);
1218 break;
1219 case Set_kind:
1220 VISIT_SEQ(st, expr, e->v.Set.elts);
1221 break;
1222 case ListComp_kind:
1223 VISIT(st, expr, e->v.ListComp.elt);
1224 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1225 break;
1226 case GeneratorExp_kind:
1227 if (!symtable_visit_genexp(st, e))
1228 return 0;
1229 break;
1230 case SetComp_kind:
1231 if (!symtable_visit_setcomp(st, e))
1232 return 0;
1233 break;
1234 case DictComp_kind:
1235 if (!symtable_visit_dictcomp(st, e))
1236 return 0;
1237 break;
1238 case Yield_kind:
1239 if (e->v.Yield.value)
1240 VISIT(st, expr, e->v.Yield.value);
1241 st->st_cur->ste_generator = 1;
1242 if (st->st_cur->ste_returns_value) {
1243 PyErr_SetString(PyExc_SyntaxError,
1244 RETURN_VAL_IN_GENERATOR);
1245 PyErr_SyntaxLocation(st->st_filename,
1246 e->lineno);
1247 return 0;
1248 }
1249 break;
1250 case Compare_kind:
1251 VISIT(st, expr, e->v.Compare.left);
1252 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1253 break;
1254 case Call_kind:
1255 VISIT(st, expr, e->v.Call.func);
1256 VISIT_SEQ(st, expr, e->v.Call.args);
1257 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1258 if (e->v.Call.starargs)
1259 VISIT(st, expr, e->v.Call.starargs);
1260 if (e->v.Call.kwargs)
1261 VISIT(st, expr, e->v.Call.kwargs);
1262 break;
1263 case Repr_kind:
1264 VISIT(st, expr, e->v.Repr.value);
1265 break;
1266 case Num_kind:
1267 case Str_kind:
1268 /* Nothing to do here. */
1269 break;
1270 /* The following exprs can be assignment targets. */
1271 case Attribute_kind:
1272 VISIT(st, expr, e->v.Attribute.value);
1273 break;
1274 case Subscript_kind:
1275 VISIT(st, expr, e->v.Subscript.value);
1276 VISIT(st, slice, e->v.Subscript.slice);
1277 break;
1278 case Name_kind:
1279 if (!symtable_add_def(st, e->v.Name.id,
1280 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1281 return 0;
1282 break;
1283 /* child nodes of List and Tuple will have expr_context set */
1284 case List_kind:
1285 VISIT_SEQ(st, expr, e->v.List.elts);
1286 break;
1287 case Tuple_kind:
1288 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1289 break;
1290 }
1291 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292}
1293
1294static int
1295symtable_implicit_arg(struct symtable *st, int pos)
1296{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001297 PyObject *id = PyString_FromFormat(".%d", pos);
1298 if (id == NULL)
1299 return 0;
1300 if (!symtable_add_def(st, id, DEF_PARAM)) {
1301 Py_DECREF(id);
1302 return 0;
1303 }
1304 Py_DECREF(id);
1305 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306}
1307
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001308static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1310{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001311 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001313 /* go through all the toplevel arguments first */
1314 for (i = 0; i < asdl_seq_LEN(args); i++) {
1315 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1316 if (arg->kind == Name_kind) {
1317 assert(arg->v.Name.ctx == Param ||
1318 (arg->v.Name.ctx == Store && !toplevel));
1319 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1320 return 0;
1321 }
1322 else if (arg->kind == Tuple_kind) {
1323 assert(arg->v.Tuple.ctx == Store);
1324 if (toplevel) {
1325 if (!symtable_implicit_arg(st, i))
1326 return 0;
1327 }
1328 }
1329 else {
1330 PyErr_SetString(PyExc_SyntaxError,
1331 "invalid expression in parameter list");
1332 PyErr_SyntaxLocation(st->st_filename,
1333 st->st_cur->ste_lineno);
1334 return 0;
1335 }
1336 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001338 if (!toplevel) {
1339 if (!symtable_visit_params_nested(st, args))
1340 return 0;
1341 }
1342
1343 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344}
1345
1346static int
1347symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1348{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001349 int i;
1350 for (i = 0; i < asdl_seq_LEN(args); i++) {
1351 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1352 if (arg->kind == Tuple_kind &&
1353 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1354 return 0;
1355 }
1356
1357 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358}
1359
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001360static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361symtable_visit_arguments(struct symtable *st, arguments_ty a)
1362{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001363 /* skip default arguments inside function block
1364 XXX should ast be different?
1365 */
1366 if (a->args && !symtable_visit_params(st, a->args, 1))
1367 return 0;
1368 if (a->vararg) {
1369 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1370 return 0;
1371 st->st_cur->ste_varargs = 1;
1372 }
1373 if (a->kwarg) {
1374 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1375 return 0;
1376 st->st_cur->ste_varkeywords = 1;
1377 }
1378 if (a->args && !symtable_visit_params_nested(st, a->args))
1379 return 0;
1380 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381}
1382
1383
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001384static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1386{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001387 if (eh->v.ExceptHandler.type)
1388 VISIT(st, expr, eh->v.ExceptHandler.type);
1389 if (eh->v.ExceptHandler.name)
1390 VISIT(st, expr, eh->v.ExceptHandler.name);
1391 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1392 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393}
1394
1395
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001396static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397symtable_visit_alias(struct symtable *st, alias_ty a)
1398{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001399 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc6660cf2010-06-11 21:40:37 +00001400 operation. It is different than a->name when a->name is a
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001401 dotted package name (e.g. spam.eggs)
1402 */
1403 PyObject *store_name;
1404 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1405 const char *base = PyString_AS_STRING(name);
1406 char *dot = strchr(base, '.');
1407 if (dot) {
1408 store_name = PyString_FromStringAndSize(base, dot - base);
1409 if (!store_name)
1410 return 0;
1411 }
1412 else {
1413 store_name = name;
1414 Py_INCREF(store_name);
1415 }
1416 if (strcmp(PyString_AS_STRING(name), "*")) {
1417 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1418 Py_DECREF(store_name);
1419 return r;
1420 }
1421 else {
1422 if (st->st_cur->ste_type != ModuleBlock) {
1423 int lineno = st->st_cur->ste_lineno;
1424 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
1425 Py_DECREF(store_name);
1426 return 0;
1427 }
1428 }
1429 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1430 Py_DECREF(store_name);
1431 return 1;
1432 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433}
1434
1435
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001436static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1438{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001439 VISIT(st, expr, lc->target);
1440 VISIT(st, expr, lc->iter);
1441 VISIT_SEQ(st, expr, lc->ifs);
1442 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443}
1444
1445
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001446static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447symtable_visit_keyword(struct symtable *st, keyword_ty k)
1448{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001449 VISIT(st, expr, k->value);
1450 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451}
1452
1453
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001454static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455symtable_visit_slice(struct symtable *st, slice_ty s)
1456{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001457 switch (s->kind) {
1458 case Slice_kind:
1459 if (s->v.Slice.lower)
1460 VISIT(st, expr, s->v.Slice.lower)
1461 if (s->v.Slice.upper)
1462 VISIT(st, expr, s->v.Slice.upper)
1463 if (s->v.Slice.step)
1464 VISIT(st, expr, s->v.Slice.step)
1465 break;
1466 case ExtSlice_kind:
1467 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1468 break;
1469 case Index_kind:
1470 VISIT(st, expr, s->v.Index.value)
1471 break;
1472 case Ellipsis_kind:
1473 break;
1474 }
1475 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476}
1477
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001478static int
1479symtable_new_tmpname(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001481 char tmpname[256];
1482 identifier tmp;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001483
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001484 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1485 ++st->st_cur->ste_tmpname);
1486 tmp = PyString_InternFromString(tmpname);
1487 if (!tmp)
1488 return 0;
1489 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1490 return 0;
1491 Py_DECREF(tmp);
1492 return 1;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001493}
1494
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001495static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001496symtable_handle_comprehension(struct symtable *st, expr_ty e,
1497 identifier scope_name, asdl_seq *generators,
1498 expr_ty elt, expr_ty value)
1499{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001500 int is_generator = (e->kind == GeneratorExp_kind);
1501 int needs_tmp = !is_generator;
1502 comprehension_ty outermost = ((comprehension_ty)
1503 asdl_seq_GET(generators, 0));
1504 /* Outermost iterator is evaluated in current scope */
1505 VISIT(st, expr, outermost->iter);
1506 /* Create comprehension scope for the rest */
1507 if (!scope_name ||
1508 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
1509 return 0;
1510 }
1511 st->st_cur->ste_generator = is_generator;
1512 /* Outermost iter is received as an argument */
1513 if (!symtable_implicit_arg(st, 0)) {
1514 symtable_exit_block(st, (void *)e);
1515 return 0;
1516 }
1517 /* Allocate temporary name if needed */
1518 if (needs_tmp && !symtable_new_tmpname(st)) {
1519 symtable_exit_block(st, (void *)e);
1520 return 0;
1521 }
1522 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1523 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1524 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1525 generators, 1, (void*)e);
1526 if (value)
1527 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1528 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1529 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530}
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001531
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001532static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001533symtable_visit_genexp(struct symtable *st, expr_ty e)
1534{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001535 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1536 e->v.GeneratorExp.generators,
1537 e->v.GeneratorExp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001538}
1539
1540static int
1541symtable_visit_setcomp(struct symtable *st, expr_ty e)
1542{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001543 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1544 e->v.SetComp.generators,
1545 e->v.SetComp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001546}
1547
1548static int
1549symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1550{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001551 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1552 e->v.DictComp.generators,
1553 e->v.DictComp.key,
1554 e->v.DictComp.value);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001555}