blob: 5f0290afa1aab5da8df318655d3dbfa502d42856 [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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 case For_kind:
994 VISIT(st, expr, s->v.For.target);
995 VISIT(st, expr, s->v.For.iter);
996 VISIT_SEQ(st, stmt, s->v.For.body);
997 if (s->v.For.orelse)
998 VISIT_SEQ(st, stmt, s->v.For.orelse);
999 break;
1000 case While_kind:
1001 VISIT(st, expr, s->v.While.test);
1002 VISIT_SEQ(st, stmt, s->v.While.body);
1003 if (s->v.While.orelse)
1004 VISIT_SEQ(st, stmt, s->v.While.orelse);
1005 break;
1006 case If_kind:
1007 /* XXX if 0: and lookup_yield() hacks */
1008 VISIT(st, expr, s->v.If.test);
1009 VISIT_SEQ(st, stmt, s->v.If.body);
1010 if (s->v.If.orelse)
1011 VISIT_SEQ(st, stmt, s->v.If.orelse);
1012 break;
1013 case Raise_kind:
1014 if (s->v.Raise.type) {
1015 VISIT(st, expr, s->v.Raise.type);
1016 if (s->v.Raise.inst) {
1017 VISIT(st, expr, s->v.Raise.inst);
1018 if (s->v.Raise.tback)
1019 VISIT(st, expr, s->v.Raise.tback);
1020 }
1021 }
1022 break;
1023 case TryExcept_kind:
1024 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1025 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1026 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1027 break;
1028 case TryFinally_kind:
1029 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1030 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1031 break;
1032 case Assert_kind:
1033 VISIT(st, expr, s->v.Assert.test);
1034 if (s->v.Assert.msg)
1035 VISIT(st, expr, s->v.Assert.msg);
1036 break;
1037 case Import_kind:
1038 VISIT_SEQ(st, alias, s->v.Import.names);
1039 /* XXX Don't have the lineno available inside
1040 visit_alias */
1041 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1042 st->st_cur->ste_opt_lineno = s->lineno;
1043 break;
1044 case ImportFrom_kind:
1045 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1046 /* XXX Don't have the lineno available inside
1047 visit_alias */
1048 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1049 st->st_cur->ste_opt_lineno = s->lineno;
1050 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051 case Global_kind: {
1052 int i;
1053 asdl_seq *seq = s->v.Global.names;
1054 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001055 identifier name = (identifier)asdl_seq_GET(seq, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001057 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058 if (cur < 0)
1059 return 0;
1060 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001061 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 if (cur & DEF_LOCAL)
1063 PyOS_snprintf(buf, sizeof(buf),
1064 GLOBAL_AFTER_ASSIGN,
1065 c_name);
1066 else
1067 PyOS_snprintf(buf, sizeof(buf),
1068 GLOBAL_AFTER_USE,
1069 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001070 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071 return 0;
1072 }
1073 if (!symtable_add_def(st, name, DEF_GLOBAL))
1074 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 break;
1077 }
1078 case Expr_kind:
1079 VISIT(st, expr, s->v.Expr.value);
1080 break;
1081 case Pass_kind:
1082 case Break_kind:
1083 case Continue_kind:
1084 /* nothing to do here */
1085 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001086 case With_kind:
1087 if (!symtable_new_tmpname(st))
1088 return 0;
1089 VISIT(st, expr, s->v.With.context_expr);
1090 if (s->v.With.optional_vars) {
1091 if (!symtable_new_tmpname(st))
1092 return 0;
1093 VISIT(st, expr, s->v.With.optional_vars);
1094 }
1095 VISIT_SEQ(st, stmt, s->v.With.body);
1096 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 }
1098 return 1;
1099}
1100
1101static int
1102symtable_visit_expr(struct symtable *st, expr_ty e)
1103{
1104 switch (e->kind) {
1105 case BoolOp_kind:
1106 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1107 break;
1108 case BinOp_kind:
1109 VISIT(st, expr, e->v.BinOp.left);
1110 VISIT(st, expr, e->v.BinOp.right);
1111 break;
1112 case UnaryOp_kind:
1113 VISIT(st, expr, e->v.UnaryOp.operand);
1114 break;
1115 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001116 if (!GET_IDENTIFIER(lambda) ||
1117 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118 return 0;
1119 if (e->v.Lambda.args->defaults)
1120 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1121 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001122 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 FunctionBlock, (void *)e, 0))
1124 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001125 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1126 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 if (!symtable_exit_block(st, (void *)e))
1128 return 0;
1129 break;
1130 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001131 case IfExp_kind:
1132 VISIT(st, expr, e->v.IfExp.test);
1133 VISIT(st, expr, e->v.IfExp.body);
1134 VISIT(st, expr, e->v.IfExp.orelse);
1135 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 case Dict_kind:
1137 VISIT_SEQ(st, expr, e->v.Dict.keys);
1138 VISIT_SEQ(st, expr, e->v.Dict.values);
1139 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001140 case Set_kind:
1141 VISIT_SEQ(st, expr, e->v.Set.elts);
1142 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001143 case ListComp_kind:
1144 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 return 0;
1146 VISIT(st, expr, e->v.ListComp.elt);
1147 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1148 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001149 case GeneratorExp_kind:
1150 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 case Yield_kind:
1154 if (e->v.Yield.value)
1155 VISIT(st, expr, e->v.Yield.value);
1156 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001157 if (st->st_cur->ste_returns_value) {
1158 PyErr_SetString(PyExc_SyntaxError,
1159 RETURN_VAL_IN_GENERATOR);
1160 PyErr_SyntaxLocation(st->st_filename,
1161 e->lineno);
1162 return 0;
1163 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 break;
1165 case Compare_kind:
1166 VISIT(st, expr, e->v.Compare.left);
1167 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1168 break;
1169 case Call_kind:
1170 VISIT(st, expr, e->v.Call.func);
1171 VISIT_SEQ(st, expr, e->v.Call.args);
1172 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1173 if (e->v.Call.starargs)
1174 VISIT(st, expr, e->v.Call.starargs);
1175 if (e->v.Call.kwargs)
1176 VISIT(st, expr, e->v.Call.kwargs);
1177 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 case Num_kind:
1179 case Str_kind:
Georg Brandl52318d62006-09-06 07:06:08 +00001180 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 /* Nothing to do here. */
1182 break;
1183 /* The following exprs can be assignment targets. */
1184 case Attribute_kind:
1185 VISIT(st, expr, e->v.Attribute.value);
1186 break;
1187 case Subscript_kind:
1188 VISIT(st, expr, e->v.Subscript.value);
1189 VISIT(st, slice, e->v.Subscript.slice);
1190 break;
1191 case Name_kind:
1192 if (!symtable_add_def(st, e->v.Name.id,
1193 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1194 return 0;
1195 break;
1196 /* child nodes of List and Tuple will have expr_context set */
1197 case List_kind:
1198 VISIT_SEQ(st, expr, e->v.List.elts);
1199 break;
1200 case Tuple_kind:
1201 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1202 break;
1203 }
1204 return 1;
1205}
1206
1207static int
1208symtable_implicit_arg(struct symtable *st, int pos)
1209{
1210 PyObject *id = PyString_FromFormat(".%d", pos);
1211 if (id == NULL)
1212 return 0;
1213 if (!symtable_add_def(st, id, DEF_PARAM)) {
1214 Py_DECREF(id);
1215 return 0;
1216 }
1217 Py_DECREF(id);
1218 return 1;
1219}
1220
1221static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001222symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel,
1223 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001225 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001226
1227 if (!args)
1228 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229
1230 /* go through all the toplevel arguments first */
1231 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001232 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1233 if (arg->kind == SimpleArg_kind) {
1234 if (!annotations) {
1235 if (!symtable_add_def(st,
1236 arg->v.SimpleArg.arg,
1237 DEF_PARAM))
1238 return 0;
1239 }
1240 else if (arg->v.SimpleArg.annotation)
1241 VISIT(st, expr, arg->v.SimpleArg.annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001243 else if (arg->kind == NestedArgs_kind) {
1244 if (toplevel && !annotations) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 if (!symtable_implicit_arg(st, i))
1246 return 0;
1247 }
1248 }
1249 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001250 PyErr_SetString(PyExc_SyntaxError,
1251 "invalid expression in parameter list");
1252 PyErr_SyntaxLocation(st->st_filename,
1253 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 return 0;
1255 }
1256 }
1257
1258 if (!toplevel) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001259 if (!symtable_visit_params_nested(st, args, annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 return 0;
1261 }
1262
1263 return 1;
1264}
1265
1266static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001267symtable_visit_params_nested(struct symtable *st, asdl_seq *args,
1268 int annotations)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269{
1270 int i;
1271 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001272 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1273 if (arg->kind == NestedArgs_kind &&
1274 !symtable_visit_params(st, arg->v.NestedArgs.args, 0,
1275 annotations))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 return 0;
1277 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001278
1279 return 1;
1280}
1281
1282
1283static int
1284symtable_visit_annotations(struct symtable *st, stmt_ty s)
1285{
1286 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287
Neal Norwitzc1505362006-12-28 06:47:50 +00001288 if (a->args && !symtable_visit_params(st, a->args, 1, 1))
1289 return 0;
1290 if (a->varargannotation)
1291 VISIT(st, expr, a->varargannotation);
1292 if (a->kwargannotation)
1293 VISIT(st, expr, a->kwargannotation);
1294 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 1))
1295 return 0;
1296 if (s->v.FunctionDef.returns)
1297 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 return 1;
1299}
1300
1301static int
1302symtable_visit_arguments(struct symtable *st, arguments_ty a)
1303{
1304 /* skip default arguments inside function block
1305 XXX should ast be different?
1306 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001307 if (a->args && !symtable_visit_params(st, a->args, 1, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001309 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1, 0))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001310 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 if (a->vararg) {
1312 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1313 return 0;
1314 st->st_cur->ste_varargs = 1;
1315 }
1316 if (a->kwarg) {
1317 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1318 return 0;
1319 st->st_cur->ste_varkeywords = 1;
1320 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001321 if (a->args && !symtable_visit_params_nested(st, a->args, 0))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322 return 0;
1323 return 1;
1324}
1325
1326
1327static int
1328symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1329{
1330 if (eh->type)
1331 VISIT(st, expr, eh->type);
1332 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001333 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1334 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 VISIT_SEQ(st, stmt, eh->body);
1336 return 1;
1337}
1338
1339
1340static int
1341symtable_visit_alias(struct symtable *st, alias_ty a)
1342{
1343 /* Compute store_name, the name actually bound by the import
1344 operation. It is diferent than a->name when a->name is a
1345 dotted package name (e.g. spam.eggs)
1346 */
1347 PyObject *store_name;
1348 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1349 const char *base = PyString_AS_STRING(name);
1350 char *dot = strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001351 if (dot) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 store_name = PyString_FromStringAndSize(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001353 if (!store_name)
1354 return 0;
1355 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 else {
1357 store_name = name;
1358 Py_INCREF(store_name);
1359 }
1360 if (strcmp(PyString_AS_STRING(name), "*")) {
1361 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1362 Py_DECREF(store_name);
1363 return r;
1364 }
1365 else {
1366 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001367 int lineno = st->st_cur->ste_lineno;
1368 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001369 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001371 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 }
1373 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001374 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 return 1;
1376 }
1377}
1378
1379
1380static int
1381symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1382{
1383 VISIT(st, expr, lc->target);
1384 VISIT(st, expr, lc->iter);
1385 VISIT_SEQ(st, expr, lc->ifs);
1386 return 1;
1387}
1388
1389
1390static int
1391symtable_visit_keyword(struct symtable *st, keyword_ty k)
1392{
1393 VISIT(st, expr, k->value);
1394 return 1;
1395}
1396
1397
1398static int
1399symtable_visit_slice(struct symtable *st, slice_ty s)
1400{
1401 switch (s->kind) {
1402 case Slice_kind:
1403 if (s->v.Slice.lower)
1404 VISIT(st, expr, s->v.Slice.lower)
1405 if (s->v.Slice.upper)
1406 VISIT(st, expr, s->v.Slice.upper)
1407 if (s->v.Slice.step)
1408 VISIT(st, expr, s->v.Slice.step)
1409 break;
1410 case ExtSlice_kind:
1411 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1412 break;
1413 case Index_kind:
1414 VISIT(st, expr, s->v.Index.value)
1415 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 }
1417 return 1;
1418}
1419
1420static int
1421symtable_visit_genexp(struct symtable *st, expr_ty e)
1422{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 comprehension_ty outermost = ((comprehension_ty)
1424 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1425 /* Outermost iterator is evaluated in current scope */
1426 VISIT(st, expr, outermost->iter);
1427 /* Create generator scope for the rest */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001428 if (!GET_IDENTIFIER(genexpr) ||
1429 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 return 0;
1431 }
1432 st->st_cur->ste_generator = 1;
1433 /* Outermost iter is received as an argument */
1434 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001435 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 return 0;
1437 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001438 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1439 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1440 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1441 e->v.GeneratorExp.generators, 1, (void*)e);
1442 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001443 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444}