blob: d275cb90343bc46ad7f024d4cd6626f60037d821 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
3#include "code.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005#include "structmember.h"
6
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#define GLOBAL_AFTER_ASSIGN \
9"name '%.400s' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000010
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#define GLOBAL_AFTER_USE \
12"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000013
Neal Norwitz5d0ad502005-12-19 04:27:42 +000014#define IMPORT_STAR_WARNING "import * only allowed at module level"
15
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000016#define RETURN_VAL_IN_GENERATOR \
17 "'return' with argument inside generator"
Neal Norwitz5d0ad502005-12-19 04:27:42 +000018
Neal Norwitz090b3dd2006-02-28 22:36:46 +000019/* XXX(nnorwitz): change name since static? */
20static PySTEntryObject *
Neal Norwitz62c2fac2005-10-24 00:30:44 +000021PySTEntry_New(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000023{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024 PySTEntryObject *ste = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000025 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027 k = PyLong_FromVoidPtr(key);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000028 if (k == NULL)
29 goto fail;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000030 ste = (PySTEntryObject *)PyObject_New(PySTEntryObject,
31 &PySTEntry_Type);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000032 ste->ste_table = st;
33 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000035
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036 ste->ste_name = name;
37 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000038
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000039 ste->ste_symbols = NULL;
40 ste->ste_varnames = NULL;
41 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000043 ste->ste_symbols = PyDict_New();
44 if (ste->ste_symbols == NULL)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000045 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000046
47 ste->ste_varnames = PyList_New(0);
48 if (ste->ste_varnames == NULL)
49 goto fail;
50
51 ste->ste_children = PyList_New(0);
52 if (ste->ste_children == NULL)
53 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000055 ste->ste_type = block;
56 ste->ste_unoptimized = 0;
57 ste->ste_nested = 0;
58 ste->ste_free = 0;
59 ste->ste_varargs = 0;
60 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000061 ste->ste_opt_lineno = 0;
Jeremy Hylton4d508ad2003-05-21 17:34:50 +000062 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000063 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000065 if (st->st_cur != NULL &&
66 (st->st_cur->ste_nested ||
67 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000068 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000070 ste->ste_generator = 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000071 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000072
73 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
74 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000075
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000076 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000077 fail:
78 Py_XDECREF(ste);
79 return NULL;
80}
81
82static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084{
85 char buf[256];
86
Barry Warsaw4b4ab202001-11-28 21:36:28 +000087 PyOS_snprintf(buf, sizeof(buf),
88 "<symtable entry %.100s(%ld), line %d>",
89 PyString_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000090 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000091 return PyString_FromString(buf);
92}
93
94static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096{
97 ste->ste_table = NULL;
98 Py_XDECREF(ste->ste_id);
99 Py_XDECREF(ste->ste_name);
100 Py_XDECREF(ste->ste_symbols);
101 Py_XDECREF(ste->ste_varnames);
102 Py_XDECREF(ste->ste_children);
103 PyObject_Del(ste);
104}
105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000107
Guido van Rossum6f799372001-09-20 20:46:19 +0000108static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109 {"id", T_OBJECT, OFF(ste_id), READONLY},
110 {"name", T_OBJECT, OFF(ste_name), READONLY},
111 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
112 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
113 {"children", T_OBJECT, OFF(ste_children), READONLY},
114 {"type", T_INT, OFF(ste_type), READONLY},
115 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000116 {NULL}
117};
118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119PyTypeObject PySTEntry_Type = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120 PyObject_HEAD_INIT(&PyType_Type)
121 0,
122 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124 0,
125 (destructor)ste_dealloc, /* tp_dealloc */
126 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000127 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000128 0, /* tp_setattr */
129 0, /* tp_compare */
130 (reprfunc)ste_repr, /* tp_repr */
131 0, /* tp_as_number */
132 0, /* tp_as_sequence */
133 0, /* tp_as_mapping */
134 0, /* tp_hash */
135 0, /* tp_call */
136 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000137 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000138 0, /* tp_setattro */
139 0, /* tp_as_buffer */
140 Py_TPFLAGS_DEFAULT, /* tp_flags */
141 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000142 0, /* tp_traverse */
143 0, /* tp_clear */
144 0, /* tp_richcompare */
145 0, /* tp_weaklistoffset */
146 0, /* tp_iter */
147 0, /* tp_iternext */
148 0, /* tp_methods */
149 ste_memberlist, /* tp_members */
150 0, /* tp_getset */
151 0, /* tp_base */
152 0, /* tp_dict */
153 0, /* tp_descr_get */
154 0, /* tp_descr_set */
155 0, /* tp_dictoffset */
156 0, /* tp_init */
157 0, /* tp_alloc */
158 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000159};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160
161static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000162static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000164 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165static int symtable_exit_block(struct symtable *st, void *ast);
166static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
167static int symtable_visit_expr(struct symtable *st, expr_ty s);
168static int symtable_visit_genexp(struct symtable *st, expr_ty s);
169static int symtable_visit_arguments(struct symtable *st, arguments_ty);
170static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
171static int symtable_visit_alias(struct symtable *st, alias_ty);
172static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
173static int symtable_visit_keyword(struct symtable *st, keyword_ty);
174static int symtable_visit_slice(struct symtable *st, slice_ty);
Neal Norwitzc1505362006-12-28 06:47:50 +0000175static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top,
176 int annotations);
177static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args,
178 int annotations);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000180static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181
182
Nick Coghlan99b25332005-11-16 12:45:24 +0000183static identifier top = NULL, lambda = NULL, genexpr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184
185#define GET_IDENTIFIER(VAR) \
186 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
187
188#define DUPLICATE_ARGUMENT \
189"duplicate argument '%s' in function definition"
190
191static struct symtable *
192symtable_new(void)
193{
194 struct symtable *st;
195
196 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
197 if (st == NULL)
198 return NULL;
199
200 st->st_filename = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000201 st->st_symbols = NULL;
202
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203 if ((st->st_stack = PyList_New(0)) == NULL)
204 goto fail;
205 if ((st->st_symbols = PyDict_New()) == NULL)
206 goto fail;
207 st->st_cur = NULL;
208 st->st_tmpname = 0;
209 st->st_private = NULL;
210 return st;
211 fail:
212 PySymtable_Free(st);
213 return NULL;
214}
215
216struct symtable *
217PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
218{
219 struct symtable *st = symtable_new();
220 asdl_seq *seq;
221 int i;
222
223 if (st == NULL)
224 return st;
225 st->st_filename = filename;
226 st->st_future = future;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000227 if (!GET_IDENTIFIER(top) ||
228 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000229 PySymtable_Free(st);
230 return NULL;
231 }
232
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 st->st_top = st->st_cur;
234 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
235 /* Any other top-level initialization? */
236 switch (mod->kind) {
237 case Module_kind:
238 seq = mod->v.Module.body;
239 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240 if (!symtable_visit_stmt(st,
241 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242 goto error;
243 break;
244 case Expression_kind:
245 if (!symtable_visit_expr(st, mod->v.Expression.body))
246 goto error;
247 break;
248 case Interactive_kind:
249 seq = mod->v.Interactive.body;
250 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000251 if (!symtable_visit_stmt(st,
252 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253 goto error;
254 break;
255 case Suite_kind:
256 PyErr_SetString(PyExc_RuntimeError,
257 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000258 goto error;
259 }
260 if (!symtable_exit_block(st, (void *)mod)) {
261 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262 return NULL;
263 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 if (symtable_analyze(st))
265 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000266 PySymtable_Free(st);
267 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000269 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 PySymtable_Free(st);
271 return NULL;
272}
273
274void
275PySymtable_Free(struct symtable *st)
276{
277 Py_XDECREF(st->st_symbols);
278 Py_XDECREF(st->st_stack);
279 PyMem_Free((void *)st);
280}
281
282PySTEntryObject *
283PySymtable_Lookup(struct symtable *st, void *key)
284{
285 PyObject *k, *v;
286
287 k = PyLong_FromVoidPtr(key);
288 if (k == NULL)
289 return NULL;
290 v = PyDict_GetItem(st->st_symbols, k);
291 if (v) {
292 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294 }
295 else {
296 PyErr_SetString(PyExc_KeyError,
297 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000299
300 Py_DECREF(k);
301 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302}
303
304int
305PyST_GetScope(PySTEntryObject *ste, PyObject *name)
306{
307 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
308 if (!v)
309 return 0;
310 assert(PyInt_Check(v));
311 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
312}
313
314
315/* Analyze raw symbol information to determine scope of each name.
316
317 The next several functions are helpers for PySymtable_Analyze(),
318 which determines whether a name is local, global, or free. In addition,
319 it determines which local variables are cell variables; they provide
320 bindings that are used for free variables in enclosed blocks.
321
322 There are also two kinds of free variables, implicit and explicit. An
323 explicit global is declared with the global statement. An implicit
324 global is a free variable for which the compiler has found no binding
325 in an enclosing function scope. The implicit global is either a global
326 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
327 to handle these names to implement slightly odd semantics. In such a
328 block, the name is treated as global until it is assigned to; then it
329 is treated as a local.
330
331 The symbol table requires two passes to determine the scope of each name.
332 The first pass collects raw facts from the AST: the name is a parameter
333 here, the name is used by not defined here, etc. The second pass analyzes
334 these facts during a pass over the PySTEntryObjects created during pass 1.
335
336 When a function is entered during the second pass, the parent passes
337 the set of all name bindings visible to its children. These bindings
338 are used to determine if the variable is free or an implicit global.
339 After doing the local analysis, it analyzes each of its child blocks
340 using an updated set of name bindings.
341
342 The children update the free variable set. If a local variable is free
343 in a child, the variable is marked as a cell. The current function must
344 provide runtime storage for the variable that may outlive the function's
345 frame. Cell variables are removed from the free set before the analyze
346 function returns to its parent.
347
348 The sets of bound and free variables are implemented as dictionaries
349 mapping strings to None.
350*/
351
352#define SET_SCOPE(DICT, NAME, I) { \
353 PyObject *o = PyInt_FromLong(I); \
354 if (!o) \
355 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000356 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
357 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000359 } \
360 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361}
362
363/* Decide on scope of name, given flags.
364
365 The dicts passed in as arguments are modified as necessary.
366 ste is passed so that flags can be updated.
367*/
368
369static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000370analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371 PyObject *bound, PyObject *local, PyObject *free,
372 PyObject *global)
373{
374 if (flags & DEF_GLOBAL) {
375 if (flags & DEF_PARAM) {
376 PyErr_Format(PyExc_SyntaxError,
377 "name '%s' is local and global",
378 PyString_AS_STRING(name));
379 return 0;
380 }
381 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
382 if (PyDict_SetItem(global, name, Py_None) < 0)
383 return 0;
384 if (bound && PyDict_GetItem(bound, name)) {
385 if (PyDict_DelItem(bound, name) < 0)
386 return 0;
387 }
388 return 1;
389 }
390 if (flags & DEF_BOUND) {
391 SET_SCOPE(dict, name, LOCAL);
392 if (PyDict_SetItem(local, name, Py_None) < 0)
393 return 0;
394 if (PyDict_GetItem(global, name)) {
395 if (PyDict_DelItem(global, name) < 0)
396 return 0;
397 }
398 return 1;
399 }
400 /* If an enclosing block has a binding for this name, it
401 is a free variable rather than a global variable.
402 Note that having a non-NULL bound implies that the block
403 is nested.
404 */
405 if (bound && PyDict_GetItem(bound, name)) {
406 SET_SCOPE(dict, name, FREE);
407 ste->ste_free = 1;
408 if (PyDict_SetItem(free, name, Py_None) < 0)
409 return 0;
410 return 1;
411 }
412 /* If a parent has a global statement, then call it global
413 explicit? It could also be global implicit.
414 */
415 else if (global && PyDict_GetItem(global, name)) {
416 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
417 return 1;
418 }
419 else {
420 if (ste->ste_nested)
421 ste->ste_free = 1;
422 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
423 return 1;
424 }
425 return 0; /* Can't get here */
426}
427
428#undef SET_SCOPE
429
430/* If a name is defined in free and also in locals, then this block
431 provides the binding for the free variable. The name should be
432 marked CELL in this block and removed from the free list.
433
434 Note that the current block's free variables are included in free.
435 That's safe because no name can be free and local in the same scope.
436*/
437
438static int
439analyze_cells(PyObject *scope, PyObject *free)
440{
441 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000442 int success = 0;
443 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444
445 w = PyInt_FromLong(CELL);
446 if (!w)
447 return 0;
448 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000449 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000451 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 if (flags != LOCAL)
453 continue;
454 if (!PyDict_GetItem(free, name))
455 continue;
456 /* Replace LOCAL with CELL for this name, and remove
457 from free. It is safe to replace the value of name
458 in the dict, because it will not cause a resize.
459 */
460 if (PyDict_SetItem(scope, name, w) < 0)
461 goto error;
462 if (!PyDict_DelItem(free, name) < 0)
463 goto error;
464 }
465 success = 1;
466 error:
467 Py_DECREF(w);
468 return success;
469}
470
471/* Check for illegal statements in unoptimized namespaces */
472static int
473check_unoptimized(const PySTEntryObject* ste) {
474 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000475 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000477 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478 || !(ste->ste_free || ste->ste_child_free))
479 return 1;
480
Armin Rigo31441302005-10-21 12:57:31 +0000481 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 "contains a nested function with free variables" :
483 "is a nested function");
484
485 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000486 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 return 1;
488 case OPT_IMPORT_STAR:
489 PyOS_snprintf(buf, sizeof(buf),
490 "import * is not allowed in function '%.100s' "
491 "because it is %s",
492 PyString_AS_STRING(ste->ste_name), trailer);
493 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494 }
495
496 PyErr_SetString(PyExc_SyntaxError, buf);
497 PyErr_SyntaxLocation(ste->ste_table->st_filename,
498 ste->ste_opt_lineno);
499 return 0;
500}
501
502/* Enter the final scope information into the st_symbols dict.
503 *
504 * All arguments are dicts. Modifies symbols, others are read-only.
505*/
506static int
507update_symbols(PyObject *symbols, PyObject *scope,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509{
510 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000511 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512
513 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000514 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 assert(PyInt_Check(v));
516 flags = PyInt_AS_LONG(v);
517 w = PyDict_GetItem(scope, name);
518 assert(w && PyInt_Check(w));
519 i = PyInt_AS_LONG(w);
520 flags |= (i << SCOPE_OFF);
521 u = PyInt_FromLong(flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000522 if (!u)
523 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 if (PyDict_SetItem(symbols, name, u) < 0) {
525 Py_DECREF(u);
526 return 0;
527 }
528 Py_DECREF(u);
529 }
530
531 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
532 if (!free_value)
533 return 0;
534
535 /* add a free variable when it's only use is for creating a closure */
536 pos = 0;
537 while (PyDict_Next(free, &pos, &name, &v)) {
538 PyObject *o = PyDict_GetItem(symbols, name);
539
540 if (o) {
541 /* It could be a free variable in a method of
542 the class that has the same name as a local
543 or global in the class scope.
544 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000545 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000547 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548 o = PyInt_FromLong(i);
549 if (!o) {
550 Py_DECREF(free_value);
551 return 0;
552 }
553 if (PyDict_SetItem(symbols, name, o) < 0) {
554 Py_DECREF(o);
555 Py_DECREF(free_value);
556 return 0;
557 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000558 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559 }
560 /* else it's not free, probably a cell */
561 continue;
562 }
563 if (!PyDict_GetItem(bound, name))
564 continue; /* it's a global */
565
566 if (PyDict_SetItem(symbols, name, free_value) < 0) {
567 Py_DECREF(free_value);
568 return 0;
569 }
570 }
571 Py_DECREF(free_value);
572 return 1;
573}
574
575/* Make final symbol table decisions for block of ste.
576 Arguments:
577 ste -- current symtable entry (input/output)
578 bound -- set of variables bound in enclosing scopes (input)
579 free -- set of free variables in enclosed scopes (output)
580 globals -- set of declared global variables in enclosing scopes (input)
581*/
582
583static int
584analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
585 PyObject *global)
586{
587 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
588 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000589 int i, success = 0;
590 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591
592 local = PyDict_New();
593 if (!local)
594 goto error;
595 scope = PyDict_New();
596 if (!scope)
597 goto error;
598 newglobal = PyDict_New();
599 if (!newglobal)
600 goto error;
601 newfree = PyDict_New();
602 if (!newfree)
603 goto error;
604 newbound = PyDict_New();
605 if (!newbound)
606 goto error;
607
608 if (ste->ste_type == ClassBlock) {
609 /* make a copy of globals before calling analyze_name(),
610 because global statements in the class have no effect
611 on nested functions.
612 */
613 if (PyDict_Update(newglobal, global) < 0)
614 goto error;
615 if (bound)
616 if (PyDict_Update(newbound, bound) < 0)
617 goto error;
618 }
619
620 assert(PySTEntry_Check(ste));
621 assert(PyDict_Check(ste->ste_symbols));
622 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000623 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 if (!analyze_name(ste, scope, name, flags, bound, local, free,
625 global))
626 goto error;
627 }
628
629 if (ste->ste_type != ClassBlock) {
630 if (ste->ste_type == FunctionBlock) {
631 if (PyDict_Update(newbound, local) < 0)
632 goto error;
633 }
634 if (bound) {
635 if (PyDict_Update(newbound, bound) < 0)
636 goto error;
637 }
638 if (PyDict_Update(newglobal, global) < 0)
639 goto error;
640 }
641
642 /* Recursively call analyze_block() on each child block */
643 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
644 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000645 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000647 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 if (!analyze_block(entry, newbound, newfree, newglobal))
649 goto error;
650 if (entry->ste_free || entry->ste_child_free)
651 ste->ste_child_free = 1;
652 }
653
654 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
655 goto error;
656 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
657 ste->ste_type == ClassBlock))
658 goto error;
659 if (!check_unoptimized(ste))
660 goto error;
661
662 if (PyDict_Update(free, newfree) < 0)
663 goto error;
664 success = 1;
665 error:
666 Py_XDECREF(local);
667 Py_XDECREF(scope);
668 Py_XDECREF(newbound);
669 Py_XDECREF(newglobal);
670 Py_XDECREF(newfree);
671 if (!success)
672 assert(PyErr_Occurred());
673 return success;
674}
675
676static int
677symtable_analyze(struct symtable *st)
678{
679 PyObject *free, *global;
680 int r;
681
682 free = PyDict_New();
683 if (!free)
684 return 0;
685 global = PyDict_New();
686 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000687 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 return 0;
689 }
690 r = analyze_block(st->st_top, NULL, free, global);
691 Py_DECREF(free);
692 Py_DECREF(global);
693 return r;
694}
695
696
697static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000698symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699{
700 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000701 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
703 PyErr_SetString(PyExc_SyntaxError, msg);
704 PyErr_SyntaxLocation(st->st_filename,
705 st->st_cur->ste_lineno);
706 }
707 return 0;
708 }
709 return 1;
710}
711
712/* symtable_enter_block() gets a reference via PySTEntry_New().
713 This reference is released when the block is exited, via the DECREF
714 in symtable_exit_block().
715*/
716
717static int
718symtable_exit_block(struct symtable *st, void *ast)
719{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000720 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000722 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 end = PyList_GET_SIZE(st->st_stack) - 1;
724 if (end >= 0) {
725 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
726 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000727 if (st->st_cur == NULL)
728 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 Py_INCREF(st->st_cur);
730 if (PySequence_DelItem(st->st_stack, end) < 0)
731 return 0;
732 }
733 return 1;
734}
735
736static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000737symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 void *ast, int lineno)
739{
740 PySTEntryObject *prev = NULL;
741
742 if (st->st_cur) {
743 prev = st->st_cur;
744 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 return 0;
746 }
747 Py_DECREF(st->st_cur);
748 }
749 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000750 if (st->st_cur == NULL)
751 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 if (name == GET_IDENTIFIER(top))
753 st->st_global = st->st_cur->ste_symbols;
754 if (prev) {
755 if (PyList_Append(prev->ste_children,
756 (PyObject *)st->st_cur) < 0) {
757 return 0;
758 }
759 }
760 return 1;
761}
762
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000763static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764symtable_lookup(struct symtable *st, PyObject *name)
765{
766 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000767 PyObject *mangled = _Py_Mangle(st->st_private, name);
768 if (!mangled)
769 return 0;
770 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
771 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 if (!o)
773 return 0;
774 return PyInt_AsLong(o);
775}
776
777static int
778symtable_add_def(struct symtable *st, PyObject *name, int flag)
779{
780 PyObject *o;
781 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000782 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000783 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000785 if (!mangled)
786 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000788 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 val = PyInt_AS_LONG(o);
790 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000791 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
793 PyString_AsString(name));
794 PyErr_SyntaxLocation(st->st_filename,
795 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000796 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 }
798 val |= flag;
799 } else
800 val = flag;
801 o = PyInt_FromLong(val);
802 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000803 goto error;
804 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000806 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808 Py_DECREF(o);
809
810 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000811 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
812 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 } else if (flag & DEF_GLOBAL) {
814 /* XXX need to update DEF_GLOBAL for other flags too;
815 perhaps only DEF_FREE_GLOBAL */
816 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000817 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 val |= PyInt_AS_LONG(o);
819 }
820 o = PyInt_FromLong(val);
821 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000822 goto error;
823 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000825 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 }
827 Py_DECREF(o);
828 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000829 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000831
832error:
833 Py_DECREF(mangled);
834 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835}
836
837/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
838 They use the ASDL name to synthesize the name of the C type and the visit
839 function.
840
841 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
842 useful if the first node in the sequence requires special treatment.
843*/
844
845#define VISIT(ST, TYPE, V) \
846 if (!symtable_visit_ ## TYPE((ST), (V))) \
847 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000848
849#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
850 if (!symtable_visit_ ## TYPE((ST), (V))) { \
851 symtable_exit_block((ST), (S)); \
852 return 0; \
853 }
854
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855#define VISIT_SEQ(ST, TYPE, SEQ) { \
856 int i; \
857 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
858 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000859 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 if (!symtable_visit_ ## TYPE((ST), elt)) \
861 return 0; \
862 } \
863}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000864
865#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
866 int i; \
867 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
868 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000869 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000870 if (!symtable_visit_ ## TYPE((ST), elt)) { \
871 symtable_exit_block((ST), (S)); \
872 return 0; \
873 } \
874 } \
875}
876
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
878 int i; \
879 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
880 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000881 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 if (!symtable_visit_ ## TYPE((ST), elt)) \
883 return 0; \
884 } \
885}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000886
887#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
888 int i; \
889 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
890 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000891 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000892 if (!symtable_visit_ ## TYPE((ST), elt)) { \
893 symtable_exit_block((ST), (S)); \
894 return 0; \
895 } \
896 } \
897}
898
Guido van Rossum4f72a782006-10-27 23:31:49 +0000899#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
900 int i = 0; \
901 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
902 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
903 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
904 if (!elt) continue; /* can be NULL */ \
905 if (!symtable_visit_expr((ST), elt)) \
906 return 0; \
907 } \
908}
909
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000911symtable_new_tmpname(struct symtable *st)
912{
913 char tmpname[256];
914 identifier tmp;
915
916 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
917 ++st->st_cur->ste_tmpname);
918 tmp = PyString_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000919 if (!tmp)
920 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000921 if (!symtable_add_def(st, tmp, DEF_LOCAL))
922 return 0;
923 Py_DECREF(tmp);
924 return 1;
925}
926
Guido van Rossum4f72a782006-10-27 23:31:49 +0000927
928
Guido van Rossumc2e20742006-02-27 22:32:47 +0000929static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930symtable_visit_stmt(struct symtable *st, stmt_ty s)
931{
932 switch (s->kind) {
933 case FunctionDef_kind:
934 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
935 return 0;
936 if (s->v.FunctionDef.args->defaults)
937 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000938 if (s->v.FunctionDef.args->kw_defaults)
939 VISIT_KWONLYDEFAULTS(st,
940 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +0000941 if (!symtable_visit_annotations(st, s))
942 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 if (s->v.FunctionDef.decorators)
944 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
945 if (!symtable_enter_block(st, s->v.FunctionDef.name,
946 FunctionBlock, (void *)s, s->lineno))
947 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000948 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
949 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950 if (!symtable_exit_block(st, s))
951 return 0;
952 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000953 case ClassDef_kind: {
954 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
956 return 0;
957 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
958 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
959 (void *)s, s->lineno))
960 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000961 tmp = st->st_private;
962 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000963 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000964 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 if (!symtable_exit_block(st, s))
966 return 0;
967 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000968 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000970 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000972 st->st_cur->ste_returns_value = 1;
973 if (st->st_cur->ste_generator) {
974 PyErr_SetString(PyExc_SyntaxError,
975 RETURN_VAL_IN_GENERATOR);
976 PyErr_SyntaxLocation(st->st_filename,
977 s->lineno);
978 return 0;
979 }
980 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 break;
982 case Delete_kind:
983 VISIT_SEQ(st, expr, s->v.Delete.targets);
984 break;
985 case Assign_kind:
986 VISIT_SEQ(st, expr, s->v.Assign.targets);
987 VISIT(st, expr, s->v.Assign.value);
988 break;
989 case AugAssign_kind:
990 VISIT(st, expr, s->v.AugAssign.target);
991 VISIT(st, expr, s->v.AugAssign.value);
992 break;
993 case Print_kind:
994 if (s->v.Print.dest)
995 VISIT(st, expr, s->v.Print.dest);
996 VISIT_SEQ(st, expr, s->v.Print.values);
997 break;
998 case For_kind:
999 VISIT(st, expr, s->v.For.target);
1000 VISIT(st, expr, s->v.For.iter);
1001 VISIT_SEQ(st, stmt, s->v.For.body);
1002 if (s->v.For.orelse)
1003 VISIT_SEQ(st, stmt, s->v.For.orelse);
1004 break;
1005 case While_kind:
1006 VISIT(st, expr, s->v.While.test);
1007 VISIT_SEQ(st, stmt, s->v.While.body);
1008 if (s->v.While.orelse)
1009 VISIT_SEQ(st, stmt, s->v.While.orelse);
1010 break;
1011 case If_kind:
1012 /* XXX if 0: and lookup_yield() hacks */
1013 VISIT(st, expr, s->v.If.test);
1014 VISIT_SEQ(st, stmt, s->v.If.body);
1015 if (s->v.If.orelse)
1016 VISIT_SEQ(st, stmt, s->v.If.orelse);
1017 break;
1018 case Raise_kind:
1019 if (s->v.Raise.type) {
1020 VISIT(st, expr, s->v.Raise.type);
1021 if (s->v.Raise.inst) {
1022 VISIT(st, expr, s->v.Raise.inst);
1023 if (s->v.Raise.tback)
1024 VISIT(st, expr, s->v.Raise.tback);
1025 }
1026 }
1027 break;
1028 case TryExcept_kind:
1029 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1030 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1031 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1032 break;
1033 case TryFinally_kind:
1034 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1035 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1036 break;
1037 case Assert_kind:
1038 VISIT(st, expr, s->v.Assert.test);
1039 if (s->v.Assert.msg)
1040 VISIT(st, expr, s->v.Assert.msg);
1041 break;
1042 case Import_kind:
1043 VISIT_SEQ(st, alias, s->v.Import.names);
1044 /* XXX Don't have the lineno available inside
1045 visit_alias */
1046 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1047 st->st_cur->ste_opt_lineno = s->lineno;
1048 break;
1049 case ImportFrom_kind:
1050 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1051 /* XXX Don't have the lineno available inside
1052 visit_alias */
1053 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1054 st->st_cur->ste_opt_lineno = s->lineno;
1055 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056 case Global_kind: {
1057 int i;
1058 asdl_seq *seq = s->v.Global.names;
1059 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001060 identifier name = (identifier)asdl_seq_GET(seq, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001062 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 if (cur < 0)
1064 return 0;
1065 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001066 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 if (cur & DEF_LOCAL)
1068 PyOS_snprintf(buf, sizeof(buf),
1069 GLOBAL_AFTER_ASSIGN,
1070 c_name);
1071 else
1072 PyOS_snprintf(buf, sizeof(buf),
1073 GLOBAL_AFTER_USE,
1074 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001075 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 return 0;
1077 }
1078 if (!symtable_add_def(st, name, DEF_GLOBAL))
1079 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 break;
1082 }
1083 case Expr_kind:
1084 VISIT(st, expr, s->v.Expr.value);
1085 break;
1086 case Pass_kind:
1087 case Break_kind:
1088 case Continue_kind:
1089 /* nothing to do here */
1090 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001091 case With_kind:
1092 if (!symtable_new_tmpname(st))
1093 return 0;
1094 VISIT(st, expr, s->v.With.context_expr);
1095 if (s->v.With.optional_vars) {
1096 if (!symtable_new_tmpname(st))
1097 return 0;
1098 VISIT(st, expr, s->v.With.optional_vars);
1099 }
1100 VISIT_SEQ(st, stmt, s->v.With.body);
1101 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 }
1103 return 1;
1104}
1105
1106static int
1107symtable_visit_expr(struct symtable *st, expr_ty e)
1108{
1109 switch (e->kind) {
1110 case BoolOp_kind:
1111 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1112 break;
1113 case BinOp_kind:
1114 VISIT(st, expr, e->v.BinOp.left);
1115 VISIT(st, expr, e->v.BinOp.right);
1116 break;
1117 case UnaryOp_kind:
1118 VISIT(st, expr, e->v.UnaryOp.operand);
1119 break;
1120 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001121 if (!GET_IDENTIFIER(lambda) ||
1122 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 return 0;
1124 if (e->v.Lambda.args->defaults)
1125 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1126 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001127 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 FunctionBlock, (void *)e, 0))
1129 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001130 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1131 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 if (!symtable_exit_block(st, (void *)e))
1133 return 0;
1134 break;
1135 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001136 case IfExp_kind:
1137 VISIT(st, expr, e->v.IfExp.test);
1138 VISIT(st, expr, e->v.IfExp.body);
1139 VISIT(st, expr, e->v.IfExp.orelse);
1140 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 case Dict_kind:
1142 VISIT_SEQ(st, expr, e->v.Dict.keys);
1143 VISIT_SEQ(st, expr, e->v.Dict.values);
1144 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001145 case Set_kind:
1146 VISIT_SEQ(st, expr, e->v.Set.elts);
1147 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001148 case ListComp_kind:
1149 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 return 0;
1151 VISIT(st, expr, e->v.ListComp.elt);
1152 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1153 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001154 case GeneratorExp_kind:
1155 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 case Yield_kind:
1159 if (e->v.Yield.value)
1160 VISIT(st, expr, e->v.Yield.value);
1161 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001162 if (st->st_cur->ste_returns_value) {
1163 PyErr_SetString(PyExc_SyntaxError,
1164 RETURN_VAL_IN_GENERATOR);
1165 PyErr_SyntaxLocation(st->st_filename,
1166 e->lineno);
1167 return 0;
1168 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 break;
1170 case Compare_kind:
1171 VISIT(st, expr, e->v.Compare.left);
1172 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1173 break;
1174 case Call_kind:
1175 VISIT(st, expr, e->v.Call.func);
1176 VISIT_SEQ(st, expr, e->v.Call.args);
1177 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1178 if (e->v.Call.starargs)
1179 VISIT(st, expr, e->v.Call.starargs);
1180 if (e->v.Call.kwargs)
1181 VISIT(st, expr, e->v.Call.kwargs);
1182 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183 case Num_kind:
1184 case Str_kind:
Georg Brandl52318d62006-09-06 07:06:08 +00001185 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 /* Nothing to do here. */
1187 break;
1188 /* The following exprs can be assignment targets. */
1189 case Attribute_kind:
1190 VISIT(st, expr, e->v.Attribute.value);
1191 break;
1192 case Subscript_kind:
1193 VISIT(st, expr, e->v.Subscript.value);
1194 VISIT(st, slice, e->v.Subscript.slice);
1195 break;
1196 case Name_kind:
1197 if (!symtable_add_def(st, e->v.Name.id,
1198 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1199 return 0;
1200 break;
1201 /* child nodes of List and Tuple will have expr_context set */
1202 case List_kind:
1203 VISIT_SEQ(st, expr, e->v.List.elts);
1204 break;
1205 case Tuple_kind:
1206 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1207 break;
1208 }
1209 return 1;
1210}
1211
1212static int
1213symtable_implicit_arg(struct symtable *st, int pos)
1214{
1215 PyObject *id = PyString_FromFormat(".%d", pos);
1216 if (id == NULL)
1217 return 0;
1218 if (!symtable_add_def(st, id, DEF_PARAM)) {
1219 Py_DECREF(id);
1220 return 0;
1221 }
1222 Py_DECREF(id);
1223 return 1;
1224}
1225
1226static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001227symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel,
1228 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001230 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001231
1232 if (!args)
1233 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234
1235 /* go through all the toplevel arguments first */
1236 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001237 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1238 if (arg->kind == SimpleArg_kind) {
1239 if (!annotations) {
1240 if (!symtable_add_def(st,
1241 arg->v.SimpleArg.arg,
1242 DEF_PARAM))
1243 return 0;
1244 }
1245 else if (arg->v.SimpleArg.annotation)
1246 VISIT(st, expr, arg->v.SimpleArg.annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001248 else if (arg->kind == NestedArgs_kind) {
1249 if (toplevel && !annotations) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 if (!symtable_implicit_arg(st, i))
1251 return 0;
1252 }
1253 }
1254 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001255 PyErr_SetString(PyExc_SyntaxError,
1256 "invalid expression in parameter list");
1257 PyErr_SyntaxLocation(st->st_filename,
1258 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 return 0;
1260 }
1261 }
1262
1263 if (!toplevel) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001264 if (!symtable_visit_params_nested(st, args, annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 return 0;
1266 }
1267
1268 return 1;
1269}
1270
1271static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001272symtable_visit_params_nested(struct symtable *st, asdl_seq *args,
1273 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274{
1275 int i;
1276 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001277 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1278 if (arg->kind == NestedArgs_kind &&
1279 !symtable_visit_params(st, arg->v.NestedArgs.args, 0,
1280 annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 return 0;
1282 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001283
1284 return 1;
1285}
1286
1287
1288static int
1289symtable_visit_annotations(struct symtable *st, stmt_ty s)
1290{
1291 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292
Neal Norwitzc1505362006-12-28 06:47:50 +00001293 if (a->args && !symtable_visit_params(st, a->args, 1, 1))
1294 return 0;
1295 if (a->varargannotation)
1296 VISIT(st, expr, a->varargannotation);
1297 if (a->kwargannotation)
1298 VISIT(st, expr, a->kwargannotation);
1299 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 1))
1300 return 0;
1301 if (s->v.FunctionDef.returns)
1302 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 return 1;
1304}
1305
1306static int
1307symtable_visit_arguments(struct symtable *st, arguments_ty a)
1308{
1309 /* skip default arguments inside function block
1310 XXX should ast be different?
1311 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001312 if (a->args && !symtable_visit_params(st, a->args, 1, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001314 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 0))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001315 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 if (a->vararg) {
1317 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1318 return 0;
1319 st->st_cur->ste_varargs = 1;
1320 }
1321 if (a->kwarg) {
1322 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1323 return 0;
1324 st->st_cur->ste_varkeywords = 1;
1325 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001326 if (a->args && !symtable_visit_params_nested(st, a->args, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 return 0;
1328 return 1;
1329}
1330
1331
1332static int
1333symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1334{
1335 if (eh->type)
1336 VISIT(st, expr, eh->type);
1337 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001338 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1339 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 VISIT_SEQ(st, stmt, eh->body);
1341 return 1;
1342}
1343
1344
1345static int
1346symtable_visit_alias(struct symtable *st, alias_ty a)
1347{
1348 /* Compute store_name, the name actually bound by the import
1349 operation. It is diferent than a->name when a->name is a
1350 dotted package name (e.g. spam.eggs)
1351 */
1352 PyObject *store_name;
1353 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1354 const char *base = PyString_AS_STRING(name);
1355 char *dot = strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001356 if (dot) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 store_name = PyString_FromStringAndSize(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001358 if (!store_name)
1359 return 0;
1360 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 else {
1362 store_name = name;
1363 Py_INCREF(store_name);
1364 }
1365 if (strcmp(PyString_AS_STRING(name), "*")) {
1366 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1367 Py_DECREF(store_name);
1368 return r;
1369 }
1370 else {
1371 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001372 int lineno = st->st_cur->ste_lineno;
1373 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001374 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001376 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 }
1378 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001379 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 return 1;
1381 }
1382}
1383
1384
1385static int
1386symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1387{
1388 VISIT(st, expr, lc->target);
1389 VISIT(st, expr, lc->iter);
1390 VISIT_SEQ(st, expr, lc->ifs);
1391 return 1;
1392}
1393
1394
1395static int
1396symtable_visit_keyword(struct symtable *st, keyword_ty k)
1397{
1398 VISIT(st, expr, k->value);
1399 return 1;
1400}
1401
1402
1403static int
1404symtable_visit_slice(struct symtable *st, slice_ty s)
1405{
1406 switch (s->kind) {
1407 case Slice_kind:
1408 if (s->v.Slice.lower)
1409 VISIT(st, expr, s->v.Slice.lower)
1410 if (s->v.Slice.upper)
1411 VISIT(st, expr, s->v.Slice.upper)
1412 if (s->v.Slice.step)
1413 VISIT(st, expr, s->v.Slice.step)
1414 break;
1415 case ExtSlice_kind:
1416 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1417 break;
1418 case Index_kind:
1419 VISIT(st, expr, s->v.Index.value)
1420 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421 }
1422 return 1;
1423}
1424
1425static int
1426symtable_visit_genexp(struct symtable *st, expr_ty e)
1427{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 comprehension_ty outermost = ((comprehension_ty)
1429 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1430 /* Outermost iterator is evaluated in current scope */
1431 VISIT(st, expr, outermost->iter);
1432 /* Create generator scope for the rest */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001433 if (!GET_IDENTIFIER(genexpr) ||
1434 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 return 0;
1436 }
1437 st->st_cur->ste_generator = 1;
1438 /* Outermost iter is received as an argument */
1439 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001440 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441 return 0;
1442 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001443 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1444 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1445 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1446 e->v.GeneratorExp.generators, 1, (void*)e);
1447 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001448 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449}