blob: 8d4b0d37b5094793c62f9be283efc03902e4a23b [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);
Christian Heimesdfaf90d2012-09-12 17:58:10 +020031 if (ste == NULL) {
32 Py_DECREF(k);
Antoine Pitrouc83ea132010-05-09 14:46:46 +000033 goto fail;
Christian Heimesdfaf90d2012-09-12 17:58:10 +020034 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +000035 ste->ste_table = st;
Christian Heimes65a01412012-09-12 17:52:46 +020036 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000037
Antoine Pitrouc83ea132010-05-09 14:46:46 +000038 ste->ste_name = name;
39 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000040
Antoine Pitrouc83ea132010-05-09 14:46:46 +000041 ste->ste_symbols = NULL;
42 ste->ste_varnames = NULL;
43 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Antoine Pitrouc83ea132010-05-09 14:46:46 +000045 ste->ste_symbols = PyDict_New();
46 if (ste->ste_symbols == NULL)
47 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000048
Antoine Pitrouc83ea132010-05-09 14:46:46 +000049 ste->ste_varnames = PyList_New(0);
50 if (ste->ste_varnames == NULL)
51 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000052
Antoine Pitrouc83ea132010-05-09 14:46:46 +000053 ste->ste_children = PyList_New(0);
54 if (ste->ste_children == NULL)
55 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000056
Antoine Pitrouc83ea132010-05-09 14:46:46 +000057 ste->ste_type = block;
58 ste->ste_unoptimized = 0;
59 ste->ste_nested = 0;
60 ste->ste_free = 0;
61 ste->ste_varargs = 0;
62 ste->ste_varkeywords = 0;
63 ste->ste_opt_lineno = 0;
Benjamin Peterson045bbcd2010-10-20 21:28:09 +000064 ste->ste_tmpname = 0;
Antoine Pitrouc83ea132010-05-09 14:46:46 +000065 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000066
Antoine Pitrouc83ea132010-05-09 14:46:46 +000067 if (st->st_cur != NULL &&
68 (st->st_cur->ste_nested ||
69 st->st_cur->ste_type == FunctionBlock))
70 ste->ste_nested = 1;
71 ste->ste_child_free = 0;
72 ste->ste_generator = 0;
73 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000074
Antoine Pitrouc83ea132010-05-09 14:46:46 +000075 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
76 goto fail;
77
78 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000079 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +000080 Py_XDECREF(ste);
81 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082}
83
84static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000086{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000087 char buf[256];
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088
Antoine Pitrouc83ea132010-05-09 14:46:46 +000089 PyOS_snprintf(buf, sizeof(buf),
90 "<symtable entry %.100s(%ld), line %d>",
91 PyString_AS_STRING(ste->ste_name),
92 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
93 return PyString_FromString(buf);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000094}
95
96static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000098{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000099 ste->ste_table = NULL;
100 Py_XDECREF(ste->ste_id);
101 Py_XDECREF(ste->ste_name);
102 Py_XDECREF(ste->ste_symbols);
103 Py_XDECREF(ste->ste_varnames);
104 Py_XDECREF(ste->ste_children);
105 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000106}
107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109
Guido van Rossum6f799372001-09-20 20:46:19 +0000110static PyMemberDef ste_memberlist[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000111 {"id", T_OBJECT, OFF(ste_id), READONLY},
112 {"name", T_OBJECT, OFF(ste_name), READONLY},
113 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
114 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
115 {"children", T_OBJECT, OFF(ste_children), READONLY},
116 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
117 {"nested", T_INT, OFF(ste_nested), READONLY},
118 {"type", T_INT, OFF(ste_type), READONLY},
119 {"lineno", T_INT, OFF(ste_lineno), READONLY},
120 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121};
122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123PyTypeObject PySTEntry_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000124 PyVarObject_HEAD_INIT(&PyType_Type, 0)
125 "symtable entry",
126 sizeof(PySTEntryObject),
127 0,
128 (destructor)ste_dealloc, /* tp_dealloc */
129 0, /* tp_print */
130 0, /* tp_getattr */
131 0, /* tp_setattr */
132 0, /* tp_compare */
133 (reprfunc)ste_repr, /* tp_repr */
134 0, /* tp_as_number */
135 0, /* tp_as_sequence */
136 0, /* tp_as_mapping */
137 0, /* tp_hash */
138 0, /* tp_call */
139 0, /* tp_str */
140 PyObject_GenericGetAttr, /* tp_getattro */
141 0, /* tp_setattro */
142 0, /* tp_as_buffer */
143 Py_TPFLAGS_DEFAULT, /* tp_flags */
144 0, /* tp_doc */
145 0, /* tp_traverse */
146 0, /* tp_clear */
147 0, /* tp_richcompare */
148 0, /* tp_weaklistoffset */
149 0, /* tp_iter */
150 0, /* tp_iternext */
151 0, /* tp_methods */
152 ste_memberlist, /* tp_members */
153 0, /* tp_getset */
154 0, /* tp_base */
155 0, /* tp_dict */
156 0, /* tp_descr_get */
157 0, /* tp_descr_set */
158 0, /* tp_dictoffset */
159 0, /* tp_init */
160 0, /* tp_alloc */
161 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000162};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163
164static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000165static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000166static int symtable_enter_block(struct symtable *st, identifier name,
167 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168static int symtable_exit_block(struct symtable *st, void *ast);
169static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
170static int symtable_visit_expr(struct symtable *st, expr_ty s);
171static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000172static int symtable_visit_setcomp(struct symtable *st, expr_ty e);
173static int symtable_visit_dictcomp(struct symtable *st, expr_ty e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static int symtable_visit_arguments(struct symtable *st, arguments_ty);
175static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
176static int symtable_visit_alias(struct symtable *st, alias_ty);
177static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
178static int symtable_visit_keyword(struct symtable *st, keyword_ty);
179static int symtable_visit_slice(struct symtable *st, slice_ty);
180static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
181static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
182static int symtable_implicit_arg(struct symtable *st, int pos);
183
184
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000185static identifier top = NULL, lambda = NULL, genexpr = NULL, setcomp = NULL,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000186 dictcomp = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
188#define GET_IDENTIFIER(VAR) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191#define DUPLICATE_ARGUMENT \
192"duplicate argument '%s' in function definition"
193
194static struct symtable *
195symtable_new(void)
196{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000197 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000199 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
200 if (st == NULL)
201 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000203 st->st_filename = NULL;
204 st->st_symbols = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000205
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000206 if ((st->st_stack = PyList_New(0)) == NULL)
207 goto fail;
208 if ((st->st_symbols = PyDict_New()) == NULL)
209 goto fail;
210 st->st_cur = NULL;
211 st->st_private = NULL;
212 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000214 PySymtable_Free(st);
215 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216}
217
218struct symtable *
219PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
220{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000221 struct symtable *st = symtable_new();
222 asdl_seq *seq;
223 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000225 if (st == NULL)
226 return st;
227 st->st_filename = filename;
228 st->st_future = future;
229 if (!GET_IDENTIFIER(top) ||
230 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
231 PySymtable_Free(st);
232 return NULL;
233 }
Neal Norwitzd12bd012006-07-21 07:59:47 +0000234
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000235 st->st_top = st->st_cur;
236 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
237 /* Any other top-level initialization? */
238 switch (mod->kind) {
239 case Module_kind:
240 seq = mod->v.Module.body;
241 for (i = 0; i < asdl_seq_LEN(seq); i++)
242 if (!symtable_visit_stmt(st,
243 (stmt_ty)asdl_seq_GET(seq, i)))
244 goto error;
245 break;
246 case Expression_kind:
247 if (!symtable_visit_expr(st, mod->v.Expression.body))
248 goto error;
249 break;
250 case Interactive_kind:
251 seq = mod->v.Interactive.body;
252 for (i = 0; i < asdl_seq_LEN(seq); i++)
253 if (!symtable_visit_stmt(st,
254 (stmt_ty)asdl_seq_GET(seq, i)))
255 goto error;
256 break;
257 case Suite_kind:
258 PyErr_SetString(PyExc_RuntimeError,
259 "this compiler does not handle Suites");
260 goto error;
261 }
262 if (!symtable_exit_block(st, (void *)mod)) {
263 PySymtable_Free(st);
264 return NULL;
265 }
266 if (symtable_analyze(st))
267 return st;
268 PySymtable_Free(st);
269 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000271 (void) symtable_exit_block(st, (void *)mod);
272 PySymtable_Free(st);
273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274}
275
276void
277PySymtable_Free(struct symtable *st)
278{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000279 Py_XDECREF(st->st_symbols);
280 Py_XDECREF(st->st_stack);
281 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282}
283
284PySTEntryObject *
285PySymtable_Lookup(struct symtable *st, void *key)
286{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000287 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000289 k = PyLong_FromVoidPtr(key);
290 if (k == NULL)
291 return NULL;
292 v = PyDict_GetItem(st->st_symbols, k);
293 if (v) {
294 assert(PySTEntry_Check(v));
295 Py_INCREF(v);
296 }
297 else {
298 PyErr_SetString(PyExc_KeyError,
299 "unknown symbol table entry");
300 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000301
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000302 Py_DECREF(k);
303 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304}
305
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000306int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307PyST_GetScope(PySTEntryObject *ste, PyObject *name)
308{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000309 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
310 if (!v)
311 return 0;
312 assert(PyInt_Check(v));
313 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314}
315
316
317/* Analyze raw symbol information to determine scope of each name.
318
319 The next several functions are helpers for PySymtable_Analyze(),
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000320 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 it determines which local variables are cell variables; they provide
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000322 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000324 There are also two kinds of free variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325 explicit global is declared with the global statement. An implicit
326 global is a free variable for which the compiler has found no binding
327 in an enclosing function scope. The implicit global is either a global
328 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
329 to handle these names to implement slightly odd semantics. In such a
330 block, the name is treated as global until it is assigned to; then it
331 is treated as a local.
332
333 The symbol table requires two passes to determine the scope of each name.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000334 The first pass collects raw facts from the AST: the name is a parameter
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000335 here, the name is used by not defined here, etc. The second pass analyzes
336 these facts during a pass over the PySTEntryObjects created during pass 1.
337
338 When a function is entered during the second pass, the parent passes
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000339 the set of all name bindings visible to its children. These bindings
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340 are used to determine if the variable is free or an implicit global.
341 After doing the local analysis, it analyzes each of its child blocks
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000342 using an updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000344 The children update the free variable set. If a local variable is free
345 in a child, the variable is marked as a cell. The current function must
346 provide runtime storage for the variable that may outlive the function's
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347 frame. Cell variables are removed from the free set before the analyze
348 function returns to its parent.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000349
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350 The sets of bound and free variables are implemented as dictionaries
351 mapping strings to None.
352*/
353
354#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000355 PyObject *o = PyInt_FromLong(I); \
356 if (!o) \
357 return 0; \
358 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
359 Py_DECREF(o); \
360 return 0; \
361 } \
362 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363}
364
365/* Decide on scope of name, given flags.
366
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000367 The namespace dictionaries may be modified to record information
368 about the new name. For example, a new global will add an entry to
369 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370*/
371
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000372static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000373analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000374 PyObject *bound, PyObject *local, PyObject *free,
375 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000377 if (flags & DEF_GLOBAL) {
378 if (flags & DEF_PARAM) {
379 PyErr_Format(PyExc_SyntaxError,
380 "name '%s' is local and global",
381 PyString_AS_STRING(name));
382 PyErr_SyntaxLocation(ste->ste_table->st_filename,
383 ste->ste_lineno);
384
385 return 0;
386 }
387 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
388 if (PyDict_SetItem(global, name, Py_None) < 0)
389 return 0;
390 if (bound && PyDict_GetItem(bound, name)) {
391 if (PyDict_DelItem(bound, name) < 0)
392 return 0;
393 }
394 return 1;
395 }
396 if (flags & DEF_BOUND) {
397 SET_SCOPE(dict, name, LOCAL);
398 if (PyDict_SetItem(local, name, Py_None) < 0)
399 return 0;
400 if (PyDict_GetItem(global, name)) {
401 if (PyDict_DelItem(global, name) < 0)
402 return 0;
403 }
404 return 1;
405 }
406 /* If an enclosing block has a binding for this name, it
407 is a free variable rather than a global variable.
408 Note that having a non-NULL bound implies that the block
409 is nested.
410 */
411 if (bound && PyDict_GetItem(bound, name)) {
412 SET_SCOPE(dict, name, FREE);
413 ste->ste_free = 1;
414 if (PyDict_SetItem(free, name, Py_None) < 0)
415 return 0;
416 return 1;
417 }
418 /* If a parent has a global statement, then call it global
419 explicit? It could also be global implicit.
420 */
421 else if (global && PyDict_GetItem(global, name)) {
422 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
423 return 1;
424 }
425 else {
426 if (ste->ste_nested)
427 ste->ste_free = 1;
428 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
429 return 1;
430 }
431 /* Should never get here. */
432 PyErr_Format(PyExc_SystemError, "failed to set scope for %s",
433 PyString_AS_STRING(name));
434 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435}
436
437#undef SET_SCOPE
438
439/* If a name is defined in free and also in locals, then this block
440 provides the binding for the free variable. The name should be
441 marked CELL in this block and removed from the free list.
442
443 Note that the current block's free variables are included in free.
444 That's safe because no name can be free and local in the same scope.
445*/
446
447static int
448analyze_cells(PyObject *scope, PyObject *free)
449{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000450 PyObject *name, *v, *w;
451 int success = 0;
452 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000454 w = PyInt_FromLong(CELL);
455 if (!w)
456 return 0;
457 while (PyDict_Next(scope, &pos, &name, &v)) {
458 long flags;
459 assert(PyInt_Check(v));
460 flags = PyInt_AS_LONG(v);
461 if (flags != LOCAL)
462 continue;
463 if (!PyDict_GetItem(free, name))
464 continue;
465 /* Replace LOCAL with CELL for this name, and remove
466 from free. It is safe to replace the value of name
467 in the dict, because it will not cause a resize.
468 */
469 if (PyDict_SetItem(scope, name, w) < 0)
470 goto error;
Benjamin Peterson8363f772014-01-16 16:56:22 -0500471 if (PyDict_DelItem(free, name) < 0)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000472 goto error;
473 }
474 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000476 Py_DECREF(w);
477 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478}
479
480/* Check for illegal statements in unoptimized namespaces */
481static int
482check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000483 char buf[300];
484 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000486 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
487 || !(ste->ste_free || ste->ste_child_free))
488 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000490 trailer = (ste->ste_child_free ?
491 "contains a nested function with free variables" :
492 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000494 switch (ste->ste_unoptimized) {
495 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
496 case OPT_EXEC: /* qualified exec is fine */
497 return 1;
498 case OPT_IMPORT_STAR:
499 PyOS_snprintf(buf, sizeof(buf),
500 "import * is not allowed in function '%.100s' "
501 "because it %s",
502 PyString_AS_STRING(ste->ste_name), trailer);
503 break;
504 case OPT_BARE_EXEC:
505 PyOS_snprintf(buf, sizeof(buf),
506 "unqualified exec is not allowed in function "
Benjamin Petersonee5729d2014-07-18 16:25:13 -0700507 "'%.100s' because it %s",
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000508 PyString_AS_STRING(ste->ste_name), trailer);
509 break;
510 default:
511 PyOS_snprintf(buf, sizeof(buf),
512 "function '%.100s' uses import * and bare exec, "
513 "which are illegal because it %s",
514 PyString_AS_STRING(ste->ste_name), trailer);
515 break;
516 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000518 PyErr_SetString(PyExc_SyntaxError, buf);
519 PyErr_SyntaxLocation(ste->ste_table->st_filename,
520 ste->ste_opt_lineno);
521 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522}
523
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000524/* Enter the final scope information into the st_symbols dict.
525 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 * All arguments are dicts. Modifies symbols, others are read-only.
527*/
528static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000529update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000530 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000532 PyObject *name, *v, *u, *w, *free_value = NULL;
533 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000535 while (PyDict_Next(symbols, &pos, &name, &v)) {
536 long i, flags;
537 assert(PyInt_Check(v));
538 flags = PyInt_AS_LONG(v);
539 w = PyDict_GetItem(scope, name);
540 assert(w && PyInt_Check(w));
541 i = PyInt_AS_LONG(w);
542 flags |= (i << SCOPE_OFF);
543 u = PyInt_FromLong(flags);
544 if (!u)
545 return 0;
546 if (PyDict_SetItem(symbols, name, u) < 0) {
547 Py_DECREF(u);
548 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000550 Py_DECREF(u);
551 }
552
553 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
554 if (!free_value)
555 return 0;
556
557 /* add a free variable when it's only use is for creating a closure */
558 pos = 0;
559 while (PyDict_Next(free, &pos, &name, &v)) {
560 PyObject *o = PyDict_GetItem(symbols, name);
561
562 if (o) {
563 /* It could be a free variable in a method of
564 the class that has the same name as a local
565 or global in the class scope.
566 */
567 if (classflag &&
568 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
569 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
570 o = PyInt_FromLong(i);
571 if (!o) {
572 Py_DECREF(free_value);
573 return 0;
574 }
575 if (PyDict_SetItem(symbols, name, o) < 0) {
576 Py_DECREF(o);
577 Py_DECREF(free_value);
578 return 0;
579 }
580 Py_DECREF(o);
581 }
582 /* else it's not free, probably a cell */
583 continue;
584 }
585 if (!PyDict_GetItem(bound, name))
586 continue; /* it's a global */
587
588 if (PyDict_SetItem(symbols, name, free_value) < 0) {
589 Py_DECREF(free_value);
590 return 0;
591 }
592 }
593 Py_DECREF(free_value);
594 return 1;
595}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596
597/* Make final symbol table decisions for block of ste.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000598
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 Arguments:
600 ste -- current symtable entry (input/output)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000601 bound -- set of variables bound in enclosing scopes (input). bound
602 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 free -- set of free variables in enclosed scopes (output)
604 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000605
606 The implementation uses two mutually recursive functions,
607 analyze_block() and analyze_child_block(). analyze_block() is
608 responsible for analyzing the individual names defined in a block.
609 analyze_child_block() prepares temporary namespace dictionaries
610 used to evaluated nested blocks.
611
612 The two functions exist because a child block should see the name
613 bindings of its enclosing blocks, but those bindings should not
614 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615*/
616
617static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000618analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
619 PyObject *global, PyObject* child_free);
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000620
621static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
623 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000625 PyObject *name, *v, *local = NULL, *scope = NULL;
626 PyObject *newbound = NULL, *newglobal = NULL;
627 PyObject *newfree = NULL, *allfree = NULL;
628 int i, success = 0;
629 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000631 local = PyDict_New(); /* collect new names bound in block */
632 if (!local)
633 goto error;
634 scope = PyDict_New(); /* collect scopes defined for each name */
635 if (!scope)
636 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000637
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000638 /* Allocate new global and bound variable dictionaries. These
639 dictionaries hold the names visible in nested blocks. For
640 ClassBlocks, the bound and global names are initialized
641 before analyzing names, because class bindings aren't
642 visible in methods. For other blocks, they are initialized
643 after names are analyzed.
644 */
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000645
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000646 /* TODO(jhylton): Package these dicts in a struct so that we
647 can write reasonable helper functions?
648 */
649 newglobal = PyDict_New();
650 if (!newglobal)
651 goto error;
652 newbound = PyDict_New();
653 if (!newbound)
654 goto error;
655 newfree = PyDict_New();
656 if (!newfree)
657 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000659 if (ste->ste_type == ClassBlock) {
660 if (PyDict_Update(newglobal, global) < 0)
661 goto error;
662 if (bound)
663 if (PyDict_Update(newbound, bound) < 0)
664 goto error;
665 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000667 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
668 long flags = PyInt_AS_LONG(v);
669 if (!analyze_name(ste, scope, name, flags,
670 bound, local, free, global))
671 goto error;
672 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000674 if (ste->ste_type != ClassBlock) {
675 if (ste->ste_type == FunctionBlock) {
676 if (PyDict_Update(newbound, local) < 0)
677 goto error;
678 }
679 if (bound) {
680 if (PyDict_Update(newbound, bound) < 0)
681 goto error;
682 }
683 if (PyDict_Update(newglobal, global) < 0)
684 goto error;
685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000687 /* Recursively call analyze_block() on each child block.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000688
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000689 newbound, newglobal now contain the names visible in
690 nested blocks. The free variables in the children will
691 be collected in allfree.
692 */
693 allfree = PyDict_New();
694 if (!allfree)
695 goto error;
696 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
697 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
698 PySTEntryObject* entry;
699 assert(c && PySTEntry_Check(c));
700 entry = (PySTEntryObject*)c;
701 if (!analyze_child_block(entry, newbound, newfree, newglobal,
702 allfree))
703 goto error;
704 if (entry->ste_free || entry->ste_child_free)
705 ste->ste_child_free = 1;
706 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000708 if (PyDict_Update(newfree, allfree) < 0)
709 goto error;
710 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
711 goto error;
712 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
713 ste->ste_type == ClassBlock))
714 goto error;
715 if (!check_unoptimized(ste))
716 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000718 if (PyDict_Update(free, newfree) < 0)
719 goto error;
720 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 Py_XDECREF(local);
723 Py_XDECREF(scope);
724 Py_XDECREF(newbound);
725 Py_XDECREF(newglobal);
726 Py_XDECREF(newfree);
727 Py_XDECREF(allfree);
728 if (!success)
729 assert(PyErr_Occurred());
730 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731}
732
733static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000734analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
735 PyObject *global, PyObject* child_free)
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000736{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000737 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000738
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000739 /* Copy the bound and global dictionaries.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000740
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000741 These dictionary are used by all blocks enclosed by the
742 current block. The analyze_block() call modifies these
743 dictionaries.
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000744
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000745 */
746 temp_bound = PyDict_New();
747 if (!temp_bound)
748 goto error;
749 if (PyDict_Update(temp_bound, bound) < 0)
750 goto error;
751 temp_free = PyDict_New();
752 if (!temp_free)
753 goto error;
754 if (PyDict_Update(temp_free, free) < 0)
755 goto error;
756 temp_global = PyDict_New();
757 if (!temp_global)
758 goto error;
759 if (PyDict_Update(temp_global, global) < 0)
760 goto error;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000761
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000762 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
763 goto error;
764 if (PyDict_Update(child_free, temp_free) < 0)
765 goto error;
766 Py_DECREF(temp_bound);
767 Py_DECREF(temp_free);
768 Py_DECREF(temp_global);
769 return 1;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000770 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000771 Py_XDECREF(temp_bound);
772 Py_XDECREF(temp_free);
773 Py_XDECREF(temp_global);
774 return 0;
Jeremy Hylton88f1c042009-03-31 13:48:15 +0000775}
776
777static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778symtable_analyze(struct symtable *st)
779{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000780 PyObject *free, *global;
781 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000783 free = PyDict_New();
784 if (!free)
785 return 0;
786 global = PyDict_New();
787 if (!global) {
788 Py_DECREF(free);
789 return 0;
790 }
791 r = analyze_block(st->st_top, NULL, free, global);
792 Py_DECREF(free);
793 Py_DECREF(global);
794 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795}
796
797
798static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000799symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000801 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
802 lineno, NULL, NULL) < 0) {
803 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
804 PyErr_SetString(PyExc_SyntaxError, msg);
805 PyErr_SyntaxLocation(st->st_filename,
806 st->st_cur->ste_lineno);
807 }
808 return 0;
809 }
810 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811}
812
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000813/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 This reference is released when the block is exited, via the DECREF
815 in symtable_exit_block().
816*/
817
818static int
819symtable_exit_block(struct symtable *st, void *ast)
820{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000821 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000823 Py_CLEAR(st->st_cur);
824 end = PyList_GET_SIZE(st->st_stack) - 1;
825 if (end >= 0) {
826 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
827 end);
828 if (st->st_cur == NULL)
829 return 0;
830 Py_INCREF(st->st_cur);
831 if (PySequence_DelItem(st->st_stack, end) < 0)
832 return 0;
833 }
834 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835}
836
837static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000838symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
839 void *ast, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000841 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000843 if (st->st_cur) {
844 prev = st->st_cur;
845 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
846 return 0;
847 }
848 Py_DECREF(st->st_cur);
849 }
850 st->st_cur = ste_new(st, name, block, ast, lineno);
851 if (st->st_cur == NULL)
852 return 0;
Benjamin Petersonf76942d2010-10-16 03:51:38 +0000853 if (block == ModuleBlock)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000854 st->st_global = st->st_cur->ste_symbols;
855 if (prev) {
856 if (PyList_Append(prev->ste_children,
857 (PyObject *)st->st_cur) < 0) {
858 return 0;
859 }
860 }
861 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862}
863
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000864static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865symtable_lookup(struct symtable *st, PyObject *name)
866{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000867 PyObject *o;
868 PyObject *mangled = _Py_Mangle(st->st_private, name);
869 if (!mangled)
870 return 0;
871 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
872 Py_DECREF(mangled);
873 if (!o)
874 return 0;
875 return PyInt_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876}
877
878static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000879symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000881 PyObject *o;
882 PyObject *dict;
883 long val;
884 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000886 if (!mangled)
887 return 0;
888 dict = st->st_cur->ste_symbols;
889 if ((o = PyDict_GetItem(dict, mangled))) {
890 val = PyInt_AS_LONG(o);
891 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
892 /* Is it better to use 'mangled' or 'name' here? */
893 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
894 PyString_AsString(name));
895 PyErr_SyntaxLocation(st->st_filename,
896 st->st_cur->ste_lineno);
897 goto error;
898 }
899 val |= flag;
900 } else
901 val = flag;
902 o = PyInt_FromLong(val);
903 if (o == NULL)
904 goto error;
905 if (PyDict_SetItem(dict, mangled, o) < 0) {
906 Py_DECREF(o);
907 goto error;
908 }
909 Py_DECREF(o);
910
911 if (flag & DEF_PARAM) {
912 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
913 goto error;
914 } else if (flag & DEF_GLOBAL) {
915 /* XXX need to update DEF_GLOBAL for other flags too;
916 perhaps only DEF_FREE_GLOBAL */
917 val = flag;
918 if ((o = PyDict_GetItem(st->st_global, mangled))) {
919 val |= PyInt_AS_LONG(o);
920 }
921 o = PyInt_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 if (o == NULL)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000923 goto error;
924 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
925 Py_DECREF(o);
926 goto error;
927 }
928 Py_DECREF(o);
929 }
930 Py_DECREF(mangled);
931 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000932
933error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000934 Py_DECREF(mangled);
935 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936}
937
938/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
939 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000940 function.
941
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
943 useful if the first node in the sequence requires special treatment.
944*/
945
946#define VISIT(ST, TYPE, V) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000947 if (!symtable_visit_ ## TYPE((ST), (V))) \
948 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000949
950#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000951 if (!symtable_visit_ ## TYPE((ST), (V))) { \
952 symtable_exit_block((ST), (S)); \
953 return 0; \
954 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000957 int i; \
958 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
959 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
960 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
961 if (!symtable_visit_ ## TYPE((ST), elt)) \
962 return 0; \
963 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000965
966#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000967 int i; \
968 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
969 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
970 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
971 if (!symtable_visit_ ## TYPE((ST), elt)) { \
972 symtable_exit_block((ST), (S)); \
973 return 0; \
974 } \
975 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000976}
977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000979 int i; \
980 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
981 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
982 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
983 if (!symtable_visit_ ## TYPE((ST), elt)) \
984 return 0; \
985 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000987
988#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000989 int i; \
990 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
991 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
992 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
993 if (!symtable_visit_ ## TYPE((ST), elt)) { \
994 symtable_exit_block((ST), (S)); \
995 return 0; \
996 } \
997 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000998}
999
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000static int
1001symtable_visit_stmt(struct symtable *st, stmt_ty s)
1002{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001003 switch (s->kind) {
1004 case FunctionDef_kind:
1005 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1006 return 0;
1007 if (s->v.FunctionDef.args->defaults)
1008 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1009 if (s->v.FunctionDef.decorator_list)
1010 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1011 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1012 FunctionBlock, (void *)s, s->lineno))
1013 return 0;
1014 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1015 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1016 if (!symtable_exit_block(st, s))
1017 return 0;
1018 break;
1019 case ClassDef_kind: {
1020 PyObject *tmp;
1021 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1022 return 0;
1023 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1024 if (s->v.ClassDef.decorator_list)
1025 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1026 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1027 (void *)s, s->lineno))
1028 return 0;
1029 tmp = st->st_private;
1030 st->st_private = s->v.ClassDef.name;
1031 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1032 st->st_private = tmp;
1033 if (!symtable_exit_block(st, s))
1034 return 0;
1035 break;
1036 }
1037 case Return_kind:
1038 if (s->v.Return.value) {
1039 VISIT(st, expr, s->v.Return.value);
1040 st->st_cur->ste_returns_value = 1;
1041 if (st->st_cur->ste_generator) {
1042 PyErr_SetString(PyExc_SyntaxError,
1043 RETURN_VAL_IN_GENERATOR);
1044 PyErr_SyntaxLocation(st->st_filename,
1045 s->lineno);
1046 return 0;
1047 }
1048 }
1049 break;
1050 case Delete_kind:
1051 VISIT_SEQ(st, expr, s->v.Delete.targets);
1052 break;
1053 case Assign_kind:
1054 VISIT_SEQ(st, expr, s->v.Assign.targets);
1055 VISIT(st, expr, s->v.Assign.value);
1056 break;
1057 case AugAssign_kind:
1058 VISIT(st, expr, s->v.AugAssign.target);
1059 VISIT(st, expr, s->v.AugAssign.value);
1060 break;
1061 case Print_kind:
1062 if (s->v.Print.dest)
1063 VISIT(st, expr, s->v.Print.dest);
1064 VISIT_SEQ(st, expr, s->v.Print.values);
1065 break;
1066 case For_kind:
1067 VISIT(st, expr, s->v.For.target);
1068 VISIT(st, expr, s->v.For.iter);
1069 VISIT_SEQ(st, stmt, s->v.For.body);
1070 if (s->v.For.orelse)
1071 VISIT_SEQ(st, stmt, s->v.For.orelse);
1072 break;
1073 case While_kind:
1074 VISIT(st, expr, s->v.While.test);
1075 VISIT_SEQ(st, stmt, s->v.While.body);
1076 if (s->v.While.orelse)
1077 VISIT_SEQ(st, stmt, s->v.While.orelse);
1078 break;
1079 case If_kind:
1080 /* XXX if 0: and lookup_yield() hacks */
1081 VISIT(st, expr, s->v.If.test);
1082 VISIT_SEQ(st, stmt, s->v.If.body);
1083 if (s->v.If.orelse)
1084 VISIT_SEQ(st, stmt, s->v.If.orelse);
1085 break;
1086 case Raise_kind:
1087 if (s->v.Raise.type) {
1088 VISIT(st, expr, s->v.Raise.type);
1089 if (s->v.Raise.inst) {
1090 VISIT(st, expr, s->v.Raise.inst);
1091 if (s->v.Raise.tback)
1092 VISIT(st, expr, s->v.Raise.tback);
1093 }
1094 }
1095 break;
1096 case TryExcept_kind:
1097 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1098 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1099 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1100 break;
1101 case TryFinally_kind:
1102 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1103 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1104 break;
1105 case Assert_kind:
1106 VISIT(st, expr, s->v.Assert.test);
1107 if (s->v.Assert.msg)
1108 VISIT(st, expr, s->v.Assert.msg);
1109 break;
1110 case Import_kind:
1111 VISIT_SEQ(st, alias, s->v.Import.names);
1112 /* XXX Don't have the lineno available inside
1113 visit_alias */
1114 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1115 st->st_cur->ste_opt_lineno = s->lineno;
1116 break;
1117 case ImportFrom_kind:
1118 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1119 /* XXX Don't have the lineno available inside
1120 visit_alias */
1121 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1122 st->st_cur->ste_opt_lineno = s->lineno;
1123 break;
1124 case Exec_kind:
1125 VISIT(st, expr, s->v.Exec.body);
1126 if (!st->st_cur->ste_opt_lineno)
1127 st->st_cur->ste_opt_lineno = s->lineno;
1128 if (s->v.Exec.globals) {
1129 st->st_cur->ste_unoptimized |= OPT_EXEC;
1130 VISIT(st, expr, s->v.Exec.globals);
1131 if (s->v.Exec.locals)
1132 VISIT(st, expr, s->v.Exec.locals);
1133 } else {
1134 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1135 }
1136 break;
1137 case Global_kind: {
1138 int i;
1139 asdl_seq *seq = s->v.Global.names;
1140 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1141 identifier name = (identifier)asdl_seq_GET(seq, i);
1142 char *c_name = PyString_AS_STRING(name);
1143 long cur = symtable_lookup(st, name);
1144 if (cur < 0)
1145 return 0;
1146 if (cur & (DEF_LOCAL | USE)) {
1147 char buf[256];
1148 if (cur & DEF_LOCAL)
1149 PyOS_snprintf(buf, sizeof(buf),
1150 GLOBAL_AFTER_ASSIGN,
1151 c_name);
1152 else
1153 PyOS_snprintf(buf, sizeof(buf),
1154 GLOBAL_AFTER_USE,
1155 c_name);
1156 if (!symtable_warn(st, buf, s->lineno))
1157 return 0;
1158 }
1159 if (!symtable_add_def(st, name, DEF_GLOBAL))
1160 return 0;
1161 }
1162 break;
1163 }
1164 case Expr_kind:
1165 VISIT(st, expr, s->v.Expr.value);
1166 break;
1167 case Pass_kind:
1168 case Break_kind:
1169 case Continue_kind:
1170 /* nothing to do here */
1171 break;
1172 case With_kind:
1173 VISIT(st, expr, s->v.With.context_expr);
1174 if (s->v.With.optional_vars) {
1175 VISIT(st, expr, s->v.With.optional_vars);
1176 }
1177 VISIT_SEQ(st, stmt, s->v.With.body);
1178 break;
1179 }
1180 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181}
1182
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001183static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184symtable_visit_expr(struct symtable *st, expr_ty e)
1185{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001186 switch (e->kind) {
1187 case BoolOp_kind:
1188 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1189 break;
1190 case BinOp_kind:
1191 VISIT(st, expr, e->v.BinOp.left);
1192 VISIT(st, expr, e->v.BinOp.right);
1193 break;
1194 case UnaryOp_kind:
1195 VISIT(st, expr, e->v.UnaryOp.operand);
1196 break;
1197 case Lambda_kind: {
1198 if (!GET_IDENTIFIER(lambda))
1199 return 0;
1200 if (e->v.Lambda.args->defaults)
1201 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1202 if (!symtable_enter_block(st, lambda,
1203 FunctionBlock, (void *)e, e->lineno))
1204 return 0;
1205 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1206 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1207 if (!symtable_exit_block(st, (void *)e))
1208 return 0;
1209 break;
1210 }
1211 case IfExp_kind:
1212 VISIT(st, expr, e->v.IfExp.test);
1213 VISIT(st, expr, e->v.IfExp.body);
1214 VISIT(st, expr, e->v.IfExp.orelse);
1215 break;
1216 case Dict_kind:
1217 VISIT_SEQ(st, expr, e->v.Dict.keys);
1218 VISIT_SEQ(st, expr, e->v.Dict.values);
1219 break;
1220 case Set_kind:
1221 VISIT_SEQ(st, expr, e->v.Set.elts);
1222 break;
1223 case ListComp_kind:
1224 VISIT(st, expr, e->v.ListComp.elt);
1225 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1226 break;
1227 case GeneratorExp_kind:
1228 if (!symtable_visit_genexp(st, e))
1229 return 0;
1230 break;
1231 case SetComp_kind:
1232 if (!symtable_visit_setcomp(st, e))
1233 return 0;
1234 break;
1235 case DictComp_kind:
1236 if (!symtable_visit_dictcomp(st, e))
1237 return 0;
1238 break;
1239 case Yield_kind:
1240 if (e->v.Yield.value)
1241 VISIT(st, expr, e->v.Yield.value);
1242 st->st_cur->ste_generator = 1;
1243 if (st->st_cur->ste_returns_value) {
1244 PyErr_SetString(PyExc_SyntaxError,
1245 RETURN_VAL_IN_GENERATOR);
1246 PyErr_SyntaxLocation(st->st_filename,
1247 e->lineno);
1248 return 0;
1249 }
1250 break;
1251 case Compare_kind:
1252 VISIT(st, expr, e->v.Compare.left);
1253 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1254 break;
1255 case Call_kind:
1256 VISIT(st, expr, e->v.Call.func);
1257 VISIT_SEQ(st, expr, e->v.Call.args);
1258 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1259 if (e->v.Call.starargs)
1260 VISIT(st, expr, e->v.Call.starargs);
1261 if (e->v.Call.kwargs)
1262 VISIT(st, expr, e->v.Call.kwargs);
1263 break;
1264 case Repr_kind:
1265 VISIT(st, expr, e->v.Repr.value);
1266 break;
1267 case Num_kind:
1268 case Str_kind:
1269 /* Nothing to do here. */
1270 break;
1271 /* The following exprs can be assignment targets. */
1272 case Attribute_kind:
1273 VISIT(st, expr, e->v.Attribute.value);
1274 break;
1275 case Subscript_kind:
1276 VISIT(st, expr, e->v.Subscript.value);
1277 VISIT(st, slice, e->v.Subscript.slice);
1278 break;
1279 case Name_kind:
1280 if (!symtable_add_def(st, e->v.Name.id,
1281 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1282 return 0;
1283 break;
1284 /* child nodes of List and Tuple will have expr_context set */
1285 case List_kind:
1286 VISIT_SEQ(st, expr, e->v.List.elts);
1287 break;
1288 case Tuple_kind:
1289 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1290 break;
1291 }
1292 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293}
1294
1295static int
1296symtable_implicit_arg(struct symtable *st, int pos)
1297{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001298 PyObject *id = PyString_FromFormat(".%d", pos);
1299 if (id == NULL)
1300 return 0;
1301 if (!symtable_add_def(st, id, DEF_PARAM)) {
1302 Py_DECREF(id);
1303 return 0;
1304 }
1305 Py_DECREF(id);
1306 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307}
1308
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001309static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1311{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001312 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001314 /* go through all the toplevel arguments first */
1315 for (i = 0; i < asdl_seq_LEN(args); i++) {
1316 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1317 if (arg->kind == Name_kind) {
1318 assert(arg->v.Name.ctx == Param ||
1319 (arg->v.Name.ctx == Store && !toplevel));
1320 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1321 return 0;
1322 }
1323 else if (arg->kind == Tuple_kind) {
1324 assert(arg->v.Tuple.ctx == Store);
1325 if (toplevel) {
1326 if (!symtable_implicit_arg(st, i))
1327 return 0;
1328 }
1329 }
1330 else {
1331 PyErr_SetString(PyExc_SyntaxError,
1332 "invalid expression in parameter list");
1333 PyErr_SyntaxLocation(st->st_filename,
1334 st->st_cur->ste_lineno);
1335 return 0;
1336 }
1337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001339 if (!toplevel) {
1340 if (!symtable_visit_params_nested(st, args))
1341 return 0;
1342 }
1343
1344 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345}
1346
1347static int
1348symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1349{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001350 int i;
1351 for (i = 0; i < asdl_seq_LEN(args); i++) {
1352 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
1353 if (arg->kind == Tuple_kind &&
1354 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1355 return 0;
1356 }
1357
1358 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359}
1360
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001361static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362symtable_visit_arguments(struct symtable *st, arguments_ty a)
1363{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001364 /* skip default arguments inside function block
1365 XXX should ast be different?
1366 */
1367 if (a->args && !symtable_visit_params(st, a->args, 1))
1368 return 0;
1369 if (a->vararg) {
1370 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1371 return 0;
1372 st->st_cur->ste_varargs = 1;
1373 }
1374 if (a->kwarg) {
1375 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1376 return 0;
1377 st->st_cur->ste_varkeywords = 1;
1378 }
1379 if (a->args && !symtable_visit_params_nested(st, a->args))
1380 return 0;
1381 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382}
1383
1384
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001385static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1387{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001388 if (eh->v.ExceptHandler.type)
1389 VISIT(st, expr, eh->v.ExceptHandler.type);
1390 if (eh->v.ExceptHandler.name)
1391 VISIT(st, expr, eh->v.ExceptHandler.name);
1392 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1393 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394}
1395
1396
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001397static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398symtable_visit_alias(struct symtable *st, alias_ty a)
1399{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001400 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc6660cf2010-06-11 21:40:37 +00001401 operation. It is different than a->name when a->name is a
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001402 dotted package name (e.g. spam.eggs)
1403 */
1404 PyObject *store_name;
1405 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1406 const char *base = PyString_AS_STRING(name);
1407 char *dot = strchr(base, '.');
1408 if (dot) {
1409 store_name = PyString_FromStringAndSize(base, dot - base);
1410 if (!store_name)
1411 return 0;
1412 }
1413 else {
1414 store_name = name;
1415 Py_INCREF(store_name);
1416 }
1417 if (strcmp(PyString_AS_STRING(name), "*")) {
1418 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1419 Py_DECREF(store_name);
1420 return r;
1421 }
1422 else {
1423 if (st->st_cur->ste_type != ModuleBlock) {
1424 int lineno = st->st_cur->ste_lineno;
1425 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
1426 Py_DECREF(store_name);
1427 return 0;
1428 }
1429 }
1430 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1431 Py_DECREF(store_name);
1432 return 1;
1433 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434}
1435
1436
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001437static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1439{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001440 VISIT(st, expr, lc->target);
1441 VISIT(st, expr, lc->iter);
1442 VISIT_SEQ(st, expr, lc->ifs);
1443 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444}
1445
1446
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001447static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448symtable_visit_keyword(struct symtable *st, keyword_ty k)
1449{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001450 VISIT(st, expr, k->value);
1451 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452}
1453
1454
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001455static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456symtable_visit_slice(struct symtable *st, slice_ty s)
1457{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001458 switch (s->kind) {
1459 case Slice_kind:
1460 if (s->v.Slice.lower)
1461 VISIT(st, expr, s->v.Slice.lower)
1462 if (s->v.Slice.upper)
1463 VISIT(st, expr, s->v.Slice.upper)
1464 if (s->v.Slice.step)
1465 VISIT(st, expr, s->v.Slice.step)
1466 break;
1467 case ExtSlice_kind:
1468 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1469 break;
1470 case Index_kind:
1471 VISIT(st, expr, s->v.Index.value)
1472 break;
1473 case Ellipsis_kind:
1474 break;
1475 }
1476 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477}
1478
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001479static int
1480symtable_new_tmpname(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001482 char tmpname[256];
1483 identifier tmp;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001484
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001485 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1486 ++st->st_cur->ste_tmpname);
1487 tmp = PyString_InternFromString(tmpname);
1488 if (!tmp)
1489 return 0;
1490 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1491 return 0;
1492 Py_DECREF(tmp);
1493 return 1;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001494}
1495
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001496static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001497symtable_handle_comprehension(struct symtable *st, expr_ty e,
1498 identifier scope_name, asdl_seq *generators,
1499 expr_ty elt, expr_ty value)
1500{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001501 int is_generator = (e->kind == GeneratorExp_kind);
1502 int needs_tmp = !is_generator;
1503 comprehension_ty outermost = ((comprehension_ty)
1504 asdl_seq_GET(generators, 0));
1505 /* Outermost iterator is evaluated in current scope */
1506 VISIT(st, expr, outermost->iter);
1507 /* Create comprehension scope for the rest */
1508 if (!scope_name ||
1509 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
1510 return 0;
1511 }
1512 st->st_cur->ste_generator = is_generator;
1513 /* Outermost iter is received as an argument */
1514 if (!symtable_implicit_arg(st, 0)) {
1515 symtable_exit_block(st, (void *)e);
1516 return 0;
1517 }
1518 /* Allocate temporary name if needed */
1519 if (needs_tmp && !symtable_new_tmpname(st)) {
1520 symtable_exit_block(st, (void *)e);
1521 return 0;
1522 }
1523 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1524 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1525 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1526 generators, 1, (void*)e);
1527 if (value)
1528 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1529 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1530 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531}
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001532
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001533static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001534symtable_visit_genexp(struct symtable *st, expr_ty e)
1535{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001536 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1537 e->v.GeneratorExp.generators,
1538 e->v.GeneratorExp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001539}
1540
1541static int
1542symtable_visit_setcomp(struct symtable *st, expr_ty e)
1543{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001544 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1545 e->v.SetComp.generators,
1546 e->v.SetComp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001547}
1548
1549static int
1550symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1551{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001552 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1553 e->v.DictComp.generators,
1554 e->v.DictComp.key,
1555 e->v.DictComp.value);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001556}