blob: 92b223276fcc4b5cae5c476e975288a3a7f062b6 [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 Hylton81e95022007-02-27 06:50:52 +000011#define NONLOCAL_AFTER_ASSIGN \
12"name '%.400s' is assigned to before nonlocal declaration"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_USE \
15"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_USE \
18"name '%.400s' is used prior to nonlocal declaration"
19
Neal Norwitz5d0ad502005-12-19 04:27:42 +000020#define IMPORT_STAR_WARNING "import * only allowed at module level"
21
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000022#define RETURN_VAL_IN_GENERATOR \
23 "'return' with argument inside generator"
Neal Norwitz5d0ad502005-12-19 04:27:42 +000024
Benjamin Peterson55e00f22008-08-17 18:02:44 +000025
Neal Norwitz090b3dd2006-02-28 22:36:46 +000026static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000027ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000029{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000030 PySTEntryObject *ste = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000031 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000033 k = PyLong_FromVoidPtr(key);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000034 if (k == NULL)
35 goto fail;
Christian Heimes08976cb2008-03-16 00:32:36 +000036 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
37 if (ste == NULL)
38 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000039 ste->ste_table = st;
40 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043 ste->ste_name = name;
44 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000045
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000046 ste->ste_symbols = NULL;
47 ste->ste_varnames = NULL;
48 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000049
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000050 ste->ste_symbols = PyDict_New();
51 if (ste->ste_symbols == NULL)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000052 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000053
54 ste->ste_varnames = PyList_New(0);
55 if (ste->ste_varnames == NULL)
56 goto fail;
57
58 ste->ste_children = PyList_New(0);
59 if (ste->ste_children == NULL)
60 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000062 ste->ste_type = block;
63 ste->ste_unoptimized = 0;
64 ste->ste_nested = 0;
65 ste->ste_free = 0;
66 ste->ste_varargs = 0;
67 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000068 ste->ste_opt_lineno = 0;
Jeremy Hylton4d508ad2003-05-21 17:34:50 +000069 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000071
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000072 if (st->st_cur != NULL &&
73 (st->st_cur->ste_nested ||
74 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000075 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000076 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000077 ste->ste_generator = 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000078 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000079
Nick Coghlan650f0d02007-04-15 12:05:43 +000080 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000081 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000082
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084 fail:
85 Py_XDECREF(ste);
86 return NULL;
87}
88
89static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000090ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000091{
Walter Dörwald5d4ede12007-06-11 16:03:16 +000092 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
93 ste->ste_name,
Christian Heimes217cfd12007-12-02 14:31:20 +000094 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000095}
96
97static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000099{
100 ste->ste_table = NULL;
101 Py_XDECREF(ste->ste_id);
102 Py_XDECREF(ste->ste_name);
103 Py_XDECREF(ste->ste_symbols);
104 Py_XDECREF(ste->ste_varnames);
105 Py_XDECREF(ste->ste_children);
106 PyObject_Del(ste);
107}
108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000110
Guido van Rossum6f799372001-09-20 20:46:19 +0000111static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000112 {"id", T_OBJECT, OFF(ste_id), READONLY},
113 {"name", T_OBJECT, OFF(ste_name), READONLY},
114 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
115 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
116 {"children", T_OBJECT, OFF(ste_children), READONLY},
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000117 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
118 {"nested", T_INT, OFF(ste_nested), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119 {"type", T_INT, OFF(ste_type), READONLY},
120 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121 {NULL}
122};
123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124PyTypeObject PySTEntry_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000125 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000128 0,
129 (destructor)ste_dealloc, /* tp_dealloc */
130 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000131 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000132 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000133 0, /* tp_reserved */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000134 (reprfunc)ste_repr, /* tp_repr */
135 0, /* tp_as_number */
136 0, /* tp_as_sequence */
137 0, /* tp_as_mapping */
138 0, /* tp_hash */
139 0, /* tp_call */
140 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000141 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000142 0, /* tp_setattro */
143 0, /* tp_as_buffer */
144 Py_TPFLAGS_DEFAULT, /* tp_flags */
145 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000146 0, /* tp_traverse */
147 0, /* tp_clear */
148 0, /* tp_richcompare */
149 0, /* tp_weaklistoffset */
150 0, /* tp_iter */
151 0, /* tp_iternext */
152 0, /* tp_methods */
153 ste_memberlist, /* tp_members */
154 0, /* tp_getset */
155 0, /* tp_base */
156 0, /* tp_dict */
157 0, /* tp_descr_get */
158 0, /* tp_descr_set */
159 0, /* tp_dictoffset */
160 0, /* tp_init */
161 0, /* tp_alloc */
162 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000163};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164
165static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000166static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000168 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169static int symtable_exit_block(struct symtable *st, void *ast);
170static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
171static int symtable_visit_expr(struct symtable *st, expr_ty s);
172static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000173static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
174static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000175static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000176static int symtable_visit_arguments(struct symtable *st, arguments_ty);
177static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
178static int symtable_visit_alias(struct symtable *st, alias_ty);
179static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
180static int symtable_visit_keyword(struct symtable *st, keyword_ty);
181static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000182static int symtable_visit_params(struct symtable *st, asdl_seq *args);
183static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000185static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186
187
Nick Coghlan650f0d02007-04-15 12:05:43 +0000188static identifier top = NULL, lambda = NULL, genexpr = NULL,
Benjamin Petersondcaf3292009-03-03 00:54:05 +0000189 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
190 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
192#define GET_IDENTIFIER(VAR) \
Martin v. Löwis5b222132007-06-10 09:51:05 +0000193 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194
195#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000196"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
198static struct symtable *
199symtable_new(void)
200{
201 struct symtable *st;
202
203 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
204 if (st == NULL)
205 return NULL;
206
207 st->st_filename = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000208 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000209
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 if ((st->st_stack = PyList_New(0)) == NULL)
211 goto fail;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000212 if ((st->st_blocks = PyDict_New()) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213 goto fail;
214 st->st_cur = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215 st->st_private = NULL;
216 return st;
217 fail:
218 PySymtable_Free(st);
219 return NULL;
220}
221
222struct symtable *
223PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
224{
225 struct symtable *st = symtable_new();
226 asdl_seq *seq;
227 int i;
228
229 if (st == NULL)
230 return st;
231 st->st_filename = filename;
232 st->st_future = future;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000233 /* Make the initial symbol information gathering pass */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000234 if (!GET_IDENTIFIER(top) ||
235 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000236 PySymtable_Free(st);
237 return NULL;
238 }
239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 st->st_top = st->st_cur;
241 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242 switch (mod->kind) {
243 case Module_kind:
244 seq = mod->v.Module.body;
245 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246 if (!symtable_visit_stmt(st,
247 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 goto error;
249 break;
250 case Expression_kind:
251 if (!symtable_visit_expr(st, mod->v.Expression.body))
252 goto error;
253 break;
254 case Interactive_kind:
255 seq = mod->v.Interactive.body;
256 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257 if (!symtable_visit_stmt(st,
258 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259 goto error;
260 break;
261 case Suite_kind:
262 PyErr_SetString(PyExc_RuntimeError,
263 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000264 goto error;
265 }
266 if (!symtable_exit_block(st, (void *)mod)) {
267 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 return NULL;
269 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000270 /* Make the second symbol analysis pass */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271 if (symtable_analyze(st))
272 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000273 PySymtable_Free(st);
274 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000276 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277 PySymtable_Free(st);
278 return NULL;
279}
280
281void
282PySymtable_Free(struct symtable *st)
283{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000284 Py_XDECREF(st->st_blocks);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 Py_XDECREF(st->st_stack);
286 PyMem_Free((void *)st);
287}
288
289PySTEntryObject *
290PySymtable_Lookup(struct symtable *st, void *key)
291{
292 PyObject *k, *v;
293
294 k = PyLong_FromVoidPtr(key);
295 if (k == NULL)
296 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000297 v = PyDict_GetItem(st->st_blocks, k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298 if (v) {
299 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301 }
302 else {
303 PyErr_SetString(PyExc_KeyError,
304 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000306
307 Py_DECREF(k);
308 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309}
310
311int
312PyST_GetScope(PySTEntryObject *ste, PyObject *name)
313{
314 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
315 if (!v)
316 return 0;
Christian Heimes217cfd12007-12-02 14:31:20 +0000317 assert(PyLong_Check(v));
318 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319}
320
321
322/* Analyze raw symbol information to determine scope of each name.
323
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000324 The next several functions are helpers for symtable_analyze(),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325 which determines whether a name is local, global, or free. In addition,
326 it determines which local variables are cell variables; they provide
327 bindings that are used for free variables in enclosed blocks.
328
Nick Coghlan650f0d02007-04-15 12:05:43 +0000329 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330 explicit global is declared with the global statement. An implicit
331 global is a free variable for which the compiler has found no binding
332 in an enclosing function scope. The implicit global is either a global
333 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
334 to handle these names to implement slightly odd semantics. In such a
335 block, the name is treated as global until it is assigned to; then it
336 is treated as a local.
337
338 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000339 The first pass collects raw facts from the AST via the symtable_visit_*
340 functions: the name is a parameter here, the name is used but not defined
341 here, etc. The second pass analyzes these facts during a pass over the
342 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343
344 When a function is entered during the second pass, the parent passes
345 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000346 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000347 Names which are explicitly declared nonlocal must exist in this set of
348 visible names - if they do not, a syntax error is raised. After doing
349 the local analysis, it analyzes each of its child blocks using an
350 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351
Nick Coghlan650f0d02007-04-15 12:05:43 +0000352 The children update the free variable set. If a local variable is added to
353 the free variable set by the child, the variable is marked as a cell. The
354 function object being defined must provide runtime storage for the variable
355 that may outlive the function's frame. Cell variables are removed from the
356 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000357
Nick Coghlan650f0d02007-04-15 12:05:43 +0000358 During analysis, the names are:
359 symbols: dict mapping from symbol names to flag values (including offset scope values)
360 scopes: dict mapping from symbol names to scope values (no offset)
361 local: set of all symbol names local to the current scope
362 bound: set of all symbol names local to a containing function scope
363 free: set of all symbol names referenced but not bound in child scopes
364 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365*/
366
367#define SET_SCOPE(DICT, NAME, I) { \
Christian Heimes217cfd12007-12-02 14:31:20 +0000368 PyObject *o = PyLong_FromLong(I); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369 if (!o) \
370 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000371 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
372 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000374 } \
375 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376}
377
378/* Decide on scope of name, given flags.
379
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000380 The namespace dictionaries may be modified to record information
381 about the new name. For example, a new global will add an entry to
382 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383*/
384
385static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000386analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 PyObject *bound, PyObject *local, PyObject *free,
388 PyObject *global)
389{
390 if (flags & DEF_GLOBAL) {
391 if (flags & DEF_PARAM) {
392 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000393 "name '%U' is parameter and global",
394 name);
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000395 PyErr_SyntaxLocation(ste->ste_table->st_filename,
396 ste->ste_lineno);
397
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398 return 0;
399 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000400 if (flags & DEF_NONLOCAL) {
401 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000402 "name '%U' is nonlocal and global",
403 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000404 return 0;
405 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000406 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
407 if (PySet_Add(global, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000409 if (bound && (PySet_Discard(bound, name) < 0))
410 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000411 return 1;
412 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000413 if (flags & DEF_NONLOCAL) {
414 if (flags & DEF_PARAM) {
415 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000416 "name '%U' is parameter and nonlocal",
417 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000418 return 0;
419 }
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000420 if (!bound) {
421 PyErr_Format(PyExc_SyntaxError,
422 "nonlocal declaration not allowed at module level");
423 return 0;
424 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000425 if (!PySet_Contains(bound, name)) {
Jeremy Hylton81e95022007-02-27 06:50:52 +0000426 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000427 "no binding for nonlocal '%U' found",
428 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000429
430 return 0;
431 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000432 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000433 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000434 return PySet_Add(free, name) >= 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000435 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436 if (flags & DEF_BOUND) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000437 SET_SCOPE(scopes, name, LOCAL);
438 if (PySet_Add(local, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000439 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000440 if (PySet_Discard(global, name) < 0)
441 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442 return 1;
443 }
444 /* If an enclosing block has a binding for this name, it
445 is a free variable rather than a global variable.
446 Note that having a non-NULL bound implies that the block
447 is nested.
448 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000449 if (bound && PySet_Contains(bound, name)) {
450 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000452 return PySet_Add(free, name) >= 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453 }
454 /* If a parent has a global statement, then call it global
455 explicit? It could also be global implicit.
456 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000457 if (global && PySet_Contains(global, name)) {
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000458 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000459 return 1;
460 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000461 if (ste->ste_nested)
462 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000463 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000464 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465}
466
467#undef SET_SCOPE
468
469/* If a name is defined in free and also in locals, then this block
470 provides the binding for the free variable. The name should be
471 marked CELL in this block and removed from the free list.
472
473 Note that the current block's free variables are included in free.
474 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000475
Martin v. Löwis2673a572007-10-29 19:54:24 +0000476 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000477 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478*/
479
480static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000481analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000483 PyObject *name, *v, *v_cell;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000484 int success = 0;
485 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486
Christian Heimes217cfd12007-12-02 14:31:20 +0000487 v_cell = PyLong_FromLong(CELL);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000488 if (!v_cell)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000490 while (PyDict_Next(scopes, &pos, &name, &v)) {
491 long scope;
Christian Heimes217cfd12007-12-02 14:31:20 +0000492 assert(PyLong_Check(v));
493 scope = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000494 if (scope != LOCAL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495 continue;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000496 if (!PySet_Contains(free, name))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497 continue;
Martin v. Löwis2673a572007-10-29 19:54:24 +0000498 if (restricted != NULL &&
499 PyUnicode_CompareWithASCIIString(name, restricted))
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000500 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501 /* Replace LOCAL with CELL for this name, and remove
502 from free. It is safe to replace the value of name
503 in the dict, because it will not cause a resize.
504 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000505 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000507 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508 goto error;
509 }
510 success = 1;
511 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000512 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513 return success;
514}
515
516/* Check for illegal statements in unoptimized namespaces */
517static int
518check_unoptimized(const PySTEntryObject* ste) {
Armin Rigo31441302005-10-21 12:57:31 +0000519 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000521 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522 || !(ste->ste_free || ste->ste_child_free))
523 return 1;
524
Armin Rigo31441302005-10-21 12:57:31 +0000525 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 "contains a nested function with free variables" :
527 "is a nested function");
528
529 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000530 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 return 1;
532 case OPT_IMPORT_STAR:
Walter Dörwald3ef72bb2007-06-11 16:12:10 +0000533 PyErr_Format(PyExc_SyntaxError,
534 "import * is not allowed in function '%U' because it %s",
Walter Dörwalde42109d2007-06-11 16:08:41 +0000535 ste->ste_name, trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 }
538
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539 PyErr_SyntaxLocation(ste->ste_table->st_filename,
540 ste->ste_opt_lineno);
541 return 0;
542}
543
Nick Coghlan650f0d02007-04-15 12:05:43 +0000544/* Enter the final scope information into the ste_symbols dict.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 *
546 * All arguments are dicts. Modifies symbols, others are read-only.
547*/
548static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000549update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000552 PyObject *name = NULL, *itr = NULL;
553 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000554 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555
Nick Coghlan650f0d02007-04-15 12:05:43 +0000556 /* Update scope information for all symbols in this scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557 while (PyDict_Next(symbols, &pos, &name, &v)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000558 long scope, flags;
Christian Heimes217cfd12007-12-02 14:31:20 +0000559 assert(PyLong_Check(v));
560 flags = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000561 v_scope = PyDict_GetItem(scopes, name);
Christian Heimes217cfd12007-12-02 14:31:20 +0000562 assert(v_scope && PyLong_Check(v_scope));
563 scope = PyLong_AS_LONG(v_scope);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000564 flags |= (scope << SCOPE_OFFSET);
Christian Heimes217cfd12007-12-02 14:31:20 +0000565 v_new = PyLong_FromLong(flags);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000566 if (!v_new)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000567 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000568 if (PyDict_SetItem(symbols, name, v_new) < 0) {
569 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 return 0;
571 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000572 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 }
574
Nick Coghlan650f0d02007-04-15 12:05:43 +0000575 /* Record not yet resolved free variables from children (if any) */
Christian Heimes217cfd12007-12-02 14:31:20 +0000576 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000577 if (!v_free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 return 0;
579
Nick Coghlan650f0d02007-04-15 12:05:43 +0000580 itr = PyObject_GetIter(free);
581 if (!itr)
582 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583
Nick Coghlan650f0d02007-04-15 12:05:43 +0000584 while ((name = PyIter_Next(itr))) {
585 v = PyDict_GetItem(symbols, name);
586
587 /* Handle symbol that already exists in this scope */
588 if (v) {
589 /* Handle a free variable in a method of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590 the class that has the same name as a local
591 or global in the class scope.
592 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000593 if (classflag &&
Christian Heimes217cfd12007-12-02 14:31:20 +0000594 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
595 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
596 v_new = PyLong_FromLong(flags);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000597 if (!v_new) {
598 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000600 if (PyDict_SetItem(symbols, name, v_new) < 0) {
601 Py_DECREF(v_new);
602 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000604 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000606 /* It's a cell, or already free in this scope */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000607 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 continue;
609 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000610 /* Handle global symbol */
611 if (!PySet_Contains(bound, name)) {
612 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 continue; /* it's a global */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000615 /* Propagate new free symbol up the lexical stack */
616 if (PyDict_SetItem(symbols, name, v_free) < 0) {
617 goto error;
618 }
619 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000621 Py_DECREF(itr);
622 Py_DECREF(v_free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000624error:
625 Py_XDECREF(v_free);
626 Py_XDECREF(itr);
627 Py_XDECREF(name);
628 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629}
630
631/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000632
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 Arguments:
634 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000635 bound -- set of variables bound in enclosing scopes (input). bound
636 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 free -- set of free variables in enclosed scopes (output)
638 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000639
640 The implementation uses two mutually recursive functions,
641 analyze_block() and analyze_child_block(). analyze_block() is
642 responsible for analyzing the individual names defined in a block.
643 analyze_child_block() prepares temporary namespace dictionaries
644 used to evaluated nested blocks.
645
646 The two functions exist because a child block should see the name
647 bindings of its enclosing blocks, but those bindings should not
648 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649*/
650
651static int
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000652analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
653 PyObject *global, PyObject* child_free);
654
655static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
657 PyObject *global)
658{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000659 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000660 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000661 int i, success = 0;
662 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000664 local = PySet_New(NULL); /* collect new names bound in block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 if (!local)
666 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000667 scopes = PyDict_New(); /* collect scopes defined for each name */
668 if (!scopes)
669 goto error;
670
671 /* Allocate new global and bound variable dictionaries. These
672 dictionaries hold the names visible in nested blocks. For
673 ClassBlocks, the bound and global names are initialized
674 before analyzing names, because class bindings aren't
675 visible in methods. For other blocks, they are initialized
676 after names are analyzed.
677 */
678
679 /* TODO(jhylton): Package these dicts in a struct so that we
680 can write reasonable helper functions?
681 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000682 newglobal = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683 if (!newglobal)
684 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000685 newfree = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 if (!newfree)
687 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000688 newbound = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 if (!newbound)
690 goto error;
691
Nick Coghlan650f0d02007-04-15 12:05:43 +0000692 /* Class namespace has no effect on names visible in
693 nested functions, so populate the global and bound
694 sets to be passed to child blocks before analyzing
695 this one.
696 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 if (ste->ste_type == ClassBlock) {
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000698 /* Pass down known globals */
699 if (!PyNumber_InPlaceOr(newglobal, global))
700 goto error;
701 Py_DECREF(newglobal);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000702 /* Pass down previously bound symbols */
703 if (bound) {
704 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000706 Py_DECREF(newbound);
707 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708 }
709
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000711 long flags = PyLong_AS_LONG(v);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000712 if (!analyze_name(ste, scopes, name, flags,
713 bound, local, free, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714 goto error;
715 }
716
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000717 /* Populate global and bound sets to be passed to children. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000719 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 if (ste->ste_type == FunctionBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000721 if (!PyNumber_InPlaceOr(newbound, local))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000723 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000725 /* Pass down previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 if (bound) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000727 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000729 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000731 /* Pass down known globals */
732 if (!PyNumber_InPlaceOr(newglobal, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000734 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000736 else {
737 /* Special-case __class__ */
738 if (!GET_IDENTIFIER(__class__))
739 goto error;
740 assert(PySet_Contains(local, __class__) == 1);
741 if (PySet_Add(newbound, __class__) < 0)
742 goto error;
743 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000745 /* Recursively call analyze_block() on each child block.
746
747 newbound, newglobal now contain the names visible in
748 nested blocks. The free variables in the children will
749 be collected in allfree.
750 */
751 allfree = PySet_New(NULL);
752 if (!allfree)
753 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
755 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000756 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000758 entry = (PySTEntryObject*)c;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000759 if (!analyze_child_block(entry, newbound, newfree, newglobal,
760 allfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000762 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763 if (entry->ste_free || entry->ste_child_free)
764 ste->ste_child_free = 1;
765 }
766
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000767 if (PyNumber_InPlaceOr(newfree, allfree) < 0)
768 goto error;
Benjamin Peterson22081a12009-04-02 01:50:37 +0000769 Py_DECREF(newfree);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000770
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000771 /* Check if any local variables must be converted to cell variables */
772 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
773 NULL))
774 goto error;
775 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
776 "__class__"))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000778 /* Records the results of the analysis in the symbol table entry */
779 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 ste->ste_type == ClassBlock))
781 goto error;
782 if (!check_unoptimized(ste))
783 goto error;
784
Nick Coghlan650f0d02007-04-15 12:05:43 +0000785 if (!PyNumber_InPlaceOr(free, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000787 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 success = 1;
789 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000790 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 Py_XDECREF(newbound);
793 Py_XDECREF(newglobal);
794 Py_XDECREF(newfree);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000795 Py_XDECREF(allfree);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 if (!success)
797 assert(PyErr_Occurred());
798 return success;
799}
800
801static int
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000802analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
803 PyObject *global, PyObject* child_free)
804{
805 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
806 int success = 0;
807
808 /* Copy the bound and global dictionaries.
809
810 These dictionary are used by all blocks enclosed by the
811 current block. The analyze_block() call modifies these
812 dictionaries.
813
814 */
815 temp_bound = PySet_New(bound);
816 if (!temp_bound)
817 goto error;
818 temp_free = PySet_New(free);
819 if (!temp_free)
820 goto error;
821 temp_global = PySet_New(global);
822 if (!temp_global)
823 goto error;
824
825 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
826 goto error;
827 success = PyNumber_InPlaceOr(child_free, temp_free) >= 0;
Benjamin Peterson22081a12009-04-02 01:50:37 +0000828 Py_DECREF(child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000829 success = 1;
830 error:
831 Py_XDECREF(temp_bound);
832 Py_XDECREF(temp_free);
833 Py_XDECREF(temp_global);
834 return success;
835}
836
837static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838symtable_analyze(struct symtable *st)
839{
840 PyObject *free, *global;
841 int r;
842
Nick Coghlan650f0d02007-04-15 12:05:43 +0000843 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 if (!free)
845 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000846 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000848 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 return 0;
850 }
851 r = analyze_block(st->st_top, NULL, free, global);
852 Py_DECREF(free);
853 Py_DECREF(global);
854 return r;
855}
856
857
858static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000859symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860{
861 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000862 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
864 PyErr_SetString(PyExc_SyntaxError, msg);
865 PyErr_SyntaxLocation(st->st_filename,
866 st->st_cur->ste_lineno);
867 }
868 return 0;
869 }
870 return 1;
871}
872
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000873/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874 This reference is released when the block is exited, via the DECREF
875 in symtable_exit_block().
876*/
877
878static int
879symtable_exit_block(struct symtable *st, void *ast)
880{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000881 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000883 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 end = PyList_GET_SIZE(st->st_stack) - 1;
885 if (end >= 0) {
886 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
887 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000888 if (st->st_cur == NULL)
889 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890 Py_INCREF(st->st_cur);
891 if (PySequence_DelItem(st->st_stack, end) < 0)
892 return 0;
893 }
894 return 1;
895}
896
897static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000898symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899 void *ast, int lineno)
900{
901 PySTEntryObject *prev = NULL;
902
903 if (st->st_cur) {
904 prev = st->st_cur;
905 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 return 0;
907 }
908 Py_DECREF(st->st_cur);
909 }
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000910 st->st_cur = ste_new(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000911 if (st->st_cur == NULL)
912 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913 if (name == GET_IDENTIFIER(top))
914 st->st_global = st->st_cur->ste_symbols;
915 if (prev) {
916 if (PyList_Append(prev->ste_children,
917 (PyObject *)st->st_cur) < 0) {
918 return 0;
919 }
920 }
921 return 1;
922}
923
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000924static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925symtable_lookup(struct symtable *st, PyObject *name)
926{
927 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000928 PyObject *mangled = _Py_Mangle(st->st_private, name);
929 if (!mangled)
930 return 0;
931 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
932 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933 if (!o)
934 return 0;
Christian Heimes217cfd12007-12-02 14:31:20 +0000935 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936}
937
938static int
939symtable_add_def(struct symtable *st, PyObject *name, int flag)
940{
941 PyObject *o;
942 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000943 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000944 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945
Jeremy Hylton81e95022007-02-27 06:50:52 +0000946
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000947 if (!mangled)
948 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000950 if ((o = PyDict_GetItem(dict, mangled))) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000951 val = PyLong_AS_LONG(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000953 /* Is it better to use 'mangled' or 'name' here? */
Neal Norwitza5d16a32007-08-24 22:53:58 +0000954 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 PyErr_SyntaxLocation(st->st_filename,
956 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000957 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958 }
959 val |= flag;
960 } else
961 val = flag;
Christian Heimes217cfd12007-12-02 14:31:20 +0000962 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000964 goto error;
965 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000967 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968 }
969 Py_DECREF(o);
970
971 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000972 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
973 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 } else if (flag & DEF_GLOBAL) {
975 /* XXX need to update DEF_GLOBAL for other flags too;
976 perhaps only DEF_FREE_GLOBAL */
977 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000978 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000979 val |= PyLong_AS_LONG(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000981 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000983 goto error;
984 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000986 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 }
988 Py_DECREF(o);
989 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000990 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000992
993error:
994 Py_DECREF(mangled);
995 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996}
997
998/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
999 They use the ASDL name to synthesize the name of the C type and the visit
1000 function.
1001
1002 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1003 useful if the first node in the sequence requires special treatment.
1004*/
1005
1006#define VISIT(ST, TYPE, V) \
1007 if (!symtable_visit_ ## TYPE((ST), (V))) \
1008 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001009
1010#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
1011 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1012 symtable_exit_block((ST), (S)); \
1013 return 0; \
1014 }
1015
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016#define VISIT_SEQ(ST, TYPE, SEQ) { \
1017 int i; \
1018 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1019 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001020 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 if (!symtable_visit_ ## TYPE((ST), elt)) \
1022 return 0; \
1023 } \
1024}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001025
1026#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
1027 int i; \
1028 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1029 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001030 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001031 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1032 symtable_exit_block((ST), (S)); \
1033 return 0; \
1034 } \
1035 } \
1036}
1037
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
1039 int i; \
1040 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1041 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001042 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 if (!symtable_visit_ ## TYPE((ST), elt)) \
1044 return 0; \
1045 } \
1046}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001047
1048#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
1049 int i; \
1050 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1051 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001052 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001053 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1054 symtable_exit_block((ST), (S)); \
1055 return 0; \
1056 } \
1057 } \
1058}
1059
Guido van Rossum4f72a782006-10-27 23:31:49 +00001060#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
1061 int i = 0; \
1062 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1063 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1064 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1065 if (!elt) continue; /* can be NULL */ \
1066 if (!symtable_visit_expr((ST), elt)) \
1067 return 0; \
1068 } \
1069}
1070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001072symtable_new_tmpname(struct symtable *st)
1073{
1074 char tmpname[256];
1075 identifier tmp;
1076
1077 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1078 ++st->st_cur->ste_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001079 tmp = PyUnicode_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001080 if (!tmp)
1081 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001082 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1083 return 0;
1084 Py_DECREF(tmp);
1085 return 1;
1086}
1087
Guido van Rossum4f72a782006-10-27 23:31:49 +00001088
1089
Guido van Rossumc2e20742006-02-27 22:32:47 +00001090static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091symtable_visit_stmt(struct symtable *st, stmt_ty s)
1092{
1093 switch (s->kind) {
1094 case FunctionDef_kind:
1095 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1096 return 0;
1097 if (s->v.FunctionDef.args->defaults)
1098 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001099 if (s->v.FunctionDef.args->kw_defaults)
1100 VISIT_KWONLYDEFAULTS(st,
1101 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001102 if (!symtable_visit_annotations(st, s))
1103 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001104 if (s->v.FunctionDef.decorator_list)
1105 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1107 FunctionBlock, (void *)s, s->lineno))
1108 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001109 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1110 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 if (!symtable_exit_block(st, s))
1112 return 0;
1113 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001114 case ClassDef_kind: {
1115 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1117 return 0;
1118 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001119 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1120 if (s->v.ClassDef.starargs)
1121 VISIT(st, expr, s->v.ClassDef.starargs);
1122 if (s->v.ClassDef.kwargs)
1123 VISIT(st, expr, s->v.ClassDef.kwargs);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001124 if (s->v.ClassDef.decorator_list)
1125 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1127 (void *)s, s->lineno))
1128 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001129 if (!GET_IDENTIFIER(__class__) ||
Benjamin Petersondcaf3292009-03-03 00:54:05 +00001130 !symtable_add_def(st, __class__, DEF_LOCAL) ||
1131 !GET_IDENTIFIER(__locals__) ||
1132 !symtable_add_def(st, __locals__, DEF_PARAM)) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001133 symtable_exit_block(st, s);
1134 return 0;
1135 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001136 tmp = st->st_private;
1137 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001138 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001139 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 if (!symtable_exit_block(st, s))
1141 return 0;
1142 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001143 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001145 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001147 st->st_cur->ste_returns_value = 1;
1148 if (st->st_cur->ste_generator) {
1149 PyErr_SetString(PyExc_SyntaxError,
1150 RETURN_VAL_IN_GENERATOR);
1151 PyErr_SyntaxLocation(st->st_filename,
1152 s->lineno);
1153 return 0;
1154 }
1155 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 break;
1157 case Delete_kind:
1158 VISIT_SEQ(st, expr, s->v.Delete.targets);
1159 break;
1160 case Assign_kind:
1161 VISIT_SEQ(st, expr, s->v.Assign.targets);
1162 VISIT(st, expr, s->v.Assign.value);
1163 break;
1164 case AugAssign_kind:
1165 VISIT(st, expr, s->v.AugAssign.target);
1166 VISIT(st, expr, s->v.AugAssign.value);
1167 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 case For_kind:
1169 VISIT(st, expr, s->v.For.target);
1170 VISIT(st, expr, s->v.For.iter);
1171 VISIT_SEQ(st, stmt, s->v.For.body);
1172 if (s->v.For.orelse)
1173 VISIT_SEQ(st, stmt, s->v.For.orelse);
1174 break;
1175 case While_kind:
1176 VISIT(st, expr, s->v.While.test);
1177 VISIT_SEQ(st, stmt, s->v.While.body);
1178 if (s->v.While.orelse)
1179 VISIT_SEQ(st, stmt, s->v.While.orelse);
1180 break;
1181 case If_kind:
1182 /* XXX if 0: and lookup_yield() hacks */
1183 VISIT(st, expr, s->v.If.test);
1184 VISIT_SEQ(st, stmt, s->v.If.body);
1185 if (s->v.If.orelse)
1186 VISIT_SEQ(st, stmt, s->v.If.orelse);
1187 break;
1188 case Raise_kind:
Collin Winter828f04a2007-08-31 00:04:24 +00001189 if (s->v.Raise.exc) {
1190 VISIT(st, expr, s->v.Raise.exc);
1191 if (s->v.Raise.cause) {
1192 VISIT(st, expr, s->v.Raise.cause);
1193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 }
1195 break;
1196 case TryExcept_kind:
1197 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1198 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1199 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1200 break;
1201 case TryFinally_kind:
1202 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1203 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1204 break;
1205 case Assert_kind:
1206 VISIT(st, expr, s->v.Assert.test);
1207 if (s->v.Assert.msg)
1208 VISIT(st, expr, s->v.Assert.msg);
1209 break;
1210 case Import_kind:
1211 VISIT_SEQ(st, alias, s->v.Import.names);
1212 /* XXX Don't have the lineno available inside
1213 visit_alias */
1214 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1215 st->st_cur->ste_opt_lineno = s->lineno;
1216 break;
1217 case ImportFrom_kind:
1218 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1219 /* XXX Don't have the lineno available inside
1220 visit_alias */
1221 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1222 st->st_cur->ste_opt_lineno = s->lineno;
1223 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 case Global_kind: {
1225 int i;
1226 asdl_seq *seq = s->v.Global.names;
1227 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001228 identifier name = (identifier)asdl_seq_GET(seq, i);
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001229 char *c_name = _PyUnicode_AsString(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001230 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 if (cur < 0)
1232 return 0;
1233 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001234 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 if (cur & DEF_LOCAL)
1236 PyOS_snprintf(buf, sizeof(buf),
1237 GLOBAL_AFTER_ASSIGN,
1238 c_name);
1239 else
1240 PyOS_snprintf(buf, sizeof(buf),
1241 GLOBAL_AFTER_USE,
1242 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001243 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 return 0;
1245 }
1246 if (!symtable_add_def(st, name, DEF_GLOBAL))
1247 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 break;
1250 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001251 case Nonlocal_kind: {
1252 int i;
1253 asdl_seq *seq = s->v.Nonlocal.names;
1254 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1255 identifier name = (identifier)asdl_seq_GET(seq, i);
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001256 char *c_name = _PyUnicode_AsString(name);
Jeremy Hylton81e95022007-02-27 06:50:52 +00001257 long cur = symtable_lookup(st, name);
1258 if (cur < 0)
1259 return 0;
1260 if (cur & (DEF_LOCAL | USE)) {
1261 char buf[256];
1262 if (cur & DEF_LOCAL)
1263 PyOS_snprintf(buf, sizeof(buf),
1264 NONLOCAL_AFTER_ASSIGN,
1265 c_name);
1266 else
1267 PyOS_snprintf(buf, sizeof(buf),
1268 NONLOCAL_AFTER_USE,
1269 c_name);
1270 if (!symtable_warn(st, buf, s->lineno))
1271 return 0;
1272 }
1273 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1274 return 0;
1275 }
1276 break;
1277 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 case Expr_kind:
1279 VISIT(st, expr, s->v.Expr.value);
1280 break;
1281 case Pass_kind:
1282 case Break_kind:
1283 case Continue_kind:
1284 /* nothing to do here */
1285 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001286 case With_kind:
1287 if (!symtable_new_tmpname(st))
1288 return 0;
1289 VISIT(st, expr, s->v.With.context_expr);
1290 if (s->v.With.optional_vars) {
1291 if (!symtable_new_tmpname(st))
1292 return 0;
1293 VISIT(st, expr, s->v.With.optional_vars);
1294 }
1295 VISIT_SEQ(st, stmt, s->v.With.body);
1296 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297 }
1298 return 1;
1299}
1300
1301static int
1302symtable_visit_expr(struct symtable *st, expr_ty e)
1303{
1304 switch (e->kind) {
1305 case BoolOp_kind:
1306 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1307 break;
1308 case BinOp_kind:
1309 VISIT(st, expr, e->v.BinOp.left);
1310 VISIT(st, expr, e->v.BinOp.right);
1311 break;
1312 case UnaryOp_kind:
1313 VISIT(st, expr, e->v.UnaryOp.operand);
1314 break;
1315 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001316 if (!GET_IDENTIFIER(lambda) ||
1317 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 return 0;
1319 if (e->v.Lambda.args->defaults)
1320 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1321 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001322 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 FunctionBlock, (void *)e, 0))
1324 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001325 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1326 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 if (!symtable_exit_block(st, (void *)e))
1328 return 0;
1329 break;
1330 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001331 case IfExp_kind:
1332 VISIT(st, expr, e->v.IfExp.test);
1333 VISIT(st, expr, e->v.IfExp.body);
1334 VISIT(st, expr, e->v.IfExp.orelse);
1335 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336 case Dict_kind:
1337 VISIT_SEQ(st, expr, e->v.Dict.keys);
1338 VISIT_SEQ(st, expr, e->v.Dict.values);
1339 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001340 case Set_kind:
1341 VISIT_SEQ(st, expr, e->v.Set.elts);
1342 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001343 case GeneratorExp_kind:
1344 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001347 case ListComp_kind:
1348 if (!symtable_visit_listcomp(st, e))
1349 return 0;
1350 break;
1351 case SetComp_kind:
1352 if (!symtable_visit_setcomp(st, e))
1353 return 0;
1354 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001355 case DictComp_kind:
1356 if (!symtable_visit_dictcomp(st, e))
1357 return 0;
1358 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 case Yield_kind:
1360 if (e->v.Yield.value)
1361 VISIT(st, expr, e->v.Yield.value);
1362 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001363 if (st->st_cur->ste_returns_value) {
1364 PyErr_SetString(PyExc_SyntaxError,
1365 RETURN_VAL_IN_GENERATOR);
1366 PyErr_SyntaxLocation(st->st_filename,
1367 e->lineno);
1368 return 0;
1369 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 break;
1371 case Compare_kind:
1372 VISIT(st, expr, e->v.Compare.left);
1373 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1374 break;
1375 case Call_kind:
1376 VISIT(st, expr, e->v.Call.func);
1377 VISIT_SEQ(st, expr, e->v.Call.args);
1378 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1379 if (e->v.Call.starargs)
1380 VISIT(st, expr, e->v.Call.starargs);
1381 if (e->v.Call.kwargs)
1382 VISIT(st, expr, e->v.Call.kwargs);
1383 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384 case Num_kind:
1385 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001386 case Bytes_kind:
1387 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 /* Nothing to do here. */
1389 break;
1390 /* The following exprs can be assignment targets. */
1391 case Attribute_kind:
1392 VISIT(st, expr, e->v.Attribute.value);
1393 break;
1394 case Subscript_kind:
1395 VISIT(st, expr, e->v.Subscript.value);
1396 VISIT(st, slice, e->v.Subscript.slice);
1397 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001398 case Starred_kind:
1399 VISIT(st, expr, e->v.Starred.value);
1400 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 case Name_kind:
1402 if (!symtable_add_def(st, e->v.Name.id,
1403 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1404 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001405 /* Special-case super: it counts as a use of __class__ */
1406 if (e->v.Name.ctx == Load &&
1407 st->st_cur->ste_type == FunctionBlock &&
1408 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1409 if (!GET_IDENTIFIER(__class__) ||
1410 !symtable_add_def(st, __class__, USE))
1411 return 0;
1412 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 break;
1414 /* child nodes of List and Tuple will have expr_context set */
1415 case List_kind:
1416 VISIT_SEQ(st, expr, e->v.List.elts);
1417 break;
1418 case Tuple_kind:
1419 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1420 break;
1421 }
1422 return 1;
1423}
1424
1425static int
1426symtable_implicit_arg(struct symtable *st, int pos)
1427{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001428 PyObject *id = PyUnicode_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 if (id == NULL)
1430 return 0;
1431 if (!symtable_add_def(st, id, DEF_PARAM)) {
1432 Py_DECREF(id);
1433 return 0;
1434 }
1435 Py_DECREF(id);
1436 return 1;
1437}
1438
1439static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001440symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001442 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001443
1444 if (!args)
1445 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001448 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001449 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 return 0;
1451 }
1452
1453 return 1;
1454}
1455
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001456static int
1457symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458{
1459 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001460
1461 if (!args)
1462 return -1;
1463
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001465 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001466 if (arg->annotation)
1467 VISIT(st, expr, arg->annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001469
1470 return 1;
1471}
1472
Neal Norwitzc1505362006-12-28 06:47:50 +00001473static int
1474symtable_visit_annotations(struct symtable *st, stmt_ty s)
1475{
1476 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001478 if (a->args && !symtable_visit_argannotations(st, a->args))
Neal Norwitzc1505362006-12-28 06:47:50 +00001479 return 0;
1480 if (a->varargannotation)
1481 VISIT(st, expr, a->varargannotation);
1482 if (a->kwargannotation)
1483 VISIT(st, expr, a->kwargannotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001484 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
Neal Norwitzc1505362006-12-28 06:47:50 +00001485 return 0;
1486 if (s->v.FunctionDef.returns)
1487 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488 return 1;
1489}
1490
1491static int
1492symtable_visit_arguments(struct symtable *st, arguments_ty a)
1493{
1494 /* skip default arguments inside function block
1495 XXX should ast be different?
1496 */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001497 if (a->args && !symtable_visit_params(st, a->args))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001499 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001500 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 if (a->vararg) {
1502 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1503 return 0;
1504 st->st_cur->ste_varargs = 1;
1505 }
1506 if (a->kwarg) {
1507 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1508 return 0;
1509 st->st_cur->ste_varkeywords = 1;
1510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 return 1;
1512}
1513
1514
1515static int
1516symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1517{
Neal Norwitzad74aa82008-03-31 05:14:30 +00001518 if (eh->v.ExceptHandler.type)
1519 VISIT(st, expr, eh->v.ExceptHandler.type);
1520 if (eh->v.ExceptHandler.name)
1521 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
Guido van Rossum16be03e2007-01-10 18:51:35 +00001522 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00001523 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 return 1;
1525}
1526
1527
1528static int
1529symtable_visit_alias(struct symtable *st, alias_ty a)
1530{
1531 /* Compute store_name, the name actually bound by the import
1532 operation. It is diferent than a->name when a->name is a
1533 dotted package name (e.g. spam.eggs)
1534 */
1535 PyObject *store_name;
1536 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001537 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1538 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001539 if (dot) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001540 store_name = PyUnicode_FromUnicode(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001541 if (!store_name)
1542 return 0;
1543 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 else {
1545 store_name = name;
1546 Py_INCREF(store_name);
1547 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001548 if (PyUnicode_CompareWithASCIIString(name, "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1550 Py_DECREF(store_name);
1551 return r;
1552 }
1553 else {
1554 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001555 int lineno = st->st_cur->ste_lineno;
Guido van Rossum33d26892007-08-05 15:29:28 +00001556 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1557 PyErr_SyntaxLocation(st->st_filename, lineno);
1558 Py_DECREF(store_name);
1559 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560 }
1561 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001562 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 return 1;
1564 }
1565}
1566
1567
1568static int
1569symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1570{
1571 VISIT(st, expr, lc->target);
1572 VISIT(st, expr, lc->iter);
1573 VISIT_SEQ(st, expr, lc->ifs);
1574 return 1;
1575}
1576
1577
1578static int
1579symtable_visit_keyword(struct symtable *st, keyword_ty k)
1580{
1581 VISIT(st, expr, k->value);
1582 return 1;
1583}
1584
1585
1586static int
1587symtable_visit_slice(struct symtable *st, slice_ty s)
1588{
1589 switch (s->kind) {
1590 case Slice_kind:
1591 if (s->v.Slice.lower)
1592 VISIT(st, expr, s->v.Slice.lower)
1593 if (s->v.Slice.upper)
1594 VISIT(st, expr, s->v.Slice.upper)
1595 if (s->v.Slice.step)
1596 VISIT(st, expr, s->v.Slice.step)
1597 break;
1598 case ExtSlice_kind:
1599 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1600 break;
1601 case Index_kind:
1602 VISIT(st, expr, s->v.Index.value)
1603 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604 }
1605 return 1;
1606}
1607
1608static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001609symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001610 identifier scope_name, asdl_seq *generators,
1611 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001613 int is_generator = (e->kind == GeneratorExp_kind);
1614 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001616 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 /* Outermost iterator is evaluated in current scope */
1618 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001619 /* Create comprehension scope for the rest */
1620 if (!scope_name ||
1621 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622 return 0;
1623 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001624 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625 /* Outermost iter is received as an argument */
1626 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001627 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628 return 0;
1629 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001630 /* Allocate temporary name if needed */
1631 if (needs_tmp && !symtable_new_tmpname(st)) {
1632 symtable_exit_block(st, (void *)e);
1633 return 0;
1634 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001635 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1636 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1637 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001638 generators, 1, (void*)e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001639 if (value)
1640 VISIT_IN_BLOCK(st, expr, value, (void*)e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001641 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001642 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001644
1645static int
1646symtable_visit_genexp(struct symtable *st, expr_ty e)
1647{
1648 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1649 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001650 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001651}
1652
1653static int
1654symtable_visit_listcomp(struct symtable *st, expr_ty e)
1655{
1656 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1657 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001658 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001659}
1660
1661static int
1662symtable_visit_setcomp(struct symtable *st, expr_ty e)
1663{
1664 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1665 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001666 e->v.SetComp.elt, NULL);
1667}
1668
1669static int
1670symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1671{
1672 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1673 e->v.DictComp.generators,
1674 e->v.DictComp.key,
1675 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001676}