blob: b0ebed408b0e44d8f038305aae2de41093d260e2 [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);
175static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
176static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
177static int symtable_implicit_arg(struct symtable *st, int pos);
178
179
Nick Coghlan99b25332005-11-16 12:45:24 +0000180static identifier top = NULL, lambda = NULL, genexpr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181
182#define GET_IDENTIFIER(VAR) \
183 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
184
185#define DUPLICATE_ARGUMENT \
186"duplicate argument '%s' in function definition"
187
188static struct symtable *
189symtable_new(void)
190{
191 struct symtable *st;
192
193 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
194 if (st == NULL)
195 return NULL;
196
197 st->st_filename = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000198 st->st_symbols = NULL;
199
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 if ((st->st_stack = PyList_New(0)) == NULL)
201 goto fail;
202 if ((st->st_symbols = PyDict_New()) == NULL)
203 goto fail;
204 st->st_cur = NULL;
205 st->st_tmpname = 0;
206 st->st_private = NULL;
207 return st;
208 fail:
209 PySymtable_Free(st);
210 return NULL;
211}
212
213struct symtable *
214PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
215{
216 struct symtable *st = symtable_new();
217 asdl_seq *seq;
218 int i;
219
220 if (st == NULL)
221 return st;
222 st->st_filename = filename;
223 st->st_future = future;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000224 if (!GET_IDENTIFIER(top) ||
225 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000226 PySymtable_Free(st);
227 return NULL;
228 }
229
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230 st->st_top = st->st_cur;
231 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
232 /* Any other top-level initialization? */
233 switch (mod->kind) {
234 case Module_kind:
235 seq = mod->v.Module.body;
236 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237 if (!symtable_visit_stmt(st,
238 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 goto error;
240 break;
241 case Expression_kind:
242 if (!symtable_visit_expr(st, mod->v.Expression.body))
243 goto error;
244 break;
245 case Interactive_kind:
246 seq = mod->v.Interactive.body;
247 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000248 if (!symtable_visit_stmt(st,
249 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250 goto error;
251 break;
252 case Suite_kind:
253 PyErr_SetString(PyExc_RuntimeError,
254 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000255 goto error;
256 }
257 if (!symtable_exit_block(st, (void *)mod)) {
258 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259 return NULL;
260 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 if (symtable_analyze(st))
262 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000263 PySymtable_Free(st);
264 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000266 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267 PySymtable_Free(st);
268 return NULL;
269}
270
271void
272PySymtable_Free(struct symtable *st)
273{
274 Py_XDECREF(st->st_symbols);
275 Py_XDECREF(st->st_stack);
276 PyMem_Free((void *)st);
277}
278
279PySTEntryObject *
280PySymtable_Lookup(struct symtable *st, void *key)
281{
282 PyObject *k, *v;
283
284 k = PyLong_FromVoidPtr(key);
285 if (k == NULL)
286 return NULL;
287 v = PyDict_GetItem(st->st_symbols, k);
288 if (v) {
289 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291 }
292 else {
293 PyErr_SetString(PyExc_KeyError,
294 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000296
297 Py_DECREF(k);
298 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299}
300
301int
302PyST_GetScope(PySTEntryObject *ste, PyObject *name)
303{
304 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
305 if (!v)
306 return 0;
307 assert(PyInt_Check(v));
308 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
309}
310
311
312/* Analyze raw symbol information to determine scope of each name.
313
314 The next several functions are helpers for PySymtable_Analyze(),
315 which determines whether a name is local, global, or free. In addition,
316 it determines which local variables are cell variables; they provide
317 bindings that are used for free variables in enclosed blocks.
318
319 There are also two kinds of free variables, implicit and explicit. An
320 explicit global is declared with the global statement. An implicit
321 global is a free variable for which the compiler has found no binding
322 in an enclosing function scope. The implicit global is either a global
323 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
324 to handle these names to implement slightly odd semantics. In such a
325 block, the name is treated as global until it is assigned to; then it
326 is treated as a local.
327
328 The symbol table requires two passes to determine the scope of each name.
329 The first pass collects raw facts from the AST: the name is a parameter
330 here, the name is used by not defined here, etc. The second pass analyzes
331 these facts during a pass over the PySTEntryObjects created during pass 1.
332
333 When a function is entered during the second pass, the parent passes
334 the set of all name bindings visible to its children. These bindings
335 are used to determine if the variable is free or an implicit global.
336 After doing the local analysis, it analyzes each of its child blocks
337 using an updated set of name bindings.
338
339 The children update the free variable set. If a local variable is free
340 in a child, the variable is marked as a cell. The current function must
341 provide runtime storage for the variable that may outlive the function's
342 frame. Cell variables are removed from the free set before the analyze
343 function returns to its parent.
344
345 The sets of bound and free variables are implemented as dictionaries
346 mapping strings to None.
347*/
348
349#define SET_SCOPE(DICT, NAME, I) { \
350 PyObject *o = PyInt_FromLong(I); \
351 if (!o) \
352 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000353 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
354 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000356 } \
357 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358}
359
360/* Decide on scope of name, given flags.
361
362 The dicts passed in as arguments are modified as necessary.
363 ste is passed so that flags can be updated.
364*/
365
366static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000367analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368 PyObject *bound, PyObject *local, PyObject *free,
369 PyObject *global)
370{
371 if (flags & DEF_GLOBAL) {
372 if (flags & DEF_PARAM) {
373 PyErr_Format(PyExc_SyntaxError,
374 "name '%s' is local and global",
375 PyString_AS_STRING(name));
376 return 0;
377 }
378 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
379 if (PyDict_SetItem(global, name, Py_None) < 0)
380 return 0;
381 if (bound && PyDict_GetItem(bound, name)) {
382 if (PyDict_DelItem(bound, name) < 0)
383 return 0;
384 }
385 return 1;
386 }
387 if (flags & DEF_BOUND) {
388 SET_SCOPE(dict, name, LOCAL);
389 if (PyDict_SetItem(local, name, Py_None) < 0)
390 return 0;
391 if (PyDict_GetItem(global, name)) {
392 if (PyDict_DelItem(global, name) < 0)
393 return 0;
394 }
395 return 1;
396 }
397 /* If an enclosing block has a binding for this name, it
398 is a free variable rather than a global variable.
399 Note that having a non-NULL bound implies that the block
400 is nested.
401 */
402 if (bound && PyDict_GetItem(bound, name)) {
403 SET_SCOPE(dict, name, FREE);
404 ste->ste_free = 1;
405 if (PyDict_SetItem(free, name, Py_None) < 0)
406 return 0;
407 return 1;
408 }
409 /* If a parent has a global statement, then call it global
410 explicit? It could also be global implicit.
411 */
412 else if (global && PyDict_GetItem(global, name)) {
413 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
414 return 1;
415 }
416 else {
417 if (ste->ste_nested)
418 ste->ste_free = 1;
419 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
420 return 1;
421 }
422 return 0; /* Can't get here */
423}
424
425#undef SET_SCOPE
426
427/* If a name is defined in free and also in locals, then this block
428 provides the binding for the free variable. The name should be
429 marked CELL in this block and removed from the free list.
430
431 Note that the current block's free variables are included in free.
432 That's safe because no name can be free and local in the same scope.
433*/
434
435static int
436analyze_cells(PyObject *scope, PyObject *free)
437{
438 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000439 int success = 0;
440 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441
442 w = PyInt_FromLong(CELL);
443 if (!w)
444 return 0;
445 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000446 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000447 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000448 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449 if (flags != LOCAL)
450 continue;
451 if (!PyDict_GetItem(free, name))
452 continue;
453 /* Replace LOCAL with CELL for this name, and remove
454 from free. It is safe to replace the value of name
455 in the dict, because it will not cause a resize.
456 */
457 if (PyDict_SetItem(scope, name, w) < 0)
458 goto error;
459 if (!PyDict_DelItem(free, name) < 0)
460 goto error;
461 }
462 success = 1;
463 error:
464 Py_DECREF(w);
465 return success;
466}
467
468/* Check for illegal statements in unoptimized namespaces */
469static int
470check_unoptimized(const PySTEntryObject* ste) {
471 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000472 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000474 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475 || !(ste->ste_free || ste->ste_child_free))
476 return 1;
477
Armin Rigo31441302005-10-21 12:57:31 +0000478 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479 "contains a nested function with free variables" :
480 "is a nested function");
481
482 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000483 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484 return 1;
485 case OPT_IMPORT_STAR:
486 PyOS_snprintf(buf, sizeof(buf),
487 "import * is not allowed in function '%.100s' "
488 "because it is %s",
489 PyString_AS_STRING(ste->ste_name), trailer);
490 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000491 }
492
493 PyErr_SetString(PyExc_SyntaxError, buf);
494 PyErr_SyntaxLocation(ste->ste_table->st_filename,
495 ste->ste_opt_lineno);
496 return 0;
497}
498
499/* Enter the final scope information into the st_symbols dict.
500 *
501 * All arguments are dicts. Modifies symbols, others are read-only.
502*/
503static int
504update_symbols(PyObject *symbols, PyObject *scope,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506{
507 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000508 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509
510 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000511 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 assert(PyInt_Check(v));
513 flags = PyInt_AS_LONG(v);
514 w = PyDict_GetItem(scope, name);
515 assert(w && PyInt_Check(w));
516 i = PyInt_AS_LONG(w);
517 flags |= (i << SCOPE_OFF);
518 u = PyInt_FromLong(flags);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000519 if (!u)
520 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 if (PyDict_SetItem(symbols, name, u) < 0) {
522 Py_DECREF(u);
523 return 0;
524 }
525 Py_DECREF(u);
526 }
527
528 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
529 if (!free_value)
530 return 0;
531
532 /* add a free variable when it's only use is for creating a closure */
533 pos = 0;
534 while (PyDict_Next(free, &pos, &name, &v)) {
535 PyObject *o = PyDict_GetItem(symbols, name);
536
537 if (o) {
538 /* It could be a free variable in a method of
539 the class that has the same name as a local
540 or global in the class scope.
541 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000544 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 o = PyInt_FromLong(i);
546 if (!o) {
547 Py_DECREF(free_value);
548 return 0;
549 }
550 if (PyDict_SetItem(symbols, name, o) < 0) {
551 Py_DECREF(o);
552 Py_DECREF(free_value);
553 return 0;
554 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000555 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 }
557 /* else it's not free, probably a cell */
558 continue;
559 }
560 if (!PyDict_GetItem(bound, name))
561 continue; /* it's a global */
562
563 if (PyDict_SetItem(symbols, name, free_value) < 0) {
564 Py_DECREF(free_value);
565 return 0;
566 }
567 }
568 Py_DECREF(free_value);
569 return 1;
570}
571
572/* Make final symbol table decisions for block of ste.
573 Arguments:
574 ste -- current symtable entry (input/output)
575 bound -- set of variables bound in enclosing scopes (input)
576 free -- set of free variables in enclosed scopes (output)
577 globals -- set of declared global variables in enclosing scopes (input)
578*/
579
580static int
581analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
582 PyObject *global)
583{
584 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
585 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000586 int i, success = 0;
587 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
589 local = PyDict_New();
590 if (!local)
591 goto error;
592 scope = PyDict_New();
593 if (!scope)
594 goto error;
595 newglobal = PyDict_New();
596 if (!newglobal)
597 goto error;
598 newfree = PyDict_New();
599 if (!newfree)
600 goto error;
601 newbound = PyDict_New();
602 if (!newbound)
603 goto error;
604
605 if (ste->ste_type == ClassBlock) {
606 /* make a copy of globals before calling analyze_name(),
607 because global statements in the class have no effect
608 on nested functions.
609 */
610 if (PyDict_Update(newglobal, global) < 0)
611 goto error;
612 if (bound)
613 if (PyDict_Update(newbound, bound) < 0)
614 goto error;
615 }
616
617 assert(PySTEntry_Check(ste));
618 assert(PyDict_Check(ste->ste_symbols));
619 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000620 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 if (!analyze_name(ste, scope, name, flags, bound, local, free,
622 global))
623 goto error;
624 }
625
626 if (ste->ste_type != ClassBlock) {
627 if (ste->ste_type == FunctionBlock) {
628 if (PyDict_Update(newbound, local) < 0)
629 goto error;
630 }
631 if (bound) {
632 if (PyDict_Update(newbound, bound) < 0)
633 goto error;
634 }
635 if (PyDict_Update(newglobal, global) < 0)
636 goto error;
637 }
638
639 /* Recursively call analyze_block() on each child block */
640 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
641 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000642 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000644 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (!analyze_block(entry, newbound, newfree, newglobal))
646 goto error;
647 if (entry->ste_free || entry->ste_child_free)
648 ste->ste_child_free = 1;
649 }
650
651 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
652 goto error;
653 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
654 ste->ste_type == ClassBlock))
655 goto error;
656 if (!check_unoptimized(ste))
657 goto error;
658
659 if (PyDict_Update(free, newfree) < 0)
660 goto error;
661 success = 1;
662 error:
663 Py_XDECREF(local);
664 Py_XDECREF(scope);
665 Py_XDECREF(newbound);
666 Py_XDECREF(newglobal);
667 Py_XDECREF(newfree);
668 if (!success)
669 assert(PyErr_Occurred());
670 return success;
671}
672
673static int
674symtable_analyze(struct symtable *st)
675{
676 PyObject *free, *global;
677 int r;
678
679 free = PyDict_New();
680 if (!free)
681 return 0;
682 global = PyDict_New();
683 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000684 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685 return 0;
686 }
687 r = analyze_block(st->st_top, NULL, free, global);
688 Py_DECREF(free);
689 Py_DECREF(global);
690 return r;
691}
692
693
694static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000695symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696{
697 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000698 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
700 PyErr_SetString(PyExc_SyntaxError, msg);
701 PyErr_SyntaxLocation(st->st_filename,
702 st->st_cur->ste_lineno);
703 }
704 return 0;
705 }
706 return 1;
707}
708
709/* symtable_enter_block() gets a reference via PySTEntry_New().
710 This reference is released when the block is exited, via the DECREF
711 in symtable_exit_block().
712*/
713
714static int
715symtable_exit_block(struct symtable *st, void *ast)
716{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000717 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000719 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 end = PyList_GET_SIZE(st->st_stack) - 1;
721 if (end >= 0) {
722 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
723 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000724 if (st->st_cur == NULL)
725 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 Py_INCREF(st->st_cur);
727 if (PySequence_DelItem(st->st_stack, end) < 0)
728 return 0;
729 }
730 return 1;
731}
732
733static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000734symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735 void *ast, int lineno)
736{
737 PySTEntryObject *prev = NULL;
738
739 if (st->st_cur) {
740 prev = st->st_cur;
741 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742 return 0;
743 }
744 Py_DECREF(st->st_cur);
745 }
746 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000747 if (st->st_cur == NULL)
748 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 if (name == GET_IDENTIFIER(top))
750 st->st_global = st->st_cur->ste_symbols;
751 if (prev) {
752 if (PyList_Append(prev->ste_children,
753 (PyObject *)st->st_cur) < 0) {
754 return 0;
755 }
756 }
757 return 1;
758}
759
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000760static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761symtable_lookup(struct symtable *st, PyObject *name)
762{
763 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000764 PyObject *mangled = _Py_Mangle(st->st_private, name);
765 if (!mangled)
766 return 0;
767 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
768 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 if (!o)
770 return 0;
771 return PyInt_AsLong(o);
772}
773
774static int
775symtable_add_def(struct symtable *st, PyObject *name, int flag)
776{
777 PyObject *o;
778 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000779 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000780 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000782 if (!mangled)
783 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000785 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 val = PyInt_AS_LONG(o);
787 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000788 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
790 PyString_AsString(name));
791 PyErr_SyntaxLocation(st->st_filename,
792 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000793 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 }
795 val |= flag;
796 } else
797 val = flag;
798 o = PyInt_FromLong(val);
799 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000800 goto error;
801 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000803 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 }
805 Py_DECREF(o);
806
807 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000808 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
809 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 } else if (flag & DEF_GLOBAL) {
811 /* XXX need to update DEF_GLOBAL for other flags too;
812 perhaps only DEF_FREE_GLOBAL */
813 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000814 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 val |= PyInt_AS_LONG(o);
816 }
817 o = PyInt_FromLong(val);
818 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000819 goto error;
820 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000822 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 }
824 Py_DECREF(o);
825 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000826 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000828
829error:
830 Py_DECREF(mangled);
831 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832}
833
834/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
835 They use the ASDL name to synthesize the name of the C type and the visit
836 function.
837
838 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
839 useful if the first node in the sequence requires special treatment.
840*/
841
842#define VISIT(ST, TYPE, V) \
843 if (!symtable_visit_ ## TYPE((ST), (V))) \
844 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000845
846#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
847 if (!symtable_visit_ ## TYPE((ST), (V))) { \
848 symtable_exit_block((ST), (S)); \
849 return 0; \
850 }
851
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852#define VISIT_SEQ(ST, TYPE, SEQ) { \
853 int i; \
854 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
855 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000856 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 if (!symtable_visit_ ## TYPE((ST), elt)) \
858 return 0; \
859 } \
860}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000861
862#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
863 int i; \
864 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
865 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000866 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000867 if (!symtable_visit_ ## TYPE((ST), elt)) { \
868 symtable_exit_block((ST), (S)); \
869 return 0; \
870 } \
871 } \
872}
873
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
875 int i; \
876 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
877 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000878 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879 if (!symtable_visit_ ## TYPE((ST), elt)) \
880 return 0; \
881 } \
882}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000883
884#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
885 int i; \
886 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
887 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000888 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000889 if (!symtable_visit_ ## TYPE((ST), elt)) { \
890 symtable_exit_block((ST), (S)); \
891 return 0; \
892 } \
893 } \
894}
895
Guido van Rossum4f72a782006-10-27 23:31:49 +0000896#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
897 int i = 0; \
898 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
899 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
900 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
901 if (!elt) continue; /* can be NULL */ \
902 if (!symtable_visit_expr((ST), elt)) \
903 return 0; \
904 } \
905}
906
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000908symtable_new_tmpname(struct symtable *st)
909{
910 char tmpname[256];
911 identifier tmp;
912
913 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
914 ++st->st_cur->ste_tmpname);
915 tmp = PyString_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000916 if (!tmp)
917 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000918 if (!symtable_add_def(st, tmp, DEF_LOCAL))
919 return 0;
920 Py_DECREF(tmp);
921 return 1;
922}
923
Guido van Rossum4f72a782006-10-27 23:31:49 +0000924
925
Guido van Rossumc2e20742006-02-27 22:32:47 +0000926static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927symtable_visit_stmt(struct symtable *st, stmt_ty s)
928{
929 switch (s->kind) {
930 case FunctionDef_kind:
931 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
932 return 0;
933 if (s->v.FunctionDef.args->defaults)
934 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000935 if (s->v.FunctionDef.args->kw_defaults)
936 VISIT_KWONLYDEFAULTS(st,
937 s->v.FunctionDef.args->kw_defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938 if (s->v.FunctionDef.decorators)
939 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
940 if (!symtable_enter_block(st, s->v.FunctionDef.name,
941 FunctionBlock, (void *)s, s->lineno))
942 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000943 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
944 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945 if (!symtable_exit_block(st, s))
946 return 0;
947 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000948 case ClassDef_kind: {
949 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
951 return 0;
952 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
953 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
954 (void *)s, s->lineno))
955 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000956 tmp = st->st_private;
957 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000958 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000959 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 if (!symtable_exit_block(st, s))
961 return 0;
962 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000963 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000965 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000967 st->st_cur->ste_returns_value = 1;
968 if (st->st_cur->ste_generator) {
969 PyErr_SetString(PyExc_SyntaxError,
970 RETURN_VAL_IN_GENERATOR);
971 PyErr_SyntaxLocation(st->st_filename,
972 s->lineno);
973 return 0;
974 }
975 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 break;
977 case Delete_kind:
978 VISIT_SEQ(st, expr, s->v.Delete.targets);
979 break;
980 case Assign_kind:
981 VISIT_SEQ(st, expr, s->v.Assign.targets);
982 VISIT(st, expr, s->v.Assign.value);
983 break;
984 case AugAssign_kind:
985 VISIT(st, expr, s->v.AugAssign.target);
986 VISIT(st, expr, s->v.AugAssign.value);
987 break;
988 case Print_kind:
989 if (s->v.Print.dest)
990 VISIT(st, expr, s->v.Print.dest);
991 VISIT_SEQ(st, expr, s->v.Print.values);
992 break;
993 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
1222symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1223{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001224 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225
1226 /* go through all the toplevel arguments first */
1227 for (i = 0; i < asdl_seq_LEN(args); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001228 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 if (arg->kind == Name_kind) {
1230 assert(arg->v.Name.ctx == Param ||
1231 (arg->v.Name.ctx == Store && !toplevel));
1232 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1233 return 0;
1234 }
1235 else if (arg->kind == Tuple_kind) {
1236 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 if (toplevel) {
1238 if (!symtable_implicit_arg(st, i))
1239 return 0;
1240 }
1241 }
1242 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001243 PyErr_SetString(PyExc_SyntaxError,
1244 "invalid expression in parameter list");
1245 PyErr_SyntaxLocation(st->st_filename,
1246 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 return 0;
1248 }
1249 }
1250
1251 if (!toplevel) {
1252 if (!symtable_visit_params_nested(st, args))
1253 return 0;
1254 }
1255
1256 return 1;
1257}
1258
1259static int
1260symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1261{
1262 int i;
1263 for (i = 0; i < asdl_seq_LEN(args); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 if (arg->kind == Tuple_kind &&
1266 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1267 return 0;
1268 }
1269
1270 return 1;
1271}
1272
1273static int
1274symtable_visit_arguments(struct symtable *st, arguments_ty a)
1275{
1276 /* skip default arguments inside function block
1277 XXX should ast be different?
1278 */
1279 if (a->args && !symtable_visit_params(st, a->args, 1))
1280 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001281 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs, 1))
1282 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 if (a->vararg) {
1284 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1285 return 0;
1286 st->st_cur->ste_varargs = 1;
1287 }
1288 if (a->kwarg) {
1289 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1290 return 0;
1291 st->st_cur->ste_varkeywords = 1;
1292 }
1293 if (a->args && !symtable_visit_params_nested(st, a->args))
1294 return 0;
1295 return 1;
1296}
1297
1298
1299static int
1300symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1301{
1302 if (eh->type)
1303 VISIT(st, expr, eh->type);
1304 if (eh->name)
1305 VISIT(st, expr, eh->name);
1306 VISIT_SEQ(st, stmt, eh->body);
1307 return 1;
1308}
1309
1310
1311static int
1312symtable_visit_alias(struct symtable *st, alias_ty a)
1313{
1314 /* Compute store_name, the name actually bound by the import
1315 operation. It is diferent than a->name when a->name is a
1316 dotted package name (e.g. spam.eggs)
1317 */
1318 PyObject *store_name;
1319 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1320 const char *base = PyString_AS_STRING(name);
1321 char *dot = strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001322 if (dot) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 store_name = PyString_FromStringAndSize(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001324 if (!store_name)
1325 return 0;
1326 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 else {
1328 store_name = name;
1329 Py_INCREF(store_name);
1330 }
1331 if (strcmp(PyString_AS_STRING(name), "*")) {
1332 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1333 Py_DECREF(store_name);
1334 return r;
1335 }
1336 else {
1337 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001338 int lineno = st->st_cur->ste_lineno;
1339 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001340 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001342 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 }
1344 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001345 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 return 1;
1347 }
1348}
1349
1350
1351static int
1352symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1353{
1354 VISIT(st, expr, lc->target);
1355 VISIT(st, expr, lc->iter);
1356 VISIT_SEQ(st, expr, lc->ifs);
1357 return 1;
1358}
1359
1360
1361static int
1362symtable_visit_keyword(struct symtable *st, keyword_ty k)
1363{
1364 VISIT(st, expr, k->value);
1365 return 1;
1366}
1367
1368
1369static int
1370symtable_visit_slice(struct symtable *st, slice_ty s)
1371{
1372 switch (s->kind) {
1373 case Slice_kind:
1374 if (s->v.Slice.lower)
1375 VISIT(st, expr, s->v.Slice.lower)
1376 if (s->v.Slice.upper)
1377 VISIT(st, expr, s->v.Slice.upper)
1378 if (s->v.Slice.step)
1379 VISIT(st, expr, s->v.Slice.step)
1380 break;
1381 case ExtSlice_kind:
1382 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1383 break;
1384 case Index_kind:
1385 VISIT(st, expr, s->v.Index.value)
1386 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 }
1388 return 1;
1389}
1390
1391static int
1392symtable_visit_genexp(struct symtable *st, expr_ty e)
1393{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 comprehension_ty outermost = ((comprehension_ty)
1395 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1396 /* Outermost iterator is evaluated in current scope */
1397 VISIT(st, expr, outermost->iter);
1398 /* Create generator scope for the rest */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001399 if (!GET_IDENTIFIER(genexpr) ||
1400 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 return 0;
1402 }
1403 st->st_cur->ste_generator = 1;
1404 /* Outermost iter is received as an argument */
1405 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001406 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 return 0;
1408 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001409 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1410 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1411 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1412 e->v.GeneratorExp.generators, 1, (void*)e);
1413 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001414 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415}