blob: ca7d502fc55bedeca11eac40de69f5fcdd9a5966 [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
340 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000341 The first pass collects raw facts from the AST via the symtable_visit_*
342 functions: the name is a parameter here, the name is used but not defined
343 here, etc. The second pass analyzes these facts during a pass over the
344 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345
346 When a function is entered during the second pass, the parent passes
347 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000348 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000349 Names which are explicitly declared nonlocal must exist in this set of
350 visible names - if they do not, a syntax error is raised. After doing
351 the local analysis, it analyzes each of its child blocks using an
352 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.
Nick Coghlan4138bfe2007-04-23 10:14:27 +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 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,
425 "no binding for nonlocal '%s' found",
426 PyString_AS_STRING(name));
427
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)) {
456 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
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.
473*/
474
475static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000476analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000478 PyObject *name, *v, *v_cell;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000479 int success = 0;
480 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481
Nick Coghlan650f0d02007-04-15 12:05:43 +0000482 v_cell = PyInt_FromLong(CELL);
483 if (!v_cell)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000485 while (PyDict_Next(scopes, &pos, &name, &v)) {
486 long scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000488 scope = PyInt_AS_LONG(v);
489 if (scope != LOCAL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 continue;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000491 if (!PySet_Contains(free, name))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492 continue;
493 /* Replace LOCAL with CELL for this name, and remove
494 from free. It is safe to replace the value of name
495 in the dict, because it will not cause a resize.
496 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000497 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000499 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 goto error;
501 }
502 success = 1;
503 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000504 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000505 return success;
506}
507
508/* Check for illegal statements in unoptimized namespaces */
509static int
510check_unoptimized(const PySTEntryObject* ste) {
511 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000512 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000514 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 || !(ste->ste_free || ste->ste_child_free))
516 return 1;
517
Armin Rigo31441302005-10-21 12:57:31 +0000518 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 "contains a nested function with free variables" :
520 "is a nested function");
521
522 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000523 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 return 1;
525 case OPT_IMPORT_STAR:
526 PyOS_snprintf(buf, sizeof(buf),
527 "import * is not allowed in function '%.100s' "
528 "because it is %s",
529 PyString_AS_STRING(ste->ste_name), trailer);
530 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 }
532
533 PyErr_SetString(PyExc_SyntaxError, buf);
534 PyErr_SyntaxLocation(ste->ste_table->st_filename,
535 ste->ste_opt_lineno);
536 return 0;
537}
538
Nick Coghlan650f0d02007-04-15 12:05:43 +0000539/* Enter the final scope information into the ste_symbols dict.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540 *
541 * All arguments are dicts. Modifies symbols, others are read-only.
542*/
543static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000544update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000547 PyObject *name = NULL, *itr = NULL;
548 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000549 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
Nick Coghlan650f0d02007-04-15 12:05:43 +0000551 /* Update scope information for all symbols in this scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552 while (PyDict_Next(symbols, &pos, &name, &v)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000553 long scope, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554 assert(PyInt_Check(v));
555 flags = PyInt_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000556 v_scope = PyDict_GetItem(scopes, name);
557 assert(v_scope && PyInt_Check(v_scope));
558 scope = PyInt_AS_LONG(v_scope);
559 flags |= (scope << SCOPE_OFFSET);
560 v_new = PyInt_FromLong(flags);
561 if (!v_new)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000562 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000563 if (PyDict_SetItem(symbols, name, v_new) < 0) {
564 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565 return 0;
566 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000567 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 }
569
Nick Coghlan650f0d02007-04-15 12:05:43 +0000570 /* Record not yet resolved free variables from children (if any) */
571 v_free = PyInt_FromLong(FREE << SCOPE_OFFSET);
572 if (!v_free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 return 0;
574
Nick Coghlan650f0d02007-04-15 12:05:43 +0000575 itr = PyObject_GetIter(free);
576 if (!itr)
577 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578
Nick Coghlan650f0d02007-04-15 12:05:43 +0000579 while ((name = PyIter_Next(itr))) {
580 v = PyDict_GetItem(symbols, name);
581
582 /* Handle symbol that already exists in this scope */
583 if (v) {
584 /* Handle a free variable in a method of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585 the class that has the same name as a local
586 or global in the class scope.
587 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000588 if (classflag &&
Nick Coghlan650f0d02007-04-15 12:05:43 +0000589 PyInt_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
590 long flags = PyInt_AS_LONG(v) | DEF_FREE_CLASS;
591 v_new = PyInt_FromLong(flags);
592 if (!v_new) {
593 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000595 if (PyDict_SetItem(symbols, name, v_new) < 0) {
596 Py_DECREF(v_new);
597 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000599 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000601 /* It's a cell, or already a free variable in this scope */
602 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 continue;
604 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000605 /* Handle global symbol */
606 if (!PySet_Contains(bound, name)) {
607 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 continue; /* it's a global */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000610 /* Propagate new free symbol up the lexical stack */
611 if (PyDict_SetItem(symbols, name, v_free) < 0) {
612 goto error;
613 }
614 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000616 Py_DECREF(itr);
617 Py_DECREF(v_free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000619error:
620 Py_XDECREF(v_free);
621 Py_XDECREF(itr);
622 Py_XDECREF(name);
623 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624}
625
626/* Make final symbol table decisions for block of ste.
627 Arguments:
628 ste -- current symtable entry (input/output)
629 bound -- set of variables bound in enclosing scopes (input)
630 free -- set of free variables in enclosed scopes (output)
631 globals -- set of declared global variables in enclosing scopes (input)
632*/
633
634static int
635analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
636 PyObject *global)
637{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000638 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000640 int i, success = 0;
641 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642
Nick Coghlan650f0d02007-04-15 12:05:43 +0000643 scopes = PyDict_New();
644 if (!scopes)
645 goto error;
646 local = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 if (!local)
648 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000649 newglobal = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 if (!newglobal)
651 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000652 newfree = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653 if (!newfree)
654 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000655 newbound = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656 if (!newbound)
657 goto error;
658
Nick Coghlan650f0d02007-04-15 12:05:43 +0000659 /* Class namespace has no effect on names visible in
660 nested functions, so populate the global and bound
661 sets to be passed to child blocks before analyzing
662 this one.
663 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664 if (ste->ste_type == ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000665 /* Pass down previously bound symbols */
666 if (bound) {
667 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000669 Py_DECREF(newbound);
670 }
671 /* Pass down known globals */
672 if (!PyNumber_InPlaceOr(newglobal, global))
673 goto error;
674 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 }
676
Nick Coghlan650f0d02007-04-15 12:05:43 +0000677 /* Analyze symbols in current scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 assert(PySTEntry_Check(ste));
679 assert(PyDict_Check(ste->ste_symbols));
680 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000681 long flags = PyInt_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000682 if (!analyze_name(ste, scopes, name, flags, bound, local, free,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683 global))
684 goto error;
685 }
686
Nick Coghlan650f0d02007-04-15 12:05:43 +0000687 /* Populate global and bound sets to be passed to children.
688 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000690 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 if (ste->ste_type == FunctionBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000692 if (!PyNumber_InPlaceOr(newbound, local))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000694 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000696 /* Pass down previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 if (bound) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000698 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000700 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000702 /* Pass down known globals */
703 if (!PyNumber_InPlaceOr(newglobal, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000705 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706 }
707
708 /* Recursively call analyze_block() on each child block */
709 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
710 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000711 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000713 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714 if (!analyze_block(entry, newbound, newfree, newglobal))
715 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000716 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717 if (entry->ste_free || entry->ste_child_free)
718 ste->ste_child_free = 1;
719 }
720
Nick Coghlan650f0d02007-04-15 12:05:43 +0000721 /* Check if any local variables need to be converted to cell variables */
722 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000724 /* Records the results of the analysis in the symbol table entry */
725 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 ste->ste_type == ClassBlock))
727 goto error;
728 if (!check_unoptimized(ste))
729 goto error;
730
Nick Coghlan650f0d02007-04-15 12:05:43 +0000731 if (!PyNumber_InPlaceOr(free, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000733 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 success = 1;
735 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000736 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 Py_XDECREF(newbound);
739 Py_XDECREF(newglobal);
740 Py_XDECREF(newfree);
741 if (!success)
742 assert(PyErr_Occurred());
743 return success;
744}
745
746static int
747symtable_analyze(struct symtable *st)
748{
749 PyObject *free, *global;
750 int r;
751
Nick Coghlan650f0d02007-04-15 12:05:43 +0000752 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 if (!free)
754 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000755 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000757 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 return 0;
759 }
760 r = analyze_block(st->st_top, NULL, free, global);
761 Py_DECREF(free);
762 Py_DECREF(global);
763 return r;
764}
765
766
767static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000768symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769{
770 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000771 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
773 PyErr_SetString(PyExc_SyntaxError, msg);
774 PyErr_SyntaxLocation(st->st_filename,
775 st->st_cur->ste_lineno);
776 }
777 return 0;
778 }
779 return 1;
780}
781
782/* symtable_enter_block() gets a reference via PySTEntry_New().
783 This reference is released when the block is exited, via the DECREF
784 in symtable_exit_block().
785*/
786
787static int
788symtable_exit_block(struct symtable *st, void *ast)
789{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000790 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000792 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 end = PyList_GET_SIZE(st->st_stack) - 1;
794 if (end >= 0) {
795 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
796 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000797 if (st->st_cur == NULL)
798 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 Py_INCREF(st->st_cur);
800 if (PySequence_DelItem(st->st_stack, end) < 0)
801 return 0;
802 }
803 return 1;
804}
805
806static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000807symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 void *ast, int lineno)
809{
810 PySTEntryObject *prev = NULL;
811
812 if (st->st_cur) {
813 prev = st->st_cur;
814 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 return 0;
816 }
817 Py_DECREF(st->st_cur);
818 }
819 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000820 if (st->st_cur == NULL)
821 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 if (name == GET_IDENTIFIER(top))
823 st->st_global = st->st_cur->ste_symbols;
824 if (prev) {
825 if (PyList_Append(prev->ste_children,
826 (PyObject *)st->st_cur) < 0) {
827 return 0;
828 }
829 }
830 return 1;
831}
832
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000833static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834symtable_lookup(struct symtable *st, PyObject *name)
835{
836 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000837 PyObject *mangled = _Py_Mangle(st->st_private, name);
838 if (!mangled)
839 return 0;
840 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
841 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 if (!o)
843 return 0;
844 return PyInt_AsLong(o);
845}
846
847static int
848symtable_add_def(struct symtable *st, PyObject *name, int flag)
849{
850 PyObject *o;
851 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000852 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000853 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854
Jeremy Hylton81e95022007-02-27 06:50:52 +0000855
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000856 if (!mangled)
857 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000859 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 val = PyInt_AS_LONG(o);
861 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000862 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
864 PyString_AsString(name));
865 PyErr_SyntaxLocation(st->st_filename,
866 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000867 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 }
869 val |= flag;
870 } else
871 val = flag;
872 o = PyInt_FromLong(val);
873 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000874 goto error;
875 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000877 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878 }
879 Py_DECREF(o);
880
881 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000882 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
883 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 } else if (flag & DEF_GLOBAL) {
885 /* XXX need to update DEF_GLOBAL for other flags too;
886 perhaps only DEF_FREE_GLOBAL */
887 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000888 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889 val |= PyInt_AS_LONG(o);
890 }
891 o = PyInt_FromLong(val);
892 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000893 goto error;
894 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000896 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897 }
898 Py_DECREF(o);
899 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000900 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000902
903error:
904 Py_DECREF(mangled);
905 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906}
907
908/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
909 They use the ASDL name to synthesize the name of the C type and the visit
910 function.
911
912 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
913 useful if the first node in the sequence requires special treatment.
914*/
915
916#define VISIT(ST, TYPE, V) \
917 if (!symtable_visit_ ## TYPE((ST), (V))) \
918 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000919
920#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
921 if (!symtable_visit_ ## TYPE((ST), (V))) { \
922 symtable_exit_block((ST), (S)); \
923 return 0; \
924 }
925
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926#define VISIT_SEQ(ST, TYPE, SEQ) { \
927 int i; \
928 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
929 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000930 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931 if (!symtable_visit_ ## TYPE((ST), elt)) \
932 return 0; \
933 } \
934}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000935
936#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
937 int i; \
938 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
939 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000940 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000941 if (!symtable_visit_ ## TYPE((ST), elt)) { \
942 symtable_exit_block((ST), (S)); \
943 return 0; \
944 } \
945 } \
946}
947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
949 int i; \
950 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
951 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000952 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953 if (!symtable_visit_ ## TYPE((ST), elt)) \
954 return 0; \
955 } \
956}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000957
958#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
959 int i; \
960 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
961 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000962 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000963 if (!symtable_visit_ ## TYPE((ST), elt)) { \
964 symtable_exit_block((ST), (S)); \
965 return 0; \
966 } \
967 } \
968}
969
Guido van Rossum4f72a782006-10-27 23:31:49 +0000970#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
971 int i = 0; \
972 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
973 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
974 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
975 if (!elt) continue; /* can be NULL */ \
976 if (!symtable_visit_expr((ST), elt)) \
977 return 0; \
978 } \
979}
980
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000982symtable_new_tmpname(struct symtable *st)
983{
984 char tmpname[256];
985 identifier tmp;
986
987 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
988 ++st->st_cur->ste_tmpname);
989 tmp = PyString_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000990 if (!tmp)
991 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000992 if (!symtable_add_def(st, tmp, DEF_LOCAL))
993 return 0;
994 Py_DECREF(tmp);
995 return 1;
996}
997
Guido van Rossum4f72a782006-10-27 23:31:49 +0000998
999
Guido van Rossumc2e20742006-02-27 22:32:47 +00001000static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001symtable_visit_stmt(struct symtable *st, stmt_ty s)
1002{
1003 switch (s->kind) {
1004 case FunctionDef_kind:
1005 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1006 return 0;
1007 if (s->v.FunctionDef.args->defaults)
1008 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001009 if (s->v.FunctionDef.args->kw_defaults)
1010 VISIT_KWONLYDEFAULTS(st,
1011 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001012 if (!symtable_visit_annotations(st, s))
1013 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 if (s->v.FunctionDef.decorators)
1015 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
1016 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1017 FunctionBlock, (void *)s, s->lineno))
1018 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001019 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1020 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 if (!symtable_exit_block(st, s))
1022 return 0;
1023 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001024 case ClassDef_kind: {
1025 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1027 return 0;
1028 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001029 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1030 if (s->v.ClassDef.starargs)
1031 VISIT(st, expr, s->v.ClassDef.starargs);
1032 if (s->v.ClassDef.kwargs)
1033 VISIT(st, expr, s->v.ClassDef.kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1035 (void *)s, s->lineno))
1036 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001037 tmp = st->st_private;
1038 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001039 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001040 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 if (!symtable_exit_block(st, s))
1042 return 0;
1043 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001044 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001046 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001048 st->st_cur->ste_returns_value = 1;
1049 if (st->st_cur->ste_generator) {
1050 PyErr_SetString(PyExc_SyntaxError,
1051 RETURN_VAL_IN_GENERATOR);
1052 PyErr_SyntaxLocation(st->st_filename,
1053 s->lineno);
1054 return 0;
1055 }
1056 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 break;
1058 case Delete_kind:
1059 VISIT_SEQ(st, expr, s->v.Delete.targets);
1060 break;
1061 case Assign_kind:
1062 VISIT_SEQ(st, expr, s->v.Assign.targets);
1063 VISIT(st, expr, s->v.Assign.value);
1064 break;
1065 case AugAssign_kind:
1066 VISIT(st, expr, s->v.AugAssign.target);
1067 VISIT(st, expr, s->v.AugAssign.value);
1068 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 case For_kind:
1070 VISIT(st, expr, s->v.For.target);
1071 VISIT(st, expr, s->v.For.iter);
1072 VISIT_SEQ(st, stmt, s->v.For.body);
1073 if (s->v.For.orelse)
1074 VISIT_SEQ(st, stmt, s->v.For.orelse);
1075 break;
1076 case While_kind:
1077 VISIT(st, expr, s->v.While.test);
1078 VISIT_SEQ(st, stmt, s->v.While.body);
1079 if (s->v.While.orelse)
1080 VISIT_SEQ(st, stmt, s->v.While.orelse);
1081 break;
1082 case If_kind:
1083 /* XXX if 0: and lookup_yield() hacks */
1084 VISIT(st, expr, s->v.If.test);
1085 VISIT_SEQ(st, stmt, s->v.If.body);
1086 if (s->v.If.orelse)
1087 VISIT_SEQ(st, stmt, s->v.If.orelse);
1088 break;
1089 case Raise_kind:
1090 if (s->v.Raise.type) {
1091 VISIT(st, expr, s->v.Raise.type);
1092 if (s->v.Raise.inst) {
1093 VISIT(st, expr, s->v.Raise.inst);
1094 if (s->v.Raise.tback)
1095 VISIT(st, expr, s->v.Raise.tback);
1096 }
1097 }
1098 break;
1099 case TryExcept_kind:
1100 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1101 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1102 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1103 break;
1104 case TryFinally_kind:
1105 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1106 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1107 break;
1108 case Assert_kind:
1109 VISIT(st, expr, s->v.Assert.test);
1110 if (s->v.Assert.msg)
1111 VISIT(st, expr, s->v.Assert.msg);
1112 break;
1113 case Import_kind:
1114 VISIT_SEQ(st, alias, s->v.Import.names);
1115 /* XXX Don't have the lineno available inside
1116 visit_alias */
1117 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1118 st->st_cur->ste_opt_lineno = s->lineno;
1119 break;
1120 case ImportFrom_kind:
1121 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1122 /* XXX Don't have the lineno available inside
1123 visit_alias */
1124 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1125 st->st_cur->ste_opt_lineno = s->lineno;
1126 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 case Global_kind: {
1128 int i;
1129 asdl_seq *seq = s->v.Global.names;
1130 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001131 identifier name = (identifier)asdl_seq_GET(seq, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001133 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 if (cur < 0)
1135 return 0;
1136 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001137 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 if (cur & DEF_LOCAL)
1139 PyOS_snprintf(buf, sizeof(buf),
1140 GLOBAL_AFTER_ASSIGN,
1141 c_name);
1142 else
1143 PyOS_snprintf(buf, sizeof(buf),
1144 GLOBAL_AFTER_USE,
1145 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001146 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 return 0;
1148 }
1149 if (!symtable_add_def(st, name, DEF_GLOBAL))
1150 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 break;
1153 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001154 case Nonlocal_kind: {
1155 int i;
1156 asdl_seq *seq = s->v.Nonlocal.names;
1157 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1158 identifier name = (identifier)asdl_seq_GET(seq, i);
1159 char *c_name = PyString_AS_STRING(name);
1160 long cur = symtable_lookup(st, name);
1161 if (cur < 0)
1162 return 0;
1163 if (cur & (DEF_LOCAL | USE)) {
1164 char buf[256];
1165 if (cur & DEF_LOCAL)
1166 PyOS_snprintf(buf, sizeof(buf),
1167 NONLOCAL_AFTER_ASSIGN,
1168 c_name);
1169 else
1170 PyOS_snprintf(buf, sizeof(buf),
1171 NONLOCAL_AFTER_USE,
1172 c_name);
1173 if (!symtable_warn(st, buf, s->lineno))
1174 return 0;
1175 }
1176 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1177 return 0;
1178 }
1179 break;
1180 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 case Expr_kind:
1182 VISIT(st, expr, s->v.Expr.value);
1183 break;
1184 case Pass_kind:
1185 case Break_kind:
1186 case Continue_kind:
1187 /* nothing to do here */
1188 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001189 case With_kind:
1190 if (!symtable_new_tmpname(st))
1191 return 0;
1192 VISIT(st, expr, s->v.With.context_expr);
1193 if (s->v.With.optional_vars) {
1194 if (!symtable_new_tmpname(st))
1195 return 0;
1196 VISIT(st, expr, s->v.With.optional_vars);
1197 }
1198 VISIT_SEQ(st, stmt, s->v.With.body);
1199 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 }
1201 return 1;
1202}
1203
1204static int
1205symtable_visit_expr(struct symtable *st, expr_ty e)
1206{
1207 switch (e->kind) {
1208 case BoolOp_kind:
1209 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1210 break;
1211 case BinOp_kind:
1212 VISIT(st, expr, e->v.BinOp.left);
1213 VISIT(st, expr, e->v.BinOp.right);
1214 break;
1215 case UnaryOp_kind:
1216 VISIT(st, expr, e->v.UnaryOp.operand);
1217 break;
1218 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001219 if (!GET_IDENTIFIER(lambda) ||
1220 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 return 0;
1222 if (e->v.Lambda.args->defaults)
1223 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1224 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001225 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 FunctionBlock, (void *)e, 0))
1227 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001228 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1229 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 if (!symtable_exit_block(st, (void *)e))
1231 return 0;
1232 break;
1233 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001234 case IfExp_kind:
1235 VISIT(st, expr, e->v.IfExp.test);
1236 VISIT(st, expr, e->v.IfExp.body);
1237 VISIT(st, expr, e->v.IfExp.orelse);
1238 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239 case Dict_kind:
1240 VISIT_SEQ(st, expr, e->v.Dict.keys);
1241 VISIT_SEQ(st, expr, e->v.Dict.values);
1242 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001243 case Set_kind:
1244 VISIT_SEQ(st, expr, e->v.Set.elts);
1245 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001246 case GeneratorExp_kind:
1247 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001250 case ListComp_kind:
1251 if (!symtable_visit_listcomp(st, e))
1252 return 0;
1253 break;
1254 case SetComp_kind:
1255 if (!symtable_visit_setcomp(st, e))
1256 return 0;
1257 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 case Yield_kind:
1259 if (e->v.Yield.value)
1260 VISIT(st, expr, e->v.Yield.value);
1261 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001262 if (st->st_cur->ste_returns_value) {
1263 PyErr_SetString(PyExc_SyntaxError,
1264 RETURN_VAL_IN_GENERATOR);
1265 PyErr_SyntaxLocation(st->st_filename,
1266 e->lineno);
1267 return 0;
1268 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 break;
1270 case Compare_kind:
1271 VISIT(st, expr, e->v.Compare.left);
1272 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1273 break;
1274 case Call_kind:
1275 VISIT(st, expr, e->v.Call.func);
1276 VISIT_SEQ(st, expr, e->v.Call.args);
1277 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1278 if (e->v.Call.starargs)
1279 VISIT(st, expr, e->v.Call.starargs);
1280 if (e->v.Call.kwargs)
1281 VISIT(st, expr, e->v.Call.kwargs);
1282 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 case Num_kind:
1284 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001285 case Bytes_kind:
1286 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 /* Nothing to do here. */
1288 break;
1289 /* The following exprs can be assignment targets. */
1290 case Attribute_kind:
1291 VISIT(st, expr, e->v.Attribute.value);
1292 break;
1293 case Subscript_kind:
1294 VISIT(st, expr, e->v.Subscript.value);
1295 VISIT(st, slice, e->v.Subscript.slice);
1296 break;
1297 case Name_kind:
1298 if (!symtable_add_def(st, e->v.Name.id,
1299 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1300 return 0;
1301 break;
1302 /* child nodes of List and Tuple will have expr_context set */
1303 case List_kind:
1304 VISIT_SEQ(st, expr, e->v.List.elts);
1305 break;
1306 case Tuple_kind:
1307 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1308 break;
1309 }
1310 return 1;
1311}
1312
1313static int
1314symtable_implicit_arg(struct symtable *st, int pos)
1315{
1316 PyObject *id = PyString_FromFormat(".%d", pos);
1317 if (id == NULL)
1318 return 0;
1319 if (!symtable_add_def(st, id, DEF_PARAM)) {
1320 Py_DECREF(id);
1321 return 0;
1322 }
1323 Py_DECREF(id);
1324 return 1;
1325}
1326
1327static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001328symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel,
1329 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001331 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001332
1333 if (!args)
1334 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335
1336 /* go through all the toplevel arguments first */
1337 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001338 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1339 if (arg->kind == SimpleArg_kind) {
1340 if (!annotations) {
1341 if (!symtable_add_def(st,
1342 arg->v.SimpleArg.arg,
1343 DEF_PARAM))
1344 return 0;
1345 }
1346 else if (arg->v.SimpleArg.annotation)
1347 VISIT(st, expr, arg->v.SimpleArg.annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001349 else if (arg->kind == NestedArgs_kind) {
1350 if (toplevel && !annotations) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 if (!symtable_implicit_arg(st, i))
1352 return 0;
1353 }
1354 }
1355 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001356 PyErr_SetString(PyExc_SyntaxError,
1357 "invalid expression in parameter list");
1358 PyErr_SyntaxLocation(st->st_filename,
1359 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 return 0;
1361 }
1362 }
1363
1364 if (!toplevel) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001365 if (!symtable_visit_params_nested(st, args, annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366 return 0;
1367 }
1368
1369 return 1;
1370}
1371
1372static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001373symtable_visit_params_nested(struct symtable *st, asdl_seq *args,
1374 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375{
1376 int i;
1377 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001378 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1379 if (arg->kind == NestedArgs_kind &&
1380 !symtable_visit_params(st, arg->v.NestedArgs.args, 0,
1381 annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382 return 0;
1383 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001384
1385 return 1;
1386}
1387
1388
1389static int
1390symtable_visit_annotations(struct symtable *st, stmt_ty s)
1391{
1392 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393
Neal Norwitzc1505362006-12-28 06:47:50 +00001394 if (a->args && !symtable_visit_params(st, a->args, 1, 1))
1395 return 0;
1396 if (a->varargannotation)
1397 VISIT(st, expr, a->varargannotation);
1398 if (a->kwargannotation)
1399 VISIT(st, expr, a->kwargannotation);
1400 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 1))
1401 return 0;
1402 if (s->v.FunctionDef.returns)
1403 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 return 1;
1405}
1406
1407static int
1408symtable_visit_arguments(struct symtable *st, arguments_ty a)
1409{
1410 /* skip default arguments inside function block
1411 XXX should ast be different?
1412 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001413 if (a->args && !symtable_visit_params(st, a->args, 1, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001415 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 0))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001416 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 if (a->vararg) {
1418 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1419 return 0;
1420 st->st_cur->ste_varargs = 1;
1421 }
1422 if (a->kwarg) {
1423 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1424 return 0;
1425 st->st_cur->ste_varkeywords = 1;
1426 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001427 if (a->args && !symtable_visit_params_nested(st, a->args, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 return 0;
1429 return 1;
1430}
1431
1432
1433static int
1434symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1435{
1436 if (eh->type)
1437 VISIT(st, expr, eh->type);
1438 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001439 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1440 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441 VISIT_SEQ(st, stmt, eh->body);
1442 return 1;
1443}
1444
1445
1446static int
1447symtable_visit_alias(struct symtable *st, alias_ty a)
1448{
1449 /* Compute store_name, the name actually bound by the import
1450 operation. It is diferent than a->name when a->name is a
1451 dotted package name (e.g. spam.eggs)
1452 */
1453 PyObject *store_name;
1454 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1455 const char *base = PyString_AS_STRING(name);
1456 char *dot = strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001457 if (dot) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 store_name = PyString_FromStringAndSize(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001459 if (!store_name)
1460 return 0;
1461 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 else {
1463 store_name = name;
1464 Py_INCREF(store_name);
1465 }
1466 if (strcmp(PyString_AS_STRING(name), "*")) {
1467 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1468 Py_DECREF(store_name);
1469 return r;
1470 }
1471 else {
1472 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001473 int lineno = st->st_cur->ste_lineno;
1474 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001475 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001477 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 }
1479 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001480 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 return 1;
1482 }
1483}
1484
1485
1486static int
1487symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1488{
1489 VISIT(st, expr, lc->target);
1490 VISIT(st, expr, lc->iter);
1491 VISIT_SEQ(st, expr, lc->ifs);
1492 return 1;
1493}
1494
1495
1496static int
1497symtable_visit_keyword(struct symtable *st, keyword_ty k)
1498{
1499 VISIT(st, expr, k->value);
1500 return 1;
1501}
1502
1503
1504static int
1505symtable_visit_slice(struct symtable *st, slice_ty s)
1506{
1507 switch (s->kind) {
1508 case Slice_kind:
1509 if (s->v.Slice.lower)
1510 VISIT(st, expr, s->v.Slice.lower)
1511 if (s->v.Slice.upper)
1512 VISIT(st, expr, s->v.Slice.upper)
1513 if (s->v.Slice.step)
1514 VISIT(st, expr, s->v.Slice.step)
1515 break;
1516 case ExtSlice_kind:
1517 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1518 break;
1519 case Index_kind:
1520 VISIT(st, expr, s->v.Index.value)
1521 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 }
1523 return 1;
1524}
1525
1526static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001527symtable_handle_comprehension(struct symtable *st, expr_ty e,
1528 identifier scope_name,
1529 asdl_seq *generators, expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001531 int is_generator = (e->kind == GeneratorExp_kind);
1532 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001534 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535 /* Outermost iterator is evaluated in current scope */
1536 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001537 /* Create comprehension scope for the rest */
1538 if (!scope_name ||
1539 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 return 0;
1541 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001542 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 /* Outermost iter is received as an argument */
1544 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001545 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 return 0;
1547 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001548 /* Allocate temporary name if needed */
1549 if (needs_tmp && !symtable_new_tmpname(st)) {
1550 symtable_exit_block(st, (void *)e);
1551 return 0;
1552 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001553 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1554 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1555 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001556 generators, 1, (void*)e);
1557 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001558 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001560
1561static int
1562symtable_visit_genexp(struct symtable *st, expr_ty e)
1563{
1564 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1565 e->v.GeneratorExp.generators,
1566 e->v.GeneratorExp.elt);
1567}
1568
1569static int
1570symtable_visit_listcomp(struct symtable *st, expr_ty e)
1571{
1572 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1573 e->v.ListComp.generators,
1574 e->v.ListComp.elt);
1575}
1576
1577static int
1578symtable_visit_setcomp(struct symtable *st, expr_ty e)
1579{
1580 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1581 e->v.SetComp.generators,
1582 e->v.SetComp.elt);
1583}