blob: 68deb0a41e1f10e61afb6f4d8e37ae7657dc0f8d [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
Neal Norwitz090b3dd2006-02-28 22:36:46 +000025/* XXX(nnorwitz): change name since static? */
26static PySTEntryObject *
Neal Norwitz62c2fac2005-10-24 00:30:44 +000027PySTEntry_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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036 ste = (PySTEntryObject *)PyObject_New(PySTEntryObject,
37 &PySTEntry_Type);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000038 ste->ste_table = st;
39 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000041
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 Hylton4d508ad2003-05-21 17:34:50 +000068 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000071 if (st->st_cur != NULL &&
72 (st->st_cur->ste_nested ||
73 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000074 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000075 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000076 ste->ste_generator = 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000077 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000078
Nick Coghlan650f0d02007-04-15 12:05:43 +000079 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000080 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083 fail:
84 Py_XDECREF(ste);
85 return NULL;
86}
87
88static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000090{
91 char buf[256];
92
Barry Warsaw4b4ab202001-11-28 21:36:28 +000093 PyOS_snprintf(buf, sizeof(buf),
94 "<symtable entry %.100s(%ld), line %d>",
95 PyString_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097 return PyString_FromString(buf);
98}
99
100static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000101ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000102{
103 ste->ste_table = NULL;
104 Py_XDECREF(ste->ste_id);
105 Py_XDECREF(ste->ste_name);
106 Py_XDECREF(ste->ste_symbols);
107 Py_XDECREF(ste->ste_varnames);
108 Py_XDECREF(ste->ste_children);
109 PyObject_Del(ste);
110}
111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000113
Guido van Rossum6f799372001-09-20 20:46:19 +0000114static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000115 {"id", T_OBJECT, OFF(ste_id), READONLY},
116 {"name", T_OBJECT, OFF(ste_name), READONLY},
117 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
118 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
119 {"children", T_OBJECT, OFF(ste_children), READONLY},
120 {"type", T_INT, OFF(ste_type), READONLY},
121 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000122 {NULL}
123};
124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125PyTypeObject PySTEntry_Type = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126 PyObject_HEAD_INIT(&PyType_Type)
127 0,
128 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000129 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130 0,
131 (destructor)ste_dealloc, /* tp_dealloc */
132 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000133 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000134 0, /* tp_setattr */
135 0, /* tp_compare */
136 (reprfunc)ste_repr, /* tp_repr */
137 0, /* tp_as_number */
138 0, /* tp_as_sequence */
139 0, /* tp_as_mapping */
140 0, /* tp_hash */
141 0, /* tp_call */
142 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000143 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000144 0, /* tp_setattro */
145 0, /* tp_as_buffer */
146 Py_TPFLAGS_DEFAULT, /* tp_flags */
147 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000148 0, /* tp_traverse */
149 0, /* tp_clear */
150 0, /* tp_richcompare */
151 0, /* tp_weaklistoffset */
152 0, /* tp_iter */
153 0, /* tp_iternext */
154 0, /* tp_methods */
155 ste_memberlist, /* tp_members */
156 0, /* tp_getset */
157 0, /* tp_base */
158 0, /* tp_dict */
159 0, /* tp_descr_get */
160 0, /* tp_descr_set */
161 0, /* tp_dictoffset */
162 0, /* tp_init */
163 0, /* tp_alloc */
164 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000165};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166
167static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000168static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000170 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171static int symtable_exit_block(struct symtable *st, void *ast);
172static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
173static int symtable_visit_expr(struct symtable *st, expr_ty s);
174static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000175static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
176static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177static int symtable_visit_arguments(struct symtable *st, arguments_ty);
178static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
179static int symtable_visit_alias(struct symtable *st, alias_ty);
180static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
181static int symtable_visit_keyword(struct symtable *st, keyword_ty);
182static int symtable_visit_slice(struct symtable *st, slice_ty);
Neal Norwitzc1505362006-12-28 06:47:50 +0000183static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top,
184 int annotations);
185static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args,
186 int annotations);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000188static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189
190
Nick Coghlan650f0d02007-04-15 12:05:43 +0000191static identifier top = NULL, lambda = NULL, genexpr = NULL,
192 listcomp = NULL, setcomp = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
194#define GET_IDENTIFIER(VAR) \
195 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
196
197#define DUPLICATE_ARGUMENT \
198"duplicate argument '%s' in function definition"
199
200static struct symtable *
201symtable_new(void)
202{
203 struct symtable *st;
204
205 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
206 if (st == NULL)
207 return NULL;
208
209 st->st_filename = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000210 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000211
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212 if ((st->st_stack = PyList_New(0)) == NULL)
213 goto fail;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000214 if ((st->st_blocks = PyDict_New()) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215 goto fail;
216 st->st_cur = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217 st->st_private = NULL;
218 return st;
219 fail:
220 PySymtable_Free(st);
221 return NULL;
222}
223
224struct symtable *
225PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
226{
227 struct symtable *st = symtable_new();
228 asdl_seq *seq;
229 int i;
230
231 if (st == NULL)
232 return st;
233 st->st_filename = filename;
234 st->st_future = future;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000235 /* Make the initial symbol information gathering pass */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000236 if (!GET_IDENTIFIER(top) ||
237 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238 PySymtable_Free(st);
239 return NULL;
240 }
241
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242 st->st_top = st->st_cur;
243 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 switch (mod->kind) {
245 case Module_kind:
246 seq = mod->v.Module.body;
247 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248 if (!symtable_visit_stmt(st,
249 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250 goto error;
251 break;
252 case Expression_kind:
253 if (!symtable_visit_expr(st, mod->v.Expression.body))
254 goto error;
255 break;
256 case Interactive_kind:
257 seq = mod->v.Interactive.body;
258 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259 if (!symtable_visit_stmt(st,
260 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 goto error;
262 break;
263 case Suite_kind:
264 PyErr_SetString(PyExc_RuntimeError,
265 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000266 goto error;
267 }
268 if (!symtable_exit_block(st, (void *)mod)) {
269 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 return NULL;
271 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000272 /* Make the second symbol analysis pass */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273 if (symtable_analyze(st))
274 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000275 PySymtable_Free(st);
276 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000278 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 PySymtable_Free(st);
280 return NULL;
281}
282
283void
284PySymtable_Free(struct symtable *st)
285{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000286 Py_XDECREF(st->st_blocks);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287 Py_XDECREF(st->st_stack);
288 PyMem_Free((void *)st);
289}
290
291PySTEntryObject *
292PySymtable_Lookup(struct symtable *st, void *key)
293{
294 PyObject *k, *v;
295
296 k = PyLong_FromVoidPtr(key);
297 if (k == NULL)
298 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000299 v = PyDict_GetItem(st->st_blocks, k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300 if (v) {
301 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303 }
304 else {
305 PyErr_SetString(PyExc_KeyError,
306 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000308
309 Py_DECREF(k);
310 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311}
312
313int
314PyST_GetScope(PySTEntryObject *ste, PyObject *name)
315{
316 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
317 if (!v)
318 return 0;
319 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000320 return (PyInt_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321}
322
323
324/* Analyze raw symbol information to determine scope of each name.
325
326 The next several functions are helpers for PySymtable_Analyze(),
327 which determines whether a name is local, global, or free. In addition,
328 it determines which local variables are cell variables; they provide
329 bindings that are used for free variables in enclosed blocks.
330
Nick Coghlan650f0d02007-04-15 12:05:43 +0000331 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332 explicit global is declared with the global statement. An implicit
333 global is a free variable for which the compiler has found no binding
334 in an enclosing function scope. The implicit global is either a global
335 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
336 to handle these names to implement slightly odd semantics. In such a
337 block, the name is treated as global until it is assigned to; then it
338 is treated as a local.
339
Jeremy Hylton81e95022007-02-27 06:50:52 +0000340 TODO(jhylton): Discuss nonlocal
341
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000343 The first pass collects raw facts from the AST via the symtable_visit_*
344 functions: the name is a parameter here, the name is used but not defined
345 here, etc. The second pass analyzes these facts during a pass over the
346 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
348 When a function is entered during the second pass, the parent passes
349 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000350 are used to determine if non-local variables are free or implicit globals.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351 After doing the local analysis, it analyzes each of its child blocks
Nick Coghlan650f0d02007-04-15 12:05:43 +0000352 using an updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353
Nick Coghlan650f0d02007-04-15 12:05:43 +0000354 The children update the free variable set. If a local variable is added to
355 the free variable set by the child, the variable is marked as a cell. The
356 function object being defined must provide runtime storage for the variable
357 that may outlive the function's frame. Cell variables are removed from the
358 free set before the analyze function returns to its parent.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359
Nick Coghlan650f0d02007-04-15 12:05:43 +0000360 During analysis, the names are:
361 symbols: dict mapping from symbol names to flag values (including offset scope values)
362 scopes: dict mapping from symbol names to scope values (no offset)
363 local: set of all symbol names local to the current scope
364 bound: set of all symbol names local to a containing function scope
365 free: set of all symbol names referenced but not bound in child scopes
366 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367*/
368
369#define SET_SCOPE(DICT, NAME, I) { \
370 PyObject *o = PyInt_FromLong(I); \
371 if (!o) \
372 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000373 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
374 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000376 } \
377 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378}
379
380/* Decide on scope of name, given flags.
381
382 The dicts passed in as arguments are modified as necessary.
383 ste is passed so that flags can be updated.
384*/
385
386static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000387analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388 PyObject *bound, PyObject *local, PyObject *free,
389 PyObject *global)
390{
391 if (flags & DEF_GLOBAL) {
392 if (flags & DEF_PARAM) {
393 PyErr_Format(PyExc_SyntaxError,
Nick Coghlan650f0d02007-04-15 12:05:43 +0000394 "name '%s' is parameter and global",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395 PyString_AS_STRING(name));
396 return 0;
397 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000398 if (flags & DEF_NONLOCAL) {
399 PyErr_Format(PyExc_SyntaxError,
400 "name '%s' is nonlocal and global",
401 PyString_AS_STRING(name));
402 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,
Nick Coghlan650f0d02007-04-15 12:05:43 +0000414 "name '%s' is parameter and nonlocal",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000415 PyString_AS_STRING(name));
416 return 0;
417 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000418 if (!PySet_Contains(bound, name)) {
Jeremy Hylton81e95022007-02-27 06:50:52 +0000419 PyErr_Format(PyExc_SyntaxError,
420 "no binding for nonlocal '%s' found",
421 PyString_AS_STRING(name));
422
423 return 0;
424 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000425 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000426 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000427 return PySet_Add(free, name) >= 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000428 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429 if (flags & DEF_BOUND) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000430 SET_SCOPE(scopes, name, LOCAL);
431 if (PySet_Add(local, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000433 if (PySet_Discard(global, name) < 0)
434 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435 return 1;
436 }
437 /* If an enclosing block has a binding for this name, it
438 is a free variable rather than a global variable.
439 Note that having a non-NULL bound implies that the block
440 is nested.
441 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000442 if (bound && PySet_Contains(bound, name)) {
443 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000445 return PySet_Add(free, name) >= 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446 }
447 /* If a parent has a global statement, then call it global
448 explicit? It could also be global implicit.
449 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000450 if (global && PySet_Contains(global, name)) {
451 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 return 1;
453 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000454 if (ste->ste_nested)
455 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000456 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000457 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458}
459
460#undef SET_SCOPE
461
462/* If a name is defined in free and also in locals, then this block
463 provides the binding for the free variable. The name should be
464 marked CELL in this block and removed from the free list.
465
466 Note that the current block's free variables are included in free.
467 That's safe because no name can be free and local in the same scope.
468*/
469
470static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000471analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000473 PyObject *name, *v, *v_cell;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000474 int success = 0;
475 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476
Nick Coghlan650f0d02007-04-15 12:05:43 +0000477 v_cell = PyInt_FromLong(CELL);
478 if (!v_cell)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000480 while (PyDict_Next(scopes, &pos, &name, &v)) {
481 long scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000483 scope = PyInt_AS_LONG(v);
484 if (scope != LOCAL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485 continue;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000486 if (!PySet_Contains(free, name))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 continue;
488 /* Replace LOCAL with CELL for this name, and remove
489 from free. It is safe to replace the value of name
490 in the dict, because it will not cause a resize.
491 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000492 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000494 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495 goto error;
496 }
497 success = 1;
498 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000499 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 return success;
501}
502
503/* Check for illegal statements in unoptimized namespaces */
504static int
505check_unoptimized(const PySTEntryObject* ste) {
506 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000507 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000509 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 || !(ste->ste_free || ste->ste_child_free))
511 return 1;
512
Armin Rigo31441302005-10-21 12:57:31 +0000513 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 "contains a nested function with free variables" :
515 "is a nested function");
516
517 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000518 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 return 1;
520 case OPT_IMPORT_STAR:
521 PyOS_snprintf(buf, sizeof(buf),
522 "import * is not allowed in function '%.100s' "
523 "because it is %s",
524 PyString_AS_STRING(ste->ste_name), trailer);
525 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 }
527
528 PyErr_SetString(PyExc_SyntaxError, buf);
529 PyErr_SyntaxLocation(ste->ste_table->st_filename,
530 ste->ste_opt_lineno);
531 return 0;
532}
533
Nick Coghlan650f0d02007-04-15 12:05:43 +0000534/* Enter the final scope information into the ste_symbols dict.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535 *
536 * All arguments are dicts. Modifies symbols, others are read-only.
537*/
538static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000539update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000540 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000542 PyObject *name = NULL, *itr = NULL;
543 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000544 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545
Nick Coghlan650f0d02007-04-15 12:05:43 +0000546 /* Update scope information for all symbols in this scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 while (PyDict_Next(symbols, &pos, &name, &v)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000548 long scope, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549 assert(PyInt_Check(v));
550 flags = PyInt_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000551 v_scope = PyDict_GetItem(scopes, name);
552 assert(v_scope && PyInt_Check(v_scope));
553 scope = PyInt_AS_LONG(v_scope);
554 flags |= (scope << SCOPE_OFFSET);
555 v_new = PyInt_FromLong(flags);
556 if (!v_new)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000557 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000558 if (PyDict_SetItem(symbols, name, v_new) < 0) {
559 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560 return 0;
561 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000562 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 }
564
Nick Coghlan650f0d02007-04-15 12:05:43 +0000565 /* Record not yet resolved free variables from children (if any) */
566 v_free = PyInt_FromLong(FREE << SCOPE_OFFSET);
567 if (!v_free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 return 0;
569
Nick Coghlan650f0d02007-04-15 12:05:43 +0000570 itr = PyObject_GetIter(free);
571 if (!itr)
572 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573
Nick Coghlan650f0d02007-04-15 12:05:43 +0000574 while ((name = PyIter_Next(itr))) {
575 v = PyDict_GetItem(symbols, name);
576
577 /* Handle symbol that already exists in this scope */
578 if (v) {
579 /* Handle a free variable in a method of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580 the class that has the same name as a local
581 or global in the class scope.
582 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000583 if (classflag &&
Nick Coghlan650f0d02007-04-15 12:05:43 +0000584 PyInt_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
585 long flags = PyInt_AS_LONG(v) | DEF_FREE_CLASS;
586 v_new = PyInt_FromLong(flags);
587 if (!v_new) {
588 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000590 if (PyDict_SetItem(symbols, name, v_new) < 0) {
591 Py_DECREF(v_new);
592 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000594 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000596 /* It's a cell, or already a free variable in this scope */
597 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 continue;
599 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000600 /* Handle global symbol */
601 if (!PySet_Contains(bound, name)) {
602 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 continue; /* it's a global */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000605 /* Propagate new free symbol up the lexical stack */
606 if (PyDict_SetItem(symbols, name, v_free) < 0) {
607 goto error;
608 }
609 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000611 Py_DECREF(itr);
612 Py_DECREF(v_free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000614error:
615 Py_XDECREF(v_free);
616 Py_XDECREF(itr);
617 Py_XDECREF(name);
618 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619}
620
621/* Make final symbol table decisions for block of ste.
622 Arguments:
623 ste -- current symtable entry (input/output)
624 bound -- set of variables bound in enclosing scopes (input)
625 free -- set of free variables in enclosed scopes (output)
626 globals -- set of declared global variables in enclosing scopes (input)
627*/
628
629static int
630analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
631 PyObject *global)
632{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000633 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000635 int i, success = 0;
636 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637
Nick Coghlan650f0d02007-04-15 12:05:43 +0000638 scopes = PyDict_New();
639 if (!scopes)
640 goto error;
641 local = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642 if (!local)
643 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000644 newglobal = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (!newglobal)
646 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000647 newfree = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 if (!newfree)
649 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000650 newbound = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 if (!newbound)
652 goto error;
653
Nick Coghlan650f0d02007-04-15 12:05:43 +0000654 /* Class namespace has no effect on names visible in
655 nested functions, so populate the global and bound
656 sets to be passed to child blocks before analyzing
657 this one.
658 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659 if (ste->ste_type == ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000660 /* Pass down previously bound symbols */
661 if (bound) {
662 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000664 Py_DECREF(newbound);
665 }
666 /* Pass down known globals */
667 if (!PyNumber_InPlaceOr(newglobal, global))
668 goto error;
669 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670 }
671
Nick Coghlan650f0d02007-04-15 12:05:43 +0000672 /* Analyze symbols in current scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 assert(PySTEntry_Check(ste));
674 assert(PyDict_Check(ste->ste_symbols));
675 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000676 long flags = PyInt_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000677 if (!analyze_name(ste, scopes, name, flags, bound, local, free,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 global))
679 goto error;
680 }
681
Nick Coghlan650f0d02007-04-15 12:05:43 +0000682 /* Populate global and bound sets to be passed to children.
683 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000685 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 if (ste->ste_type == FunctionBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000687 if (!PyNumber_InPlaceOr(newbound, local))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000689 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000691 /* Pass down previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 if (bound) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000693 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000695 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000697 /* Pass down known globals */
698 if (!PyNumber_InPlaceOr(newglobal, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000700 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 }
702
703 /* Recursively call analyze_block() on each child block */
704 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
705 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000706 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000708 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709 if (!analyze_block(entry, newbound, newfree, newglobal))
710 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000711 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 if (entry->ste_free || entry->ste_child_free)
713 ste->ste_child_free = 1;
714 }
715
Nick Coghlan650f0d02007-04-15 12:05:43 +0000716 /* Check if any local variables need to be converted to cell variables */
717 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000719 /* Records the results of the analysis in the symbol table entry */
720 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 ste->ste_type == ClassBlock))
722 goto error;
723 if (!check_unoptimized(ste))
724 goto error;
725
Nick Coghlan650f0d02007-04-15 12:05:43 +0000726 if (!PyNumber_InPlaceOr(free, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000728 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 success = 1;
730 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000731 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733 Py_XDECREF(newbound);
734 Py_XDECREF(newglobal);
735 Py_XDECREF(newfree);
736 if (!success)
737 assert(PyErr_Occurred());
738 return success;
739}
740
741static int
742symtable_analyze(struct symtable *st)
743{
744 PyObject *free, *global;
745 int r;
746
Nick Coghlan650f0d02007-04-15 12:05:43 +0000747 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 if (!free)
749 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000750 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000752 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 return 0;
754 }
755 r = analyze_block(st->st_top, NULL, free, global);
756 Py_DECREF(free);
757 Py_DECREF(global);
758 return r;
759}
760
761
762static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000763symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764{
765 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000766 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
768 PyErr_SetString(PyExc_SyntaxError, msg);
769 PyErr_SyntaxLocation(st->st_filename,
770 st->st_cur->ste_lineno);
771 }
772 return 0;
773 }
774 return 1;
775}
776
777/* symtable_enter_block() gets a reference via PySTEntry_New().
778 This reference is released when the block is exited, via the DECREF
779 in symtable_exit_block().
780*/
781
782static int
783symtable_exit_block(struct symtable *st, void *ast)
784{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000785 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000787 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788 end = PyList_GET_SIZE(st->st_stack) - 1;
789 if (end >= 0) {
790 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
791 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000792 if (st->st_cur == NULL)
793 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 Py_INCREF(st->st_cur);
795 if (PySequence_DelItem(st->st_stack, end) < 0)
796 return 0;
797 }
798 return 1;
799}
800
801static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000802symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 void *ast, int lineno)
804{
805 PySTEntryObject *prev = NULL;
806
807 if (st->st_cur) {
808 prev = st->st_cur;
809 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 return 0;
811 }
812 Py_DECREF(st->st_cur);
813 }
814 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000815 if (st->st_cur == NULL)
816 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 if (name == GET_IDENTIFIER(top))
818 st->st_global = st->st_cur->ste_symbols;
819 if (prev) {
820 if (PyList_Append(prev->ste_children,
821 (PyObject *)st->st_cur) < 0) {
822 return 0;
823 }
824 }
825 return 1;
826}
827
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000828static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829symtable_lookup(struct symtable *st, PyObject *name)
830{
831 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000832 PyObject *mangled = _Py_Mangle(st->st_private, name);
833 if (!mangled)
834 return 0;
835 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
836 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 if (!o)
838 return 0;
839 return PyInt_AsLong(o);
840}
841
842static int
843symtable_add_def(struct symtable *st, PyObject *name, int flag)
844{
845 PyObject *o;
846 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000847 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000848 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849
Jeremy Hylton81e95022007-02-27 06:50:52 +0000850
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000851 if (!mangled)
852 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000854 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855 val = PyInt_AS_LONG(o);
856 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000857 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
859 PyString_AsString(name));
860 PyErr_SyntaxLocation(st->st_filename,
861 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000862 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 }
864 val |= flag;
865 } else
866 val = flag;
867 o = PyInt_FromLong(val);
868 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000869 goto error;
870 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000872 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 }
874 Py_DECREF(o);
875
876 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000877 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
878 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879 } else if (flag & DEF_GLOBAL) {
880 /* XXX need to update DEF_GLOBAL for other flags too;
881 perhaps only DEF_FREE_GLOBAL */
882 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000883 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 val |= PyInt_AS_LONG(o);
885 }
886 o = PyInt_FromLong(val);
887 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000888 goto error;
889 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000891 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 }
893 Py_DECREF(o);
894 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000895 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000897
898error:
899 Py_DECREF(mangled);
900 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901}
902
903/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
904 They use the ASDL name to synthesize the name of the C type and the visit
905 function.
906
907 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
908 useful if the first node in the sequence requires special treatment.
909*/
910
911#define VISIT(ST, TYPE, V) \
912 if (!symtable_visit_ ## TYPE((ST), (V))) \
913 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000914
915#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
916 if (!symtable_visit_ ## TYPE((ST), (V))) { \
917 symtable_exit_block((ST), (S)); \
918 return 0; \
919 }
920
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921#define VISIT_SEQ(ST, TYPE, SEQ) { \
922 int i; \
923 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
924 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000925 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 if (!symtable_visit_ ## TYPE((ST), elt)) \
927 return 0; \
928 } \
929}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000930
931#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
932 int i; \
933 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
934 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000935 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000936 if (!symtable_visit_ ## TYPE((ST), elt)) { \
937 symtable_exit_block((ST), (S)); \
938 return 0; \
939 } \
940 } \
941}
942
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
944 int i; \
945 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
946 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000947 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 if (!symtable_visit_ ## TYPE((ST), elt)) \
949 return 0; \
950 } \
951}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000952
953#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
954 int i; \
955 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
956 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000957 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000958 if (!symtable_visit_ ## TYPE((ST), elt)) { \
959 symtable_exit_block((ST), (S)); \
960 return 0; \
961 } \
962 } \
963}
964
Guido van Rossum4f72a782006-10-27 23:31:49 +0000965#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
966 int i = 0; \
967 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
968 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
969 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
970 if (!elt) continue; /* can be NULL */ \
971 if (!symtable_visit_expr((ST), elt)) \
972 return 0; \
973 } \
974}
975
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000977symtable_new_tmpname(struct symtable *st)
978{
979 char tmpname[256];
980 identifier tmp;
981
982 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
983 ++st->st_cur->ste_tmpname);
984 tmp = PyString_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000985 if (!tmp)
986 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000987 if (!symtable_add_def(st, tmp, DEF_LOCAL))
988 return 0;
989 Py_DECREF(tmp);
990 return 1;
991}
992
Guido van Rossum4f72a782006-10-27 23:31:49 +0000993
994
Guido van Rossumc2e20742006-02-27 22:32:47 +0000995static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996symtable_visit_stmt(struct symtable *st, stmt_ty s)
997{
998 switch (s->kind) {
999 case FunctionDef_kind:
1000 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1001 return 0;
1002 if (s->v.FunctionDef.args->defaults)
1003 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001004 if (s->v.FunctionDef.args->kw_defaults)
1005 VISIT_KWONLYDEFAULTS(st,
1006 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001007 if (!symtable_visit_annotations(st, s))
1008 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 if (s->v.FunctionDef.decorators)
1010 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
1011 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1012 FunctionBlock, (void *)s, s->lineno))
1013 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001014 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1015 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 if (!symtable_exit_block(st, s))
1017 return 0;
1018 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001019 case ClassDef_kind: {
1020 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1022 return 0;
1023 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001024 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1025 if (s->v.ClassDef.starargs)
1026 VISIT(st, expr, s->v.ClassDef.starargs);
1027 if (s->v.ClassDef.kwargs)
1028 VISIT(st, expr, s->v.ClassDef.kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1030 (void *)s, s->lineno))
1031 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001032 tmp = st->st_private;
1033 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001034 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001035 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 if (!symtable_exit_block(st, s))
1037 return 0;
1038 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001039 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001041 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001043 st->st_cur->ste_returns_value = 1;
1044 if (st->st_cur->ste_generator) {
1045 PyErr_SetString(PyExc_SyntaxError,
1046 RETURN_VAL_IN_GENERATOR);
1047 PyErr_SyntaxLocation(st->st_filename,
1048 s->lineno);
1049 return 0;
1050 }
1051 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 break;
1053 case Delete_kind:
1054 VISIT_SEQ(st, expr, s->v.Delete.targets);
1055 break;
1056 case Assign_kind:
1057 VISIT_SEQ(st, expr, s->v.Assign.targets);
1058 VISIT(st, expr, s->v.Assign.value);
1059 break;
1060 case AugAssign_kind:
1061 VISIT(st, expr, s->v.AugAssign.target);
1062 VISIT(st, expr, s->v.AugAssign.value);
1063 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064 case For_kind:
1065 VISIT(st, expr, s->v.For.target);
1066 VISIT(st, expr, s->v.For.iter);
1067 VISIT_SEQ(st, stmt, s->v.For.body);
1068 if (s->v.For.orelse)
1069 VISIT_SEQ(st, stmt, s->v.For.orelse);
1070 break;
1071 case While_kind:
1072 VISIT(st, expr, s->v.While.test);
1073 VISIT_SEQ(st, stmt, s->v.While.body);
1074 if (s->v.While.orelse)
1075 VISIT_SEQ(st, stmt, s->v.While.orelse);
1076 break;
1077 case If_kind:
1078 /* XXX if 0: and lookup_yield() hacks */
1079 VISIT(st, expr, s->v.If.test);
1080 VISIT_SEQ(st, stmt, s->v.If.body);
1081 if (s->v.If.orelse)
1082 VISIT_SEQ(st, stmt, s->v.If.orelse);
1083 break;
1084 case Raise_kind:
1085 if (s->v.Raise.type) {
1086 VISIT(st, expr, s->v.Raise.type);
1087 if (s->v.Raise.inst) {
1088 VISIT(st, expr, s->v.Raise.inst);
1089 if (s->v.Raise.tback)
1090 VISIT(st, expr, s->v.Raise.tback);
1091 }
1092 }
1093 break;
1094 case TryExcept_kind:
1095 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1096 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1097 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1098 break;
1099 case TryFinally_kind:
1100 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1101 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1102 break;
1103 case Assert_kind:
1104 VISIT(st, expr, s->v.Assert.test);
1105 if (s->v.Assert.msg)
1106 VISIT(st, expr, s->v.Assert.msg);
1107 break;
1108 case Import_kind:
1109 VISIT_SEQ(st, alias, s->v.Import.names);
1110 /* XXX Don't have the lineno available inside
1111 visit_alias */
1112 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1113 st->st_cur->ste_opt_lineno = s->lineno;
1114 break;
1115 case ImportFrom_kind:
1116 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1117 /* XXX Don't have the lineno available inside
1118 visit_alias */
1119 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1120 st->st_cur->ste_opt_lineno = s->lineno;
1121 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 case Global_kind: {
1123 int i;
1124 asdl_seq *seq = s->v.Global.names;
1125 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001126 identifier name = (identifier)asdl_seq_GET(seq, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001128 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 if (cur < 0)
1130 return 0;
1131 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001132 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 if (cur & DEF_LOCAL)
1134 PyOS_snprintf(buf, sizeof(buf),
1135 GLOBAL_AFTER_ASSIGN,
1136 c_name);
1137 else
1138 PyOS_snprintf(buf, sizeof(buf),
1139 GLOBAL_AFTER_USE,
1140 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001141 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 return 0;
1143 }
1144 if (!symtable_add_def(st, name, DEF_GLOBAL))
1145 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 break;
1148 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001149 case Nonlocal_kind: {
1150 int i;
1151 asdl_seq *seq = s->v.Nonlocal.names;
1152 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1153 identifier name = (identifier)asdl_seq_GET(seq, i);
1154 char *c_name = PyString_AS_STRING(name);
1155 long cur = symtable_lookup(st, name);
1156 if (cur < 0)
1157 return 0;
1158 if (cur & (DEF_LOCAL | USE)) {
1159 char buf[256];
1160 if (cur & DEF_LOCAL)
1161 PyOS_snprintf(buf, sizeof(buf),
1162 NONLOCAL_AFTER_ASSIGN,
1163 c_name);
1164 else
1165 PyOS_snprintf(buf, sizeof(buf),
1166 NONLOCAL_AFTER_USE,
1167 c_name);
1168 if (!symtable_warn(st, buf, s->lineno))
1169 return 0;
1170 }
1171 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1172 return 0;
1173 }
1174 break;
1175 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 case Expr_kind:
1177 VISIT(st, expr, s->v.Expr.value);
1178 break;
1179 case Pass_kind:
1180 case Break_kind:
1181 case Continue_kind:
1182 /* nothing to do here */
1183 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001184 case With_kind:
1185 if (!symtable_new_tmpname(st))
1186 return 0;
1187 VISIT(st, expr, s->v.With.context_expr);
1188 if (s->v.With.optional_vars) {
1189 if (!symtable_new_tmpname(st))
1190 return 0;
1191 VISIT(st, expr, s->v.With.optional_vars);
1192 }
1193 VISIT_SEQ(st, stmt, s->v.With.body);
1194 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 }
1196 return 1;
1197}
1198
1199static int
1200symtable_visit_expr(struct symtable *st, expr_ty e)
1201{
1202 switch (e->kind) {
1203 case BoolOp_kind:
1204 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1205 break;
1206 case BinOp_kind:
1207 VISIT(st, expr, e->v.BinOp.left);
1208 VISIT(st, expr, e->v.BinOp.right);
1209 break;
1210 case UnaryOp_kind:
1211 VISIT(st, expr, e->v.UnaryOp.operand);
1212 break;
1213 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001214 if (!GET_IDENTIFIER(lambda) ||
1215 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 return 0;
1217 if (e->v.Lambda.args->defaults)
1218 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1219 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001220 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 FunctionBlock, (void *)e, 0))
1222 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001223 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1224 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 if (!symtable_exit_block(st, (void *)e))
1226 return 0;
1227 break;
1228 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001229 case IfExp_kind:
1230 VISIT(st, expr, e->v.IfExp.test);
1231 VISIT(st, expr, e->v.IfExp.body);
1232 VISIT(st, expr, e->v.IfExp.orelse);
1233 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 case Dict_kind:
1235 VISIT_SEQ(st, expr, e->v.Dict.keys);
1236 VISIT_SEQ(st, expr, e->v.Dict.values);
1237 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001238 case Set_kind:
1239 VISIT_SEQ(st, expr, e->v.Set.elts);
1240 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001241 case GeneratorExp_kind:
1242 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001245 case ListComp_kind:
1246 if (!symtable_visit_listcomp(st, e))
1247 return 0;
1248 break;
1249 case SetComp_kind:
1250 if (!symtable_visit_setcomp(st, e))
1251 return 0;
1252 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 case Yield_kind:
1254 if (e->v.Yield.value)
1255 VISIT(st, expr, e->v.Yield.value);
1256 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001257 if (st->st_cur->ste_returns_value) {
1258 PyErr_SetString(PyExc_SyntaxError,
1259 RETURN_VAL_IN_GENERATOR);
1260 PyErr_SyntaxLocation(st->st_filename,
1261 e->lineno);
1262 return 0;
1263 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 break;
1265 case Compare_kind:
1266 VISIT(st, expr, e->v.Compare.left);
1267 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1268 break;
1269 case Call_kind:
1270 VISIT(st, expr, e->v.Call.func);
1271 VISIT_SEQ(st, expr, e->v.Call.args);
1272 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1273 if (e->v.Call.starargs)
1274 VISIT(st, expr, e->v.Call.starargs);
1275 if (e->v.Call.kwargs)
1276 VISIT(st, expr, e->v.Call.kwargs);
1277 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 case Num_kind:
1279 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001280 case Bytes_kind:
1281 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 /* Nothing to do here. */
1283 break;
1284 /* The following exprs can be assignment targets. */
1285 case Attribute_kind:
1286 VISIT(st, expr, e->v.Attribute.value);
1287 break;
1288 case Subscript_kind:
1289 VISIT(st, expr, e->v.Subscript.value);
1290 VISIT(st, slice, e->v.Subscript.slice);
1291 break;
1292 case Name_kind:
1293 if (!symtable_add_def(st, e->v.Name.id,
1294 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1295 return 0;
1296 break;
1297 /* child nodes of List and Tuple will have expr_context set */
1298 case List_kind:
1299 VISIT_SEQ(st, expr, e->v.List.elts);
1300 break;
1301 case Tuple_kind:
1302 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1303 break;
1304 }
1305 return 1;
1306}
1307
1308static int
1309symtable_implicit_arg(struct symtable *st, int pos)
1310{
1311 PyObject *id = PyString_FromFormat(".%d", pos);
1312 if (id == NULL)
1313 return 0;
1314 if (!symtable_add_def(st, id, DEF_PARAM)) {
1315 Py_DECREF(id);
1316 return 0;
1317 }
1318 Py_DECREF(id);
1319 return 1;
1320}
1321
1322static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001323symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel,
1324 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001326 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001327
1328 if (!args)
1329 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330
1331 /* go through all the toplevel arguments first */
1332 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001333 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1334 if (arg->kind == SimpleArg_kind) {
1335 if (!annotations) {
1336 if (!symtable_add_def(st,
1337 arg->v.SimpleArg.arg,
1338 DEF_PARAM))
1339 return 0;
1340 }
1341 else if (arg->v.SimpleArg.annotation)
1342 VISIT(st, expr, arg->v.SimpleArg.annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001344 else if (arg->kind == NestedArgs_kind) {
1345 if (toplevel && !annotations) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 if (!symtable_implicit_arg(st, i))
1347 return 0;
1348 }
1349 }
1350 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001351 PyErr_SetString(PyExc_SyntaxError,
1352 "invalid expression in parameter list");
1353 PyErr_SyntaxLocation(st->st_filename,
1354 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 return 0;
1356 }
1357 }
1358
1359 if (!toplevel) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001360 if (!symtable_visit_params_nested(st, args, annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 return 0;
1362 }
1363
1364 return 1;
1365}
1366
1367static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001368symtable_visit_params_nested(struct symtable *st, asdl_seq *args,
1369 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370{
1371 int i;
1372 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001373 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1374 if (arg->kind == NestedArgs_kind &&
1375 !symtable_visit_params(st, arg->v.NestedArgs.args, 0,
1376 annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 return 0;
1378 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001379
1380 return 1;
1381}
1382
1383
1384static int
1385symtable_visit_annotations(struct symtable *st, stmt_ty s)
1386{
1387 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388
Neal Norwitzc1505362006-12-28 06:47:50 +00001389 if (a->args && !symtable_visit_params(st, a->args, 1, 1))
1390 return 0;
1391 if (a->varargannotation)
1392 VISIT(st, expr, a->varargannotation);
1393 if (a->kwargannotation)
1394 VISIT(st, expr, a->kwargannotation);
1395 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 1))
1396 return 0;
1397 if (s->v.FunctionDef.returns)
1398 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 return 1;
1400}
1401
1402static int
1403symtable_visit_arguments(struct symtable *st, arguments_ty a)
1404{
1405 /* skip default arguments inside function block
1406 XXX should ast be different?
1407 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001408 if (a->args && !symtable_visit_params(st, a->args, 1, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001410 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 0))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 if (a->vararg) {
1413 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1414 return 0;
1415 st->st_cur->ste_varargs = 1;
1416 }
1417 if (a->kwarg) {
1418 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1419 return 0;
1420 st->st_cur->ste_varkeywords = 1;
1421 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001422 if (a->args && !symtable_visit_params_nested(st, a->args, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 return 0;
1424 return 1;
1425}
1426
1427
1428static int
1429symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1430{
1431 if (eh->type)
1432 VISIT(st, expr, eh->type);
1433 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001434 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1435 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 VISIT_SEQ(st, stmt, eh->body);
1437 return 1;
1438}
1439
1440
1441static int
1442symtable_visit_alias(struct symtable *st, alias_ty a)
1443{
1444 /* Compute store_name, the name actually bound by the import
1445 operation. It is diferent than a->name when a->name is a
1446 dotted package name (e.g. spam.eggs)
1447 */
1448 PyObject *store_name;
1449 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1450 const char *base = PyString_AS_STRING(name);
1451 char *dot = strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001452 if (dot) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 store_name = PyString_FromStringAndSize(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001454 if (!store_name)
1455 return 0;
1456 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 else {
1458 store_name = name;
1459 Py_INCREF(store_name);
1460 }
1461 if (strcmp(PyString_AS_STRING(name), "*")) {
1462 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1463 Py_DECREF(store_name);
1464 return r;
1465 }
1466 else {
1467 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001468 int lineno = st->st_cur->ste_lineno;
1469 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001470 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 }
1474 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001475 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 return 1;
1477 }
1478}
1479
1480
1481static int
1482symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1483{
1484 VISIT(st, expr, lc->target);
1485 VISIT(st, expr, lc->iter);
1486 VISIT_SEQ(st, expr, lc->ifs);
1487 return 1;
1488}
1489
1490
1491static int
1492symtable_visit_keyword(struct symtable *st, keyword_ty k)
1493{
1494 VISIT(st, expr, k->value);
1495 return 1;
1496}
1497
1498
1499static int
1500symtable_visit_slice(struct symtable *st, slice_ty s)
1501{
1502 switch (s->kind) {
1503 case Slice_kind:
1504 if (s->v.Slice.lower)
1505 VISIT(st, expr, s->v.Slice.lower)
1506 if (s->v.Slice.upper)
1507 VISIT(st, expr, s->v.Slice.upper)
1508 if (s->v.Slice.step)
1509 VISIT(st, expr, s->v.Slice.step)
1510 break;
1511 case ExtSlice_kind:
1512 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1513 break;
1514 case Index_kind:
1515 VISIT(st, expr, s->v.Index.value)
1516 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 }
1518 return 1;
1519}
1520
1521static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001522symtable_handle_comprehension(struct symtable *st, expr_ty e,
1523 identifier scope_name,
1524 asdl_seq *generators, expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001526 int is_generator = (e->kind == GeneratorExp_kind);
1527 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001529 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530 /* Outermost iterator is evaluated in current scope */
1531 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001532 /* Create comprehension scope for the rest */
1533 if (!scope_name ||
1534 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535 return 0;
1536 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001537 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 /* Outermost iter is received as an argument */
1539 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001540 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 return 0;
1542 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001543 /* Allocate temporary name if needed */
1544 if (needs_tmp && !symtable_new_tmpname(st)) {
1545 symtable_exit_block(st, (void *)e);
1546 return 0;
1547 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001548 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1549 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1550 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001551 generators, 1, (void*)e);
1552 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001553 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001555
1556static int
1557symtable_visit_genexp(struct symtable *st, expr_ty e)
1558{
1559 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1560 e->v.GeneratorExp.generators,
1561 e->v.GeneratorExp.elt);
1562}
1563
1564static int
1565symtable_visit_listcomp(struct symtable *st, expr_ty e)
1566{
1567 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1568 e->v.ListComp.generators,
1569 e->v.ListComp.elt);
1570}
1571
1572static int
1573symtable_visit_setcomp(struct symtable *st, expr_ty e)
1574{
1575 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1576 e->v.SetComp.generators,
1577 e->v.SetComp.elt);
1578}