blob: e0e63daa098c1a15097cf10aab7d2c2336525602 [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;
41
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042 ste->ste_name = name;
43 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000045 ste->ste_symbols = NULL;
46 ste->ste_varnames = NULL;
47 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000048
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000049 ste->ste_symbols = PyDict_New();
50 if (ste->ste_symbols == NULL)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000051 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000052
53 ste->ste_varnames = PyList_New(0);
54 if (ste->ste_varnames == NULL)
55 goto fail;
56
57 ste->ste_children = PyList_New(0);
58 if (ste->ste_children == NULL)
59 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000060
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000061 ste->ste_type = block;
62 ste->ste_unoptimized = 0;
63 ste->ste_nested = 0;
64 ste->ste_free = 0;
65 ste->ste_varargs = 0;
66 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000067 ste->ste_opt_lineno = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000068 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000070 if (st->st_cur != NULL &&
71 (st->st_cur->ste_nested ||
72 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000073 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000074 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000075 ste->ste_generator = 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000076 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000077
Nick Coghlan650f0d02007-04-15 12:05:43 +000078 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000079 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082 fail:
83 Py_XDECREF(ste);
84 return NULL;
85}
86
87static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000089{
Walter Dörwald5d4ede12007-06-11 16:03:16 +000090 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
91 ste->ste_name,
Christian Heimes217cfd12007-12-02 14:31:20 +000092 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093}
94
95static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097{
98 ste->ste_table = NULL;
99 Py_XDECREF(ste->ste_id);
100 Py_XDECREF(ste->ste_name);
101 Py_XDECREF(ste->ste_symbols);
102 Py_XDECREF(ste->ste_varnames);
103 Py_XDECREF(ste->ste_children);
104 PyObject_Del(ste);
105}
106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108
Guido van Rossum6f799372001-09-20 20:46:19 +0000109static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000110 {"id", T_OBJECT, OFF(ste_id), READONLY},
111 {"name", T_OBJECT, OFF(ste_name), READONLY},
112 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
113 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
114 {"children", T_OBJECT, OFF(ste_children), READONLY},
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000115 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
116 {"nested", T_INT, OFF(ste_nested), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000117 {"type", T_INT, OFF(ste_type), READONLY},
118 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119 {NULL}
120};
121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122PyTypeObject PySTEntry_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000123 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126 0,
127 (destructor)ste_dealloc, /* tp_dealloc */
128 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000129 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000131 0, /* tp_reserved */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000132 (reprfunc)ste_repr, /* tp_repr */
133 0, /* tp_as_number */
134 0, /* tp_as_sequence */
135 0, /* tp_as_mapping */
136 0, /* tp_hash */
137 0, /* tp_call */
138 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000139 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000140 0, /* tp_setattro */
141 0, /* tp_as_buffer */
142 Py_TPFLAGS_DEFAULT, /* tp_flags */
143 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000144 0, /* tp_traverse */
145 0, /* tp_clear */
146 0, /* tp_richcompare */
147 0, /* tp_weaklistoffset */
148 0, /* tp_iter */
149 0, /* tp_iternext */
150 0, /* tp_methods */
151 ste_memberlist, /* tp_members */
152 0, /* tp_getset */
153 0, /* tp_base */
154 0, /* tp_dict */
155 0, /* tp_descr_get */
156 0, /* tp_descr_set */
157 0, /* tp_dictoffset */
158 0, /* tp_init */
159 0, /* tp_alloc */
160 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000161};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162
163static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000164static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000166 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_exit_block(struct symtable *st, void *ast);
168static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
169static int symtable_visit_expr(struct symtable *st, expr_ty s);
170static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000171static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
172static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000173static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
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);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000180static int symtable_visit_params(struct symtable *st, asdl_seq *args);
181static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000183static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184
185
Nick Coghlan650f0d02007-04-15 12:05:43 +0000186static identifier top = NULL, lambda = NULL, genexpr = NULL,
Benjamin Petersondcaf3292009-03-03 00:54:05 +0000187 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
188 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189
190#define GET_IDENTIFIER(VAR) \
Martin v. Löwis5b222132007-06-10 09:51:05 +0000191 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
193#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000194"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196static struct symtable *
197symtable_new(void)
198{
199 struct symtable *st;
200
201 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
202 if (st == NULL)
203 return NULL;
204
205 st->st_filename = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000206 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000207
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208 if ((st->st_stack = PyList_New(0)) == NULL)
209 goto fail;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000210 if ((st->st_blocks = PyDict_New()) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211 goto fail;
212 st->st_cur = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213 st->st_private = NULL;
214 return st;
215 fail:
216 PySymtable_Free(st);
217 return NULL;
218}
219
220struct symtable *
221PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
222{
223 struct symtable *st = symtable_new();
224 asdl_seq *seq;
225 int i;
226
227 if (st == NULL)
228 return st;
229 st->st_filename = filename;
230 st->st_future = future;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000231 /* Make the initial symbol information gathering pass */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000232 if (!GET_IDENTIFIER(top) ||
233 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000234 PySymtable_Free(st);
235 return NULL;
236 }
237
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 st->st_top = st->st_cur;
239 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 switch (mod->kind) {
241 case Module_kind:
242 seq = mod->v.Module.body;
243 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244 if (!symtable_visit_stmt(st,
245 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246 goto error;
247 break;
248 case Expression_kind:
249 if (!symtable_visit_expr(st, mod->v.Expression.body))
250 goto error;
251 break;
252 case Interactive_kind:
253 seq = mod->v.Interactive.body;
254 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255 if (!symtable_visit_stmt(st,
256 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257 goto error;
258 break;
259 case Suite_kind:
260 PyErr_SetString(PyExc_RuntimeError,
261 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000262 goto error;
263 }
264 if (!symtable_exit_block(st, (void *)mod)) {
265 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266 return NULL;
267 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000268 /* Make the second symbol analysis pass */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269 if (symtable_analyze(st))
270 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000271 PySymtable_Free(st);
272 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000274 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 PySymtable_Free(st);
276 return NULL;
277}
278
279void
280PySymtable_Free(struct symtable *st)
281{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000282 Py_XDECREF(st->st_blocks);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283 Py_XDECREF(st->st_stack);
284 PyMem_Free((void *)st);
285}
286
287PySTEntryObject *
288PySymtable_Lookup(struct symtable *st, void *key)
289{
290 PyObject *k, *v;
291
292 k = PyLong_FromVoidPtr(key);
293 if (k == NULL)
294 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000295 v = PyDict_GetItem(st->st_blocks, k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000296 if (v) {
297 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299 }
300 else {
301 PyErr_SetString(PyExc_KeyError,
302 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000304
305 Py_DECREF(k);
306 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307}
308
309int
310PyST_GetScope(PySTEntryObject *ste, PyObject *name)
311{
312 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
313 if (!v)
314 return 0;
Christian Heimes217cfd12007-12-02 14:31:20 +0000315 assert(PyLong_Check(v));
316 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317}
318
319
320/* Analyze raw symbol information to determine scope of each name.
321
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000322 The next several functions are helpers for symtable_analyze(),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323 which determines whether a name is local, global, or free. In addition,
324 it determines which local variables are cell variables; they provide
325 bindings that are used for free variables in enclosed blocks.
326
Nick Coghlan650f0d02007-04-15 12:05:43 +0000327 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328 explicit global is declared with the global statement. An implicit
329 global is a free variable for which the compiler has found no binding
330 in an enclosing function scope. The implicit global is either a global
331 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
332 to handle these names to implement slightly odd semantics. In such a
333 block, the name is treated as global until it is assigned to; then it
334 is treated as a local.
335
336 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000337 The first pass collects raw facts from the AST via the symtable_visit_*
338 functions: the name is a parameter here, the name is used but not defined
339 here, etc. The second pass analyzes these facts during a pass over the
340 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341
342 When a function is entered during the second pass, the parent passes
343 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000344 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000345 Names which are explicitly declared nonlocal must exist in this set of
346 visible names - if they do not, a syntax error is raised. After doing
347 the local analysis, it analyzes each of its child blocks using an
348 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
Nick Coghlan650f0d02007-04-15 12:05:43 +0000350 The children update the free variable set. If a local variable is added to
351 the free variable set by the child, the variable is marked as a cell. The
352 function object being defined must provide runtime storage for the variable
353 that may outlive the function's frame. Cell variables are removed from the
354 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000355
Nick Coghlan650f0d02007-04-15 12:05:43 +0000356 During analysis, the names are:
357 symbols: dict mapping from symbol names to flag values (including offset scope values)
358 scopes: dict mapping from symbol names to scope values (no offset)
359 local: set of all symbol names local to the current scope
360 bound: set of all symbol names local to a containing function scope
361 free: set of all symbol names referenced but not bound in child scopes
362 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363*/
364
365#define SET_SCOPE(DICT, NAME, I) { \
Christian Heimes217cfd12007-12-02 14:31:20 +0000366 PyObject *o = PyLong_FromLong(I); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367 if (!o) \
368 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000369 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
370 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000372 } \
373 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374}
375
376/* Decide on scope of name, given flags.
377
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000378 The namespace dictionaries may be modified to record information
379 about the new name. For example, a new global will add an entry to
380 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381*/
382
383static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000384analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385 PyObject *bound, PyObject *local, PyObject *free,
386 PyObject *global)
387{
388 if (flags & DEF_GLOBAL) {
389 if (flags & DEF_PARAM) {
390 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000391 "name '%U' is parameter and global",
392 name);
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000393 PyErr_SyntaxLocation(ste->ste_table->st_filename,
394 ste->ste_lineno);
395
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396 return 0;
397 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000398 if (flags & DEF_NONLOCAL) {
399 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000400 "name '%U' is nonlocal and global",
401 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000402 return 0;
403 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000404 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
405 if (PySet_Add(global, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000406 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000407 if (bound && (PySet_Discard(bound, name) < 0))
408 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409 return 1;
410 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000411 if (flags & DEF_NONLOCAL) {
412 if (flags & DEF_PARAM) {
413 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000414 "name '%U' is parameter and nonlocal",
415 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000416 return 0;
417 }
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000418 if (!bound) {
419 PyErr_Format(PyExc_SyntaxError,
420 "nonlocal declaration not allowed at module level");
421 return 0;
422 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000423 if (!PySet_Contains(bound, name)) {
Jeremy Hylton81e95022007-02-27 06:50:52 +0000424 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000425 "no binding for nonlocal '%U' found",
426 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000427
428 return 0;
429 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000430 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000431 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000432 return PySet_Add(free, name) >= 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000433 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434 if (flags & DEF_BOUND) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000435 SET_SCOPE(scopes, name, LOCAL);
436 if (PySet_Add(local, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000438 if (PySet_Discard(global, name) < 0)
439 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440 return 1;
441 }
442 /* If an enclosing block has a binding for this name, it
443 is a free variable rather than a global variable.
444 Note that having a non-NULL bound implies that the block
445 is nested.
446 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000447 if (bound && PySet_Contains(bound, name)) {
448 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000450 return PySet_Add(free, name) >= 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451 }
452 /* If a parent has a global statement, then call it global
453 explicit? It could also be global implicit.
454 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000455 if (global && PySet_Contains(global, name)) {
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000456 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457 return 1;
458 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000459 if (ste->ste_nested)
460 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000461 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000462 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463}
464
465#undef SET_SCOPE
466
467/* If a name is defined in free and also in locals, then this block
468 provides the binding for the free variable. The name should be
469 marked CELL in this block and removed from the free list.
470
471 Note that the current block's free variables are included in free.
472 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000473
Martin v. Löwis2673a572007-10-29 19:54:24 +0000474 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000475 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476*/
477
478static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000479analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000481 PyObject *name, *v, *v_cell;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482 int success = 0;
483 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484
Christian Heimes217cfd12007-12-02 14:31:20 +0000485 v_cell = PyLong_FromLong(CELL);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000486 if (!v_cell)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000488 while (PyDict_Next(scopes, &pos, &name, &v)) {
489 long scope;
Christian Heimes217cfd12007-12-02 14:31:20 +0000490 assert(PyLong_Check(v));
491 scope = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000492 if (scope != LOCAL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 continue;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000494 if (!PySet_Contains(free, name))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495 continue;
Martin v. Löwis2673a572007-10-29 19:54:24 +0000496 if (restricted != NULL &&
497 PyUnicode_CompareWithASCIIString(name, restricted))
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000498 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499 /* Replace LOCAL with CELL for this name, and remove
500 from free. It is safe to replace the value of name
501 in the dict, because it will not cause a resize.
502 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000503 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000505 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 goto error;
507 }
508 success = 1;
509 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000510 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511 return success;
512}
513
514/* Check for illegal statements in unoptimized namespaces */
515static int
516check_unoptimized(const PySTEntryObject* ste) {
Armin Rigo31441302005-10-21 12:57:31 +0000517 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000519 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520 || !(ste->ste_free || ste->ste_child_free))
521 return 1;
522
Armin Rigo31441302005-10-21 12:57:31 +0000523 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 "contains a nested function with free variables" :
525 "is a nested function");
526
527 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000528 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 return 1;
530 case OPT_IMPORT_STAR:
Walter Dörwald3ef72bb2007-06-11 16:12:10 +0000531 PyErr_Format(PyExc_SyntaxError,
532 "import * is not allowed in function '%U' because it %s",
Walter Dörwalde42109d2007-06-11 16:08:41 +0000533 ste->ste_name, trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535 }
536
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 PyErr_SyntaxLocation(ste->ste_table->st_filename,
538 ste->ste_opt_lineno);
539 return 0;
540}
541
Nick Coghlan650f0d02007-04-15 12:05:43 +0000542/* Enter the final scope information into the ste_symbols dict.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543 *
544 * All arguments are dicts. Modifies symbols, others are read-only.
545*/
546static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000547update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000550 PyObject *name = NULL, *itr = NULL;
551 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000552 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553
Nick Coghlan650f0d02007-04-15 12:05:43 +0000554 /* Update scope information for all symbols in this scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 while (PyDict_Next(symbols, &pos, &name, &v)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000556 long scope, flags;
Christian Heimes217cfd12007-12-02 14:31:20 +0000557 assert(PyLong_Check(v));
558 flags = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000559 v_scope = PyDict_GetItem(scopes, name);
Christian Heimes217cfd12007-12-02 14:31:20 +0000560 assert(v_scope && PyLong_Check(v_scope));
561 scope = PyLong_AS_LONG(v_scope);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000562 flags |= (scope << SCOPE_OFFSET);
Christian Heimes217cfd12007-12-02 14:31:20 +0000563 v_new = PyLong_FromLong(flags);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000564 if (!v_new)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000565 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000566 if (PyDict_SetItem(symbols, name, v_new) < 0) {
567 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 return 0;
569 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000570 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 }
572
Nick Coghlan650f0d02007-04-15 12:05:43 +0000573 /* Record not yet resolved free variables from children (if any) */
Christian Heimes217cfd12007-12-02 14:31:20 +0000574 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000575 if (!v_free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576 return 0;
577
Nick Coghlan650f0d02007-04-15 12:05:43 +0000578 itr = PyObject_GetIter(free);
579 if (!itr)
580 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581
Nick Coghlan650f0d02007-04-15 12:05:43 +0000582 while ((name = PyIter_Next(itr))) {
583 v = PyDict_GetItem(symbols, name);
584
585 /* Handle symbol that already exists in this scope */
586 if (v) {
587 /* Handle a free variable in a method of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588 the class that has the same name as a local
589 or global in the class scope.
590 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000591 if (classflag &&
Christian Heimes217cfd12007-12-02 14:31:20 +0000592 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
593 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
594 v_new = PyLong_FromLong(flags);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000595 if (!v_new) {
596 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000598 if (PyDict_SetItem(symbols, name, v_new) < 0) {
599 Py_DECREF(v_new);
600 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000602 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000604 /* It's a cell, or already free in this scope */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000605 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 continue;
607 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000608 /* Handle global symbol */
609 if (!PySet_Contains(bound, name)) {
610 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611 continue; /* it's a global */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000613 /* Propagate new free symbol up the lexical stack */
614 if (PyDict_SetItem(symbols, name, v_free) < 0) {
615 goto error;
616 }
617 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000619 Py_DECREF(itr);
620 Py_DECREF(v_free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000622error:
623 Py_XDECREF(v_free);
624 Py_XDECREF(itr);
625 Py_XDECREF(name);
626 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627}
628
629/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000630
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631 Arguments:
632 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000633 bound -- set of variables bound in enclosing scopes (input). bound
634 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635 free -- set of free variables in enclosed scopes (output)
636 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000637
638 The implementation uses two mutually recursive functions,
639 analyze_block() and analyze_child_block(). analyze_block() is
640 responsible for analyzing the individual names defined in a block.
641 analyze_child_block() prepares temporary namespace dictionaries
642 used to evaluated nested blocks.
643
644 The two functions exist because a child block should see the name
645 bindings of its enclosing blocks, but those bindings should not
646 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647*/
648
649static int
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000650analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
651 PyObject *global, PyObject* child_free);
652
653static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
655 PyObject *global)
656{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000657 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000658 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
Benjamin Peterson57512582009-05-12 20:39:25 +0000659 PyObject *temp;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000660 int i, success = 0;
661 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000663 local = PySet_New(NULL); /* collect new names bound in block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664 if (!local)
665 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000666 scopes = PyDict_New(); /* collect scopes defined for each name */
667 if (!scopes)
668 goto error;
669
670 /* Allocate new global and bound variable dictionaries. These
671 dictionaries hold the names visible in nested blocks. For
672 ClassBlocks, the bound and global names are initialized
673 before analyzing names, because class bindings aren't
674 visible in methods. For other blocks, they are initialized
675 after names are analyzed.
676 */
677
678 /* TODO(jhylton): Package these dicts in a struct so that we
679 can write reasonable helper functions?
680 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000681 newglobal = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 if (!newglobal)
683 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000684 newfree = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685 if (!newfree)
686 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000687 newbound = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 if (!newbound)
689 goto error;
690
Nick Coghlan650f0d02007-04-15 12:05:43 +0000691 /* Class namespace has no effect on names visible in
692 nested functions, so populate the global and bound
693 sets to be passed to child blocks before analyzing
694 this one.
695 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 if (ste->ste_type == ClassBlock) {
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000697 /* Pass down known globals */
Benjamin Peterson57512582009-05-12 20:39:25 +0000698 temp = PyNumber_InPlaceOr(newglobal, global);
699 if (!temp)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000700 goto error;
Benjamin Peterson57512582009-05-12 20:39:25 +0000701 Py_DECREF(temp);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000702 /* Pass down previously bound symbols */
703 if (bound) {
Benjamin Peterson57512582009-05-12 20:39:25 +0000704 temp = PyNumber_InPlaceOr(newbound, bound);
705 if (!temp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706 goto error;
Benjamin Peterson57512582009-05-12 20:39:25 +0000707 Py_DECREF(temp);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000708 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709 }
710
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000712 long flags = PyLong_AS_LONG(v);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000713 if (!analyze_name(ste, scopes, name, flags,
714 bound, local, free, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715 goto error;
716 }
717
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000718 /* Populate global and bound sets to be passed to children. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000720 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson57512582009-05-12 20:39:25 +0000722 temp = PyNumber_InPlaceOr(newbound, local);
723 if (!temp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 goto error;
Benjamin Peterson57512582009-05-12 20:39:25 +0000725 Py_DECREF(temp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000727 /* Pass down previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728 if (bound) {
Benjamin Peterson57512582009-05-12 20:39:25 +0000729 temp = PyNumber_InPlaceOr(newbound, bound);
730 if (!temp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731 goto error;
Benjamin Peterson57512582009-05-12 20:39:25 +0000732 Py_DECREF(temp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000734 /* Pass down known globals */
Benjamin Peterson57512582009-05-12 20:39:25 +0000735 temp = PyNumber_InPlaceOr(newglobal, global);
736 if (!temp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 goto error;
Benjamin Peterson57512582009-05-12 20:39:25 +0000738 Py_DECREF(temp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000740 else {
741 /* Special-case __class__ */
742 if (!GET_IDENTIFIER(__class__))
743 goto error;
744 assert(PySet_Contains(local, __class__) == 1);
745 if (PySet_Add(newbound, __class__) < 0)
746 goto error;
747 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000749 /* Recursively call analyze_block() on each child block.
750
751 newbound, newglobal now contain the names visible in
752 nested blocks. The free variables in the children will
753 be collected in allfree.
754 */
755 allfree = PySet_New(NULL);
756 if (!allfree)
757 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
759 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000760 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000762 entry = (PySTEntryObject*)c;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000763 if (!analyze_child_block(entry, newbound, newfree, newglobal,
764 allfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000766 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 if (entry->ste_free || entry->ste_child_free)
768 ste->ste_child_free = 1;
769 }
770
Benjamin Peterson57512582009-05-12 20:39:25 +0000771 temp = PyNumber_InPlaceOr(newfree, allfree);
772 if (!temp)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000773 goto error;
Benjamin Peterson57512582009-05-12 20:39:25 +0000774 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000775
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000776 /* Check if any local variables must be converted to cell variables */
777 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
778 NULL))
779 goto error;
780 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
781 "__class__"))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000783 /* Records the results of the analysis in the symbol table entry */
784 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 ste->ste_type == ClassBlock))
786 goto error;
787 if (!check_unoptimized(ste))
788 goto error;
789
Benjamin Peterson57512582009-05-12 20:39:25 +0000790 temp = PyNumber_InPlaceOr(free, newfree);
791 if (!temp)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 goto error;
Benjamin Peterson57512582009-05-12 20:39:25 +0000793 Py_DECREF(temp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 success = 1;
795 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000796 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 Py_XDECREF(newbound);
799 Py_XDECREF(newglobal);
800 Py_XDECREF(newfree);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000801 Py_XDECREF(allfree);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 if (!success)
803 assert(PyErr_Occurred());
804 return success;
805}
806
807static int
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000808analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
809 PyObject *global, PyObject* child_free)
810{
811 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
Benjamin Peterson57512582009-05-12 20:39:25 +0000812 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000813
814 /* Copy the bound and global dictionaries.
815
816 These dictionary are used by all blocks enclosed by the
817 current block. The analyze_block() call modifies these
818 dictionaries.
819
820 */
821 temp_bound = PySet_New(bound);
822 if (!temp_bound)
823 goto error;
824 temp_free = PySet_New(free);
825 if (!temp_free)
826 goto error;
827 temp_global = PySet_New(global);
828 if (!temp_global)
829 goto error;
830
831 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
832 goto error;
Benjamin Peterson57512582009-05-12 20:39:25 +0000833 temp = PyNumber_InPlaceOr(child_free, temp_free);
834 if (!temp)
Benjamin Petersond1e54932009-04-02 02:27:20 +0000835 goto error;
Benjamin Peterson57512582009-05-12 20:39:25 +0000836 Py_DECREF(temp);
Benjamin Petersond1e54932009-04-02 02:27:20 +0000837 Py_DECREF(temp_bound);
838 Py_DECREF(temp_free);
839 Py_DECREF(temp_global);
840 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000841 error:
842 Py_XDECREF(temp_bound);
843 Py_XDECREF(temp_free);
844 Py_XDECREF(temp_global);
Benjamin Petersond1e54932009-04-02 02:27:20 +0000845 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000846}
847
848static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849symtable_analyze(struct symtable *st)
850{
851 PyObject *free, *global;
852 int r;
853
Nick Coghlan650f0d02007-04-15 12:05:43 +0000854 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 if (!free)
856 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000857 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000859 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 return 0;
861 }
862 r = analyze_block(st->st_top, NULL, free, global);
863 Py_DECREF(free);
864 Py_DECREF(global);
865 return r;
866}
867
868
869static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000870symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871{
872 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000873 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
875 PyErr_SetString(PyExc_SyntaxError, msg);
876 PyErr_SyntaxLocation(st->st_filename,
877 st->st_cur->ste_lineno);
878 }
879 return 0;
880 }
881 return 1;
882}
883
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000884/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885 This reference is released when the block is exited, via the DECREF
886 in symtable_exit_block().
887*/
888
889static int
890symtable_exit_block(struct symtable *st, void *ast)
891{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000892 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000894 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895 end = PyList_GET_SIZE(st->st_stack) - 1;
896 if (end >= 0) {
897 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
898 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000899 if (st->st_cur == NULL)
900 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901 Py_INCREF(st->st_cur);
902 if (PySequence_DelItem(st->st_stack, end) < 0)
903 return 0;
904 }
905 return 1;
906}
907
908static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000909symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 void *ast, int lineno)
911{
912 PySTEntryObject *prev = NULL;
913
914 if (st->st_cur) {
915 prev = st->st_cur;
916 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917 return 0;
918 }
919 Py_DECREF(st->st_cur);
920 }
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000921 st->st_cur = ste_new(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000922 if (st->st_cur == NULL)
923 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 if (name == GET_IDENTIFIER(top))
925 st->st_global = st->st_cur->ste_symbols;
926 if (prev) {
927 if (PyList_Append(prev->ste_children,
928 (PyObject *)st->st_cur) < 0) {
929 return 0;
930 }
931 }
932 return 1;
933}
934
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000935static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936symtable_lookup(struct symtable *st, PyObject *name)
937{
938 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000939 PyObject *mangled = _Py_Mangle(st->st_private, name);
940 if (!mangled)
941 return 0;
942 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
943 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944 if (!o)
945 return 0;
Christian Heimes217cfd12007-12-02 14:31:20 +0000946 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947}
948
949static int
950symtable_add_def(struct symtable *st, PyObject *name, int flag)
951{
952 PyObject *o;
953 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000954 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000955 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956
Jeremy Hylton81e95022007-02-27 06:50:52 +0000957
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000958 if (!mangled)
959 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000961 if ((o = PyDict_GetItem(dict, mangled))) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000962 val = PyLong_AS_LONG(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000964 /* Is it better to use 'mangled' or 'name' here? */
Neal Norwitza5d16a32007-08-24 22:53:58 +0000965 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 PyErr_SyntaxLocation(st->st_filename,
967 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000968 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969 }
970 val |= flag;
971 } else
972 val = flag;
Christian Heimes217cfd12007-12-02 14:31:20 +0000973 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000975 goto error;
976 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000978 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 }
980 Py_DECREF(o);
981
982 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000983 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
984 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 } else if (flag & DEF_GLOBAL) {
986 /* XXX need to update DEF_GLOBAL for other flags too;
987 perhaps only DEF_FREE_GLOBAL */
988 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000989 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000990 val |= PyLong_AS_LONG(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000992 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000994 goto error;
995 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000997 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 }
999 Py_DECREF(o);
1000 }
Neal Norwitz4737b232005-11-19 23:58:29 +00001001 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001003
1004error:
1005 Py_DECREF(mangled);
1006 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007}
1008
1009/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1010 They use the ASDL name to synthesize the name of the C type and the visit
1011 function.
1012
1013 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1014 useful if the first node in the sequence requires special treatment.
1015*/
1016
1017#define VISIT(ST, TYPE, V) \
1018 if (!symtable_visit_ ## TYPE((ST), (V))) \
1019 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001020
1021#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
1022 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1023 symtable_exit_block((ST), (S)); \
1024 return 0; \
1025 }
1026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027#define VISIT_SEQ(ST, TYPE, SEQ) { \
1028 int i; \
1029 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1030 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001031 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 if (!symtable_visit_ ## TYPE((ST), elt)) \
1033 return 0; \
1034 } \
1035}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001036
1037#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
1038 int i; \
1039 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1040 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001041 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001042 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1043 symtable_exit_block((ST), (S)); \
1044 return 0; \
1045 } \
1046 } \
1047}
1048
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
1050 int i; \
1051 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1052 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001053 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054 if (!symtable_visit_ ## TYPE((ST), elt)) \
1055 return 0; \
1056 } \
1057}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001058
1059#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
1060 int i; \
1061 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1062 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001063 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001064 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1065 symtable_exit_block((ST), (S)); \
1066 return 0; \
1067 } \
1068 } \
1069}
1070
Guido van Rossum4f72a782006-10-27 23:31:49 +00001071#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
1072 int i = 0; \
1073 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1074 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1075 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1076 if (!elt) continue; /* can be NULL */ \
1077 if (!symtable_visit_expr((ST), elt)) \
1078 return 0; \
1079 } \
1080}
1081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001083symtable_new_tmpname(struct symtable *st)
1084{
1085 char tmpname[256];
1086 identifier tmp;
1087
1088 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1089 ++st->st_cur->ste_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001090 tmp = PyUnicode_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001091 if (!tmp)
1092 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001093 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1094 return 0;
1095 Py_DECREF(tmp);
1096 return 1;
1097}
1098
Guido van Rossum4f72a782006-10-27 23:31:49 +00001099
Guido van Rossumc2e20742006-02-27 22:32:47 +00001100static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101symtable_visit_stmt(struct symtable *st, stmt_ty s)
1102{
1103 switch (s->kind) {
1104 case FunctionDef_kind:
1105 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1106 return 0;
1107 if (s->v.FunctionDef.args->defaults)
1108 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001109 if (s->v.FunctionDef.args->kw_defaults)
1110 VISIT_KWONLYDEFAULTS(st,
1111 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001112 if (!symtable_visit_annotations(st, s))
1113 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001114 if (s->v.FunctionDef.decorator_list)
1115 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1117 FunctionBlock, (void *)s, s->lineno))
1118 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001119 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1120 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 if (!symtable_exit_block(st, s))
1122 return 0;
1123 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001124 case ClassDef_kind: {
1125 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1127 return 0;
1128 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001129 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1130 if (s->v.ClassDef.starargs)
1131 VISIT(st, expr, s->v.ClassDef.starargs);
1132 if (s->v.ClassDef.kwargs)
1133 VISIT(st, expr, s->v.ClassDef.kwargs);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001134 if (s->v.ClassDef.decorator_list)
1135 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1137 (void *)s, s->lineno))
1138 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001139 if (!GET_IDENTIFIER(__class__) ||
Benjamin Petersondcaf3292009-03-03 00:54:05 +00001140 !symtable_add_def(st, __class__, DEF_LOCAL) ||
1141 !GET_IDENTIFIER(__locals__) ||
1142 !symtable_add_def(st, __locals__, DEF_PARAM)) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001143 symtable_exit_block(st, s);
1144 return 0;
1145 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001146 tmp = st->st_private;
1147 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001148 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001149 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 if (!symtable_exit_block(st, s))
1151 return 0;
1152 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001153 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001155 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001157 st->st_cur->ste_returns_value = 1;
1158 if (st->st_cur->ste_generator) {
1159 PyErr_SetString(PyExc_SyntaxError,
1160 RETURN_VAL_IN_GENERATOR);
1161 PyErr_SyntaxLocation(st->st_filename,
1162 s->lineno);
1163 return 0;
1164 }
1165 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 break;
1167 case Delete_kind:
1168 VISIT_SEQ(st, expr, s->v.Delete.targets);
1169 break;
1170 case Assign_kind:
1171 VISIT_SEQ(st, expr, s->v.Assign.targets);
1172 VISIT(st, expr, s->v.Assign.value);
1173 break;
1174 case AugAssign_kind:
1175 VISIT(st, expr, s->v.AugAssign.target);
1176 VISIT(st, expr, s->v.AugAssign.value);
1177 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 case For_kind:
1179 VISIT(st, expr, s->v.For.target);
1180 VISIT(st, expr, s->v.For.iter);
1181 VISIT_SEQ(st, stmt, s->v.For.body);
1182 if (s->v.For.orelse)
1183 VISIT_SEQ(st, stmt, s->v.For.orelse);
1184 break;
1185 case While_kind:
1186 VISIT(st, expr, s->v.While.test);
1187 VISIT_SEQ(st, stmt, s->v.While.body);
1188 if (s->v.While.orelse)
1189 VISIT_SEQ(st, stmt, s->v.While.orelse);
1190 break;
1191 case If_kind:
1192 /* XXX if 0: and lookup_yield() hacks */
1193 VISIT(st, expr, s->v.If.test);
1194 VISIT_SEQ(st, stmt, s->v.If.body);
1195 if (s->v.If.orelse)
1196 VISIT_SEQ(st, stmt, s->v.If.orelse);
1197 break;
1198 case Raise_kind:
Collin Winter828f04a2007-08-31 00:04:24 +00001199 if (s->v.Raise.exc) {
1200 VISIT(st, expr, s->v.Raise.exc);
1201 if (s->v.Raise.cause) {
1202 VISIT(st, expr, s->v.Raise.cause);
1203 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 }
1205 break;
1206 case TryExcept_kind:
1207 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1208 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1209 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1210 break;
1211 case TryFinally_kind:
1212 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1213 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1214 break;
1215 case Assert_kind:
1216 VISIT(st, expr, s->v.Assert.test);
1217 if (s->v.Assert.msg)
1218 VISIT(st, expr, s->v.Assert.msg);
1219 break;
1220 case Import_kind:
1221 VISIT_SEQ(st, alias, s->v.Import.names);
1222 /* XXX Don't have the lineno available inside
1223 visit_alias */
1224 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1225 st->st_cur->ste_opt_lineno = s->lineno;
1226 break;
1227 case ImportFrom_kind:
1228 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1229 /* XXX Don't have the lineno available inside
1230 visit_alias */
1231 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1232 st->st_cur->ste_opt_lineno = s->lineno;
1233 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 case Global_kind: {
1235 int i;
1236 asdl_seq *seq = s->v.Global.names;
1237 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001238 identifier name = (identifier)asdl_seq_GET(seq, i);
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001239 char *c_name = _PyUnicode_AsString(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001240 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 if (cur < 0)
1242 return 0;
1243 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001244 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 if (cur & DEF_LOCAL)
1246 PyOS_snprintf(buf, sizeof(buf),
1247 GLOBAL_AFTER_ASSIGN,
1248 c_name);
1249 else
1250 PyOS_snprintf(buf, sizeof(buf),
1251 GLOBAL_AFTER_USE,
1252 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001253 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 return 0;
1255 }
1256 if (!symtable_add_def(st, name, DEF_GLOBAL))
1257 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 break;
1260 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001261 case Nonlocal_kind: {
1262 int i;
1263 asdl_seq *seq = s->v.Nonlocal.names;
1264 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1265 identifier name = (identifier)asdl_seq_GET(seq, i);
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001266 char *c_name = _PyUnicode_AsString(name);
Jeremy Hylton81e95022007-02-27 06:50:52 +00001267 long cur = symtable_lookup(st, name);
1268 if (cur < 0)
1269 return 0;
1270 if (cur & (DEF_LOCAL | USE)) {
1271 char buf[256];
1272 if (cur & DEF_LOCAL)
1273 PyOS_snprintf(buf, sizeof(buf),
1274 NONLOCAL_AFTER_ASSIGN,
1275 c_name);
1276 else
1277 PyOS_snprintf(buf, sizeof(buf),
1278 NONLOCAL_AFTER_USE,
1279 c_name);
1280 if (!symtable_warn(st, buf, s->lineno))
1281 return 0;
1282 }
1283 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1284 return 0;
1285 }
1286 break;
1287 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288 case Expr_kind:
1289 VISIT(st, expr, s->v.Expr.value);
1290 break;
1291 case Pass_kind:
1292 case Break_kind:
1293 case Continue_kind:
1294 /* nothing to do here */
1295 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001296 case With_kind:
Guido van Rossumc2e20742006-02-27 22:32:47 +00001297 VISIT(st, expr, s->v.With.context_expr);
1298 if (s->v.With.optional_vars) {
Guido van Rossumc2e20742006-02-27 22:32:47 +00001299 VISIT(st, expr, s->v.With.optional_vars);
1300 }
1301 VISIT_SEQ(st, stmt, s->v.With.body);
1302 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 }
1304 return 1;
1305}
1306
1307static int
1308symtable_visit_expr(struct symtable *st, expr_ty e)
1309{
1310 switch (e->kind) {
1311 case BoolOp_kind:
1312 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1313 break;
1314 case BinOp_kind:
1315 VISIT(st, expr, e->v.BinOp.left);
1316 VISIT(st, expr, e->v.BinOp.right);
1317 break;
1318 case UnaryOp_kind:
1319 VISIT(st, expr, e->v.UnaryOp.operand);
1320 break;
1321 case Lambda_kind: {
Benjamin Peterson78565b22009-06-28 19:19:51 +00001322 if (!GET_IDENTIFIER(lambda))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 return 0;
1324 if (e->v.Lambda.args->defaults)
1325 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1326 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001327 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001328 FunctionBlock, (void *)e, 0))
1329 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001330 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1331 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 if (!symtable_exit_block(st, (void *)e))
1333 return 0;
1334 break;
1335 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001336 case IfExp_kind:
1337 VISIT(st, expr, e->v.IfExp.test);
1338 VISIT(st, expr, e->v.IfExp.body);
1339 VISIT(st, expr, e->v.IfExp.orelse);
1340 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 case Dict_kind:
1342 VISIT_SEQ(st, expr, e->v.Dict.keys);
1343 VISIT_SEQ(st, expr, e->v.Dict.values);
1344 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001345 case Set_kind:
1346 VISIT_SEQ(st, expr, e->v.Set.elts);
1347 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001348 case GeneratorExp_kind:
1349 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001352 case ListComp_kind:
1353 if (!symtable_visit_listcomp(st, e))
1354 return 0;
1355 break;
1356 case SetComp_kind:
1357 if (!symtable_visit_setcomp(st, e))
1358 return 0;
1359 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001360 case DictComp_kind:
1361 if (!symtable_visit_dictcomp(st, e))
1362 return 0;
1363 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 case Yield_kind:
1365 if (e->v.Yield.value)
1366 VISIT(st, expr, e->v.Yield.value);
1367 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001368 if (st->st_cur->ste_returns_value) {
1369 PyErr_SetString(PyExc_SyntaxError,
1370 RETURN_VAL_IN_GENERATOR);
1371 PyErr_SyntaxLocation(st->st_filename,
1372 e->lineno);
1373 return 0;
1374 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 break;
1376 case Compare_kind:
1377 VISIT(st, expr, e->v.Compare.left);
1378 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1379 break;
1380 case Call_kind:
1381 VISIT(st, expr, e->v.Call.func);
1382 VISIT_SEQ(st, expr, e->v.Call.args);
1383 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1384 if (e->v.Call.starargs)
1385 VISIT(st, expr, e->v.Call.starargs);
1386 if (e->v.Call.kwargs)
1387 VISIT(st, expr, e->v.Call.kwargs);
1388 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389 case Num_kind:
1390 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001391 case Bytes_kind:
1392 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 /* Nothing to do here. */
1394 break;
1395 /* The following exprs can be assignment targets. */
1396 case Attribute_kind:
1397 VISIT(st, expr, e->v.Attribute.value);
1398 break;
1399 case Subscript_kind:
1400 VISIT(st, expr, e->v.Subscript.value);
1401 VISIT(st, slice, e->v.Subscript.slice);
1402 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001403 case Starred_kind:
1404 VISIT(st, expr, e->v.Starred.value);
1405 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 case Name_kind:
1407 if (!symtable_add_def(st, e->v.Name.id,
1408 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1409 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001410 /* Special-case super: it counts as a use of __class__ */
1411 if (e->v.Name.ctx == Load &&
1412 st->st_cur->ste_type == FunctionBlock &&
1413 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1414 if (!GET_IDENTIFIER(__class__) ||
1415 !symtable_add_def(st, __class__, USE))
1416 return 0;
1417 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 break;
1419 /* child nodes of List and Tuple will have expr_context set */
1420 case List_kind:
1421 VISIT_SEQ(st, expr, e->v.List.elts);
1422 break;
1423 case Tuple_kind:
1424 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1425 break;
1426 }
1427 return 1;
1428}
1429
1430static int
1431symtable_implicit_arg(struct symtable *st, int pos)
1432{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001433 PyObject *id = PyUnicode_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 if (id == NULL)
1435 return 0;
1436 if (!symtable_add_def(st, id, DEF_PARAM)) {
1437 Py_DECREF(id);
1438 return 0;
1439 }
1440 Py_DECREF(id);
1441 return 1;
1442}
1443
1444static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001445symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001447 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001448
1449 if (!args)
1450 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001453 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001454 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 return 0;
1456 }
1457
1458 return 1;
1459}
1460
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001461static int
1462symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463{
1464 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001465
1466 if (!args)
1467 return -1;
1468
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001470 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001471 if (arg->annotation)
1472 VISIT(st, expr, arg->annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001474
1475 return 1;
1476}
1477
Neal Norwitzc1505362006-12-28 06:47:50 +00001478static int
1479symtable_visit_annotations(struct symtable *st, stmt_ty s)
1480{
1481 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001483 if (a->args && !symtable_visit_argannotations(st, a->args))
Neal Norwitzc1505362006-12-28 06:47:50 +00001484 return 0;
1485 if (a->varargannotation)
1486 VISIT(st, expr, a->varargannotation);
1487 if (a->kwargannotation)
1488 VISIT(st, expr, a->kwargannotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001489 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
Neal Norwitzc1505362006-12-28 06:47:50 +00001490 return 0;
1491 if (s->v.FunctionDef.returns)
1492 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 return 1;
1494}
1495
1496static int
1497symtable_visit_arguments(struct symtable *st, arguments_ty a)
1498{
1499 /* skip default arguments inside function block
1500 XXX should ast be different?
1501 */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001502 if (a->args && !symtable_visit_params(st, a->args))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001504 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001505 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 if (a->vararg) {
1507 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1508 return 0;
1509 st->st_cur->ste_varargs = 1;
1510 }
1511 if (a->kwarg) {
1512 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1513 return 0;
1514 st->st_cur->ste_varkeywords = 1;
1515 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 return 1;
1517}
1518
1519
1520static int
1521symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1522{
Neal Norwitzad74aa82008-03-31 05:14:30 +00001523 if (eh->v.ExceptHandler.type)
1524 VISIT(st, expr, eh->v.ExceptHandler.type);
1525 if (eh->v.ExceptHandler.name)
1526 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
Guido van Rossum16be03e2007-01-10 18:51:35 +00001527 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00001528 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 return 1;
1530}
1531
1532
1533static int
1534symtable_visit_alias(struct symtable *st, alias_ty a)
1535{
1536 /* Compute store_name, the name actually bound by the import
1537 operation. It is diferent than a->name when a->name is a
1538 dotted package name (e.g. spam.eggs)
1539 */
1540 PyObject *store_name;
1541 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001542 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1543 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001544 if (dot) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001545 store_name = PyUnicode_FromUnicode(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001546 if (!store_name)
1547 return 0;
1548 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 else {
1550 store_name = name;
1551 Py_INCREF(store_name);
1552 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001553 if (PyUnicode_CompareWithASCIIString(name, "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1555 Py_DECREF(store_name);
1556 return r;
1557 }
1558 else {
1559 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001560 int lineno = st->st_cur->ste_lineno;
Guido van Rossum33d26892007-08-05 15:29:28 +00001561 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1562 PyErr_SyntaxLocation(st->st_filename, lineno);
1563 Py_DECREF(store_name);
1564 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565 }
1566 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001567 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568 return 1;
1569 }
1570}
1571
1572
1573static int
1574symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1575{
1576 VISIT(st, expr, lc->target);
1577 VISIT(st, expr, lc->iter);
1578 VISIT_SEQ(st, expr, lc->ifs);
1579 return 1;
1580}
1581
1582
1583static int
1584symtable_visit_keyword(struct symtable *st, keyword_ty k)
1585{
1586 VISIT(st, expr, k->value);
1587 return 1;
1588}
1589
1590
1591static int
1592symtable_visit_slice(struct symtable *st, slice_ty s)
1593{
1594 switch (s->kind) {
1595 case Slice_kind:
1596 if (s->v.Slice.lower)
1597 VISIT(st, expr, s->v.Slice.lower)
1598 if (s->v.Slice.upper)
1599 VISIT(st, expr, s->v.Slice.upper)
1600 if (s->v.Slice.step)
1601 VISIT(st, expr, s->v.Slice.step)
1602 break;
1603 case ExtSlice_kind:
1604 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1605 break;
1606 case Index_kind:
1607 VISIT(st, expr, s->v.Index.value)
1608 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 }
1610 return 1;
1611}
1612
1613static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001614symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001615 identifier scope_name, asdl_seq *generators,
1616 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001618 int is_generator = (e->kind == GeneratorExp_kind);
1619 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001621 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622 /* Outermost iterator is evaluated in current scope */
1623 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001624 /* Create comprehension scope for the rest */
1625 if (!scope_name ||
1626 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627 return 0;
1628 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001629 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630 /* Outermost iter is received as an argument */
1631 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001632 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633 return 0;
1634 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001635 /* Allocate temporary name if needed */
1636 if (needs_tmp && !symtable_new_tmpname(st)) {
1637 symtable_exit_block(st, (void *)e);
1638 return 0;
1639 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001640 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1641 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1642 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001643 generators, 1, (void*)e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001644 if (value)
1645 VISIT_IN_BLOCK(st, expr, value, (void*)e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001646 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001647 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001649
1650static int
1651symtable_visit_genexp(struct symtable *st, expr_ty e)
1652{
1653 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1654 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001655 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001656}
1657
1658static int
1659symtable_visit_listcomp(struct symtable *st, expr_ty e)
1660{
1661 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1662 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001663 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001664}
1665
1666static int
1667symtable_visit_setcomp(struct symtable *st, expr_ty e)
1668{
1669 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1670 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001671 e->v.SetComp.elt, NULL);
1672}
1673
1674static int
1675symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1676{
1677 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1678 e->v.DictComp.generators,
1679 e->v.DictComp.key,
1680 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001681}