blob: 56e187a7d156c795659b0e01fcca5294c8ddd466 [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
Georg Brandlddbaa662006-06-04 21:56:52 +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;
Georg Brandlddbaa662006-06-04 21:56:52 +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 = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000120 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000123 0,
124 (destructor)ste_dealloc, /* tp_dealloc */
125 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000126 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000127 0, /* tp_setattr */
128 0, /* tp_compare */
129 (reprfunc)ste_repr, /* tp_repr */
130 0, /* tp_as_number */
131 0, /* tp_as_sequence */
132 0, /* tp_as_mapping */
133 0, /* tp_hash */
134 0, /* tp_call */
135 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000136 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000137 0, /* tp_setattro */
138 0, /* tp_as_buffer */
139 Py_TPFLAGS_DEFAULT, /* tp_flags */
140 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000141 0, /* tp_traverse */
142 0, /* tp_clear */
143 0, /* tp_richcompare */
144 0, /* tp_weaklistoffset */
145 0, /* tp_iter */
146 0, /* tp_iternext */
147 0, /* tp_methods */
148 ste_memberlist, /* tp_members */
149 0, /* tp_getset */
150 0, /* tp_base */
151 0, /* tp_dict */
152 0, /* tp_descr_get */
153 0, /* tp_descr_set */
154 0, /* tp_dictoffset */
155 0, /* tp_init */
156 0, /* tp_alloc */
157 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000158};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000159
160static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000161static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000163 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164static int symtable_exit_block(struct symtable *st, void *ast);
165static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
166static int symtable_visit_expr(struct symtable *st, expr_ty s);
167static int symtable_visit_genexp(struct symtable *st, expr_ty s);
168static int symtable_visit_arguments(struct symtable *st, arguments_ty);
169static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
170static int symtable_visit_alias(struct symtable *st, alias_ty);
171static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
172static int symtable_visit_keyword(struct symtable *st, keyword_ty);
173static int symtable_visit_slice(struct symtable *st, slice_ty);
174static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
175static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
176static int symtable_implicit_arg(struct symtable *st, int pos);
177
178
Nick Coghlan99b25332005-11-16 12:45:24 +0000179static identifier top = NULL, lambda = NULL, genexpr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180
181#define GET_IDENTIFIER(VAR) \
182 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
183
184#define DUPLICATE_ARGUMENT \
185"duplicate argument '%s' in function definition"
186
187static struct symtable *
188symtable_new(void)
189{
190 struct symtable *st;
191
192 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
193 if (st == NULL)
194 return NULL;
195
196 st->st_filename = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000197 st->st_symbols = NULL;
198
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 if ((st->st_stack = PyList_New(0)) == NULL)
200 goto fail;
201 if ((st->st_symbols = PyDict_New()) == NULL)
202 goto fail;
203 st->st_cur = NULL;
204 st->st_tmpname = 0;
205 st->st_private = NULL;
206 return st;
207 fail:
208 PySymtable_Free(st);
209 return NULL;
210}
211
212struct symtable *
213PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
214{
215 struct symtable *st = symtable_new();
216 asdl_seq *seq;
217 int i;
218
219 if (st == NULL)
220 return st;
221 st->st_filename = filename;
222 st->st_future = future;
Neal Norwitz76059362006-08-19 04:52:03 +0000223 if (!GET_IDENTIFIER(top) ||
224 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000225 PySymtable_Free(st);
226 return NULL;
227 }
228
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229 st->st_top = st->st_cur;
230 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
231 /* Any other top-level initialization? */
232 switch (mod->kind) {
233 case Module_kind:
234 seq = mod->v.Module.body;
235 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000236 if (!symtable_visit_stmt(st,
237 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 goto error;
239 break;
240 case Expression_kind:
241 if (!symtable_visit_expr(st, mod->v.Expression.body))
242 goto error;
243 break;
244 case Interactive_kind:
245 seq = mod->v.Interactive.body;
246 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000247 if (!symtable_visit_stmt(st,
248 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249 goto error;
250 break;
251 case Suite_kind:
252 PyErr_SetString(PyExc_RuntimeError,
253 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000254 goto error;
255 }
256 if (!symtable_exit_block(st, (void *)mod)) {
257 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 return NULL;
259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260 if (symtable_analyze(st))
261 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000262 PySymtable_Free(st);
263 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000265 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266 PySymtable_Free(st);
267 return NULL;
268}
269
270void
271PySymtable_Free(struct symtable *st)
272{
273 Py_XDECREF(st->st_symbols);
274 Py_XDECREF(st->st_stack);
275 PyMem_Free((void *)st);
276}
277
278PySTEntryObject *
279PySymtable_Lookup(struct symtable *st, void *key)
280{
281 PyObject *k, *v;
282
283 k = PyLong_FromVoidPtr(key);
284 if (k == NULL)
285 return NULL;
286 v = PyDict_GetItem(st->st_symbols, k);
287 if (v) {
288 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290 }
291 else {
292 PyErr_SetString(PyExc_KeyError,
293 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000295
296 Py_DECREF(k);
297 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298}
299
300int
301PyST_GetScope(PySTEntryObject *ste, PyObject *name)
302{
303 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
304 if (!v)
305 return 0;
306 assert(PyInt_Check(v));
307 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
308}
309
310
311/* Analyze raw symbol information to determine scope of each name.
312
313 The next several functions are helpers for PySymtable_Analyze(),
314 which determines whether a name is local, global, or free. In addition,
315 it determines which local variables are cell variables; they provide
316 bindings that are used for free variables in enclosed blocks.
317
318 There are also two kinds of free variables, implicit and explicit. An
319 explicit global is declared with the global statement. An implicit
320 global is a free variable for which the compiler has found no binding
321 in an enclosing function scope. The implicit global is either a global
322 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
323 to handle these names to implement slightly odd semantics. In such a
324 block, the name is treated as global until it is assigned to; then it
325 is treated as a local.
326
327 The symbol table requires two passes to determine the scope of each name.
328 The first pass collects raw facts from the AST: the name is a parameter
329 here, the name is used by not defined here, etc. The second pass analyzes
330 these facts during a pass over the PySTEntryObjects created during pass 1.
331
332 When a function is entered during the second pass, the parent passes
333 the set of all name bindings visible to its children. These bindings
334 are used to determine if the variable is free or an implicit global.
335 After doing the local analysis, it analyzes each of its child blocks
336 using an updated set of name bindings.
337
338 The children update the free variable set. If a local variable is free
339 in a child, the variable is marked as a cell. The current function must
340 provide runtime storage for the variable that may outlive the function's
341 frame. Cell variables are removed from the free set before the analyze
342 function returns to its parent.
343
344 The sets of bound and free variables are implemented as dictionaries
345 mapping strings to None.
346*/
347
348#define SET_SCOPE(DICT, NAME, I) { \
349 PyObject *o = PyInt_FromLong(I); \
350 if (!o) \
351 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000352 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
353 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000355 } \
356 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357}
358
359/* Decide on scope of name, given flags.
360
361 The dicts passed in as arguments are modified as necessary.
362 ste is passed so that flags can be updated.
363*/
364
365static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000366analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367 PyObject *bound, PyObject *local, PyObject *free,
368 PyObject *global)
369{
370 if (flags & DEF_GLOBAL) {
371 if (flags & DEF_PARAM) {
372 PyErr_Format(PyExc_SyntaxError,
373 "name '%s' is local and global",
374 PyString_AS_STRING(name));
375 return 0;
376 }
377 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
378 if (PyDict_SetItem(global, name, Py_None) < 0)
379 return 0;
380 if (bound && PyDict_GetItem(bound, name)) {
381 if (PyDict_DelItem(bound, name) < 0)
382 return 0;
383 }
384 return 1;
385 }
386 if (flags & DEF_BOUND) {
387 SET_SCOPE(dict, name, LOCAL);
388 if (PyDict_SetItem(local, name, Py_None) < 0)
389 return 0;
390 if (PyDict_GetItem(global, name)) {
391 if (PyDict_DelItem(global, name) < 0)
392 return 0;
393 }
394 return 1;
395 }
396 /* If an enclosing block has a binding for this name, it
397 is a free variable rather than a global variable.
398 Note that having a non-NULL bound implies that the block
399 is nested.
400 */
401 if (bound && PyDict_GetItem(bound, name)) {
402 SET_SCOPE(dict, name, FREE);
403 ste->ste_free = 1;
404 if (PyDict_SetItem(free, name, Py_None) < 0)
405 return 0;
406 return 1;
407 }
408 /* If a parent has a global statement, then call it global
409 explicit? It could also be global implicit.
410 */
411 else if (global && PyDict_GetItem(global, name)) {
412 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
413 return 1;
414 }
415 else {
416 if (ste->ste_nested)
417 ste->ste_free = 1;
418 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
419 return 1;
420 }
421 return 0; /* Can't get here */
422}
423
424#undef SET_SCOPE
425
426/* If a name is defined in free and also in locals, then this block
427 provides the binding for the free variable. The name should be
428 marked CELL in this block and removed from the free list.
429
430 Note that the current block's free variables are included in free.
431 That's safe because no name can be free and local in the same scope.
432*/
433
434static int
435analyze_cells(PyObject *scope, PyObject *free)
436{
437 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000438 int success = 0;
439 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440
441 w = PyInt_FromLong(CELL);
442 if (!w)
443 return 0;
444 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000445 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000447 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000448 if (flags != LOCAL)
449 continue;
450 if (!PyDict_GetItem(free, name))
451 continue;
452 /* Replace LOCAL with CELL for this name, and remove
453 from free. It is safe to replace the value of name
454 in the dict, because it will not cause a resize.
455 */
456 if (PyDict_SetItem(scope, name, w) < 0)
457 goto error;
458 if (!PyDict_DelItem(free, name) < 0)
459 goto error;
460 }
461 success = 1;
462 error:
463 Py_DECREF(w);
464 return success;
465}
466
467/* Check for illegal statements in unoptimized namespaces */
468static int
469check_unoptimized(const PySTEntryObject* ste) {
470 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000471 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000473 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474 || !(ste->ste_free || ste->ste_child_free))
475 return 1;
476
Armin Rigo31441302005-10-21 12:57:31 +0000477 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478 "contains a nested function with free variables" :
479 "is a nested function");
480
481 switch (ste->ste_unoptimized) {
482 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
483 case OPT_EXEC: /* qualified exec is fine */
484 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;
491 case OPT_BARE_EXEC:
492 PyOS_snprintf(buf, sizeof(buf),
493 "unqualified exec is not allowed in function "
494 "'%.100s' it %s",
495 PyString_AS_STRING(ste->ste_name), trailer);
496 break;
497 default:
498 PyOS_snprintf(buf, sizeof(buf),
499 "function '%.100s' uses import * and bare exec, "
500 "which are illegal because it %s",
501 PyString_AS_STRING(ste->ste_name), trailer);
502 break;
503 }
504
505 PyErr_SetString(PyExc_SyntaxError, buf);
506 PyErr_SyntaxLocation(ste->ste_table->st_filename,
507 ste->ste_opt_lineno);
508 return 0;
509}
510
511/* Enter the final scope information into the st_symbols dict.
512 *
513 * All arguments are dicts. Modifies symbols, others are read-only.
514*/
515static int
516update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000517 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518{
519 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000520 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521
522 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000523 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 assert(PyInt_Check(v));
525 flags = PyInt_AS_LONG(v);
526 w = PyDict_GetItem(scope, name);
527 assert(w && PyInt_Check(w));
528 i = PyInt_AS_LONG(w);
529 flags |= (i << SCOPE_OFF);
530 u = PyInt_FromLong(flags);
Neal Norwitz18b6adf2006-07-23 07:50:36 +0000531 if (!u)
532 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533 if (PyDict_SetItem(symbols, name, u) < 0) {
534 Py_DECREF(u);
535 return 0;
536 }
537 Py_DECREF(u);
538 }
539
540 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
541 if (!free_value)
542 return 0;
543
544 /* add a free variable when it's only use is for creating a closure */
545 pos = 0;
546 while (PyDict_Next(free, &pos, &name, &v)) {
547 PyObject *o = PyDict_GetItem(symbols, name);
548
549 if (o) {
550 /* It could be a free variable in a method of
551 the class that has the same name as a local
552 or global in the class scope.
553 */
Anthony Baxter019aec62006-04-12 04:00:50 +0000554 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000556 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557 o = PyInt_FromLong(i);
558 if (!o) {
559 Py_DECREF(free_value);
560 return 0;
561 }
562 if (PyDict_SetItem(symbols, name, o) < 0) {
563 Py_DECREF(o);
564 Py_DECREF(free_value);
565 return 0;
566 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000567 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 }
569 /* else it's not free, probably a cell */
570 continue;
571 }
572 if (!PyDict_GetItem(bound, name))
573 continue; /* it's a global */
574
575 if (PyDict_SetItem(symbols, name, free_value) < 0) {
576 Py_DECREF(free_value);
577 return 0;
578 }
579 }
580 Py_DECREF(free_value);
581 return 1;
582}
583
584/* Make final symbol table decisions for block of ste.
585 Arguments:
586 ste -- current symtable entry (input/output)
587 bound -- set of variables bound in enclosing scopes (input)
588 free -- set of free variables in enclosed scopes (output)
589 globals -- set of declared global variables in enclosing scopes (input)
590*/
591
592static int
593analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
594 PyObject *global)
595{
596 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
597 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000598 int i, success = 0;
599 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
601 local = PyDict_New();
602 if (!local)
603 goto error;
604 scope = PyDict_New();
605 if (!scope)
606 goto error;
607 newglobal = PyDict_New();
608 if (!newglobal)
609 goto error;
610 newfree = PyDict_New();
611 if (!newfree)
612 goto error;
613 newbound = PyDict_New();
614 if (!newbound)
615 goto error;
616
617 if (ste->ste_type == ClassBlock) {
618 /* make a copy of globals before calling analyze_name(),
619 because global statements in the class have no effect
620 on nested functions.
621 */
622 if (PyDict_Update(newglobal, global) < 0)
623 goto error;
624 if (bound)
625 if (PyDict_Update(newbound, bound) < 0)
626 goto error;
627 }
628
629 assert(PySTEntry_Check(ste));
630 assert(PyDict_Check(ste->ste_symbols));
631 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000632 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 if (!analyze_name(ste, scope, name, flags, bound, local, free,
634 global))
635 goto error;
636 }
637
638 if (ste->ste_type != ClassBlock) {
639 if (ste->ste_type == FunctionBlock) {
640 if (PyDict_Update(newbound, local) < 0)
641 goto error;
642 }
643 if (bound) {
644 if (PyDict_Update(newbound, bound) < 0)
645 goto error;
646 }
647 if (PyDict_Update(newglobal, global) < 0)
648 goto error;
649 }
650
651 /* Recursively call analyze_block() on each child block */
652 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
653 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000654 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000656 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657 if (!analyze_block(entry, newbound, newfree, newglobal))
658 goto error;
659 if (entry->ste_free || entry->ste_child_free)
660 ste->ste_child_free = 1;
661 }
662
663 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
664 goto error;
665 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
666 ste->ste_type == ClassBlock))
667 goto error;
668 if (!check_unoptimized(ste))
669 goto error;
670
671 if (PyDict_Update(free, newfree) < 0)
672 goto error;
673 success = 1;
674 error:
675 Py_XDECREF(local);
676 Py_XDECREF(scope);
677 Py_XDECREF(newbound);
678 Py_XDECREF(newglobal);
679 Py_XDECREF(newfree);
680 if (!success)
681 assert(PyErr_Occurred());
682 return success;
683}
684
685static int
686symtable_analyze(struct symtable *st)
687{
688 PyObject *free, *global;
689 int r;
690
691 free = PyDict_New();
692 if (!free)
693 return 0;
694 global = PyDict_New();
695 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000696 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 return 0;
698 }
699 r = analyze_block(st->st_top, NULL, free, global);
700 Py_DECREF(free);
701 Py_DECREF(global);
702 return r;
703}
704
705
706static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000707symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708{
709 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000710 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
712 PyErr_SetString(PyExc_SyntaxError, msg);
713 PyErr_SyntaxLocation(st->st_filename,
714 st->st_cur->ste_lineno);
715 }
716 return 0;
717 }
718 return 1;
719}
720
721/* symtable_enter_block() gets a reference via PySTEntry_New().
722 This reference is released when the block is exited, via the DECREF
723 in symtable_exit_block().
724*/
725
726static int
727symtable_exit_block(struct symtable *st, void *ast)
728{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000729 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000731 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 end = PyList_GET_SIZE(st->st_stack) - 1;
733 if (end >= 0) {
734 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
735 end);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000736 if (st->st_cur == NULL)
737 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 Py_INCREF(st->st_cur);
739 if (PySequence_DelItem(st->st_stack, end) < 0)
740 return 0;
741 }
742 return 1;
743}
744
745static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000746symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 void *ast, int lineno)
748{
749 PySTEntryObject *prev = NULL;
750
751 if (st->st_cur) {
752 prev = st->st_cur;
753 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 return 0;
755 }
756 Py_DECREF(st->st_cur);
757 }
758 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000759 if (st->st_cur == NULL)
760 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 if (name == GET_IDENTIFIER(top))
762 st->st_global = st->st_cur->ste_symbols;
763 if (prev) {
764 if (PyList_Append(prev->ste_children,
765 (PyObject *)st->st_cur) < 0) {
766 return 0;
767 }
768 }
769 return 1;
770}
771
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000772static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773symtable_lookup(struct symtable *st, PyObject *name)
774{
775 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000776 PyObject *mangled = _Py_Mangle(st->st_private, name);
777 if (!mangled)
778 return 0;
779 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
780 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 if (!o)
782 return 0;
783 return PyInt_AsLong(o);
784}
785
786static int
787symtable_add_def(struct symtable *st, PyObject *name, int flag)
788{
789 PyObject *o;
790 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000791 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000792 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000794 if (!mangled)
795 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000797 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 val = PyInt_AS_LONG(o);
799 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000800 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
802 PyString_AsString(name));
803 PyErr_SyntaxLocation(st->st_filename,
804 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000805 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 }
807 val |= flag;
808 } else
809 val = flag;
810 o = PyInt_FromLong(val);
811 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000812 goto error;
813 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000815 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 }
817 Py_DECREF(o);
818
819 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000820 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
821 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 } else if (flag & DEF_GLOBAL) {
823 /* XXX need to update DEF_GLOBAL for other flags too;
824 perhaps only DEF_FREE_GLOBAL */
825 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000826 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 val |= PyInt_AS_LONG(o);
828 }
829 o = PyInt_FromLong(val);
830 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000831 goto error;
832 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000834 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 }
836 Py_DECREF(o);
837 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000838 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000840
841error:
842 Py_DECREF(mangled);
843 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844}
845
846/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
847 They use the ASDL name to synthesize the name of the C type and the visit
848 function.
849
850 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
851 useful if the first node in the sequence requires special treatment.
852*/
853
854#define VISIT(ST, TYPE, V) \
855 if (!symtable_visit_ ## TYPE((ST), (V))) \
856 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000857
858#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
859 if (!symtable_visit_ ## TYPE((ST), (V))) { \
860 symtable_exit_block((ST), (S)); \
861 return 0; \
862 }
863
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864#define VISIT_SEQ(ST, TYPE, SEQ) { \
865 int i; \
866 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
867 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000868 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 if (!symtable_visit_ ## TYPE((ST), elt)) \
870 return 0; \
871 } \
872}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000873
874#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
875 int i; \
876 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
877 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000878 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000879 if (!symtable_visit_ ## TYPE((ST), elt)) { \
880 symtable_exit_block((ST), (S)); \
881 return 0; \
882 } \
883 } \
884}
885
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
887 int i; \
888 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
889 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000890 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891 if (!symtable_visit_ ## TYPE((ST), elt)) \
892 return 0; \
893 } \
894}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000895
896#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
897 int i; \
898 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
899 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000900 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000901 if (!symtable_visit_ ## TYPE((ST), elt)) { \
902 symtable_exit_block((ST), (S)); \
903 return 0; \
904 } \
905 } \
906}
907
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000909symtable_new_tmpname(struct symtable *st)
910{
911 char tmpname[256];
912 identifier tmp;
913
914 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
915 ++st->st_cur->ste_tmpname);
916 tmp = PyString_InternFromString(tmpname);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +0000917 if (!tmp)
918 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000919 if (!symtable_add_def(st, tmp, DEF_LOCAL))
920 return 0;
921 Py_DECREF(tmp);
922 return 1;
923}
924
925static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926symtable_visit_stmt(struct symtable *st, stmt_ty s)
927{
928 switch (s->kind) {
929 case FunctionDef_kind:
930 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
931 return 0;
932 if (s->v.FunctionDef.args->defaults)
933 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Christian Heimes5224d282008-02-23 15:01:05 +0000934 if (s->v.FunctionDef.decorator_list)
935 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 if (!symtable_enter_block(st, s->v.FunctionDef.name,
937 FunctionBlock, (void *)s, s->lineno))
938 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000939 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
940 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 if (!symtable_exit_block(st, s))
942 return 0;
943 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000944 case ClassDef_kind: {
945 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
947 return 0;
948 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Christian Heimes5224d282008-02-23 15:01:05 +0000949 if (s->v.ClassDef.decorator_list)
950 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
952 (void *)s, s->lineno))
953 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000954 tmp = st->st_private;
955 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000956 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000957 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958 if (!symtable_exit_block(st, s))
959 return 0;
960 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000961 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962 case Return_kind:
Georg Brandlddbaa662006-06-04 21:56:52 +0000963 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964 VISIT(st, expr, s->v.Return.value);
Georg Brandlddbaa662006-06-04 21:56:52 +0000965 st->st_cur->ste_returns_value = 1;
966 if (st->st_cur->ste_generator) {
967 PyErr_SetString(PyExc_SyntaxError,
968 RETURN_VAL_IN_GENERATOR);
969 PyErr_SyntaxLocation(st->st_filename,
970 s->lineno);
971 return 0;
972 }
973 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 break;
975 case Delete_kind:
976 VISIT_SEQ(st, expr, s->v.Delete.targets);
977 break;
978 case Assign_kind:
979 VISIT_SEQ(st, expr, s->v.Assign.targets);
980 VISIT(st, expr, s->v.Assign.value);
981 break;
982 case AugAssign_kind:
983 VISIT(st, expr, s->v.AugAssign.target);
984 VISIT(st, expr, s->v.AugAssign.value);
985 break;
986 case Print_kind:
987 if (s->v.Print.dest)
988 VISIT(st, expr, s->v.Print.dest);
989 VISIT_SEQ(st, expr, s->v.Print.values);
990 break;
991 case For_kind:
992 VISIT(st, expr, s->v.For.target);
993 VISIT(st, expr, s->v.For.iter);
994 VISIT_SEQ(st, stmt, s->v.For.body);
995 if (s->v.For.orelse)
996 VISIT_SEQ(st, stmt, s->v.For.orelse);
997 break;
998 case While_kind:
999 VISIT(st, expr, s->v.While.test);
1000 VISIT_SEQ(st, stmt, s->v.While.body);
1001 if (s->v.While.orelse)
1002 VISIT_SEQ(st, stmt, s->v.While.orelse);
1003 break;
1004 case If_kind:
1005 /* XXX if 0: and lookup_yield() hacks */
1006 VISIT(st, expr, s->v.If.test);
1007 VISIT_SEQ(st, stmt, s->v.If.body);
1008 if (s->v.If.orelse)
1009 VISIT_SEQ(st, stmt, s->v.If.orelse);
1010 break;
1011 case Raise_kind:
1012 if (s->v.Raise.type) {
1013 VISIT(st, expr, s->v.Raise.type);
1014 if (s->v.Raise.inst) {
1015 VISIT(st, expr, s->v.Raise.inst);
1016 if (s->v.Raise.tback)
1017 VISIT(st, expr, s->v.Raise.tback);
1018 }
1019 }
1020 break;
1021 case TryExcept_kind:
1022 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1023 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1024 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1025 break;
1026 case TryFinally_kind:
1027 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1028 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1029 break;
1030 case Assert_kind:
1031 VISIT(st, expr, s->v.Assert.test);
1032 if (s->v.Assert.msg)
1033 VISIT(st, expr, s->v.Assert.msg);
1034 break;
1035 case Import_kind:
1036 VISIT_SEQ(st, alias, s->v.Import.names);
1037 /* XXX Don't have the lineno available inside
1038 visit_alias */
1039 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1040 st->st_cur->ste_opt_lineno = s->lineno;
1041 break;
1042 case ImportFrom_kind:
1043 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1044 /* XXX Don't have the lineno available inside
1045 visit_alias */
1046 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1047 st->st_cur->ste_opt_lineno = s->lineno;
1048 break;
1049 case Exec_kind:
1050 VISIT(st, expr, s->v.Exec.body);
1051 if (!st->st_cur->ste_opt_lineno)
1052 st->st_cur->ste_opt_lineno = s->lineno;
1053 if (s->v.Exec.globals) {
1054 st->st_cur->ste_unoptimized |= OPT_EXEC;
1055 VISIT(st, expr, s->v.Exec.globals);
1056 if (s->v.Exec.locals)
1057 VISIT(st, expr, s->v.Exec.locals);
1058 } else {
1059 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1060 }
1061 break;
1062 case Global_kind: {
1063 int i;
1064 asdl_seq *seq = s->v.Global.names;
1065 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001066 identifier name = (identifier)asdl_seq_GET(seq, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001068 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 if (cur < 0)
1070 return 0;
1071 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001072 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 if (cur & DEF_LOCAL)
1074 PyOS_snprintf(buf, sizeof(buf),
1075 GLOBAL_AFTER_ASSIGN,
1076 c_name);
1077 else
1078 PyOS_snprintf(buf, sizeof(buf),
1079 GLOBAL_AFTER_USE,
1080 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001081 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082 return 0;
1083 }
1084 if (!symtable_add_def(st, name, DEF_GLOBAL))
1085 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 break;
1088 }
1089 case Expr_kind:
1090 VISIT(st, expr, s->v.Expr.value);
1091 break;
1092 case Pass_kind:
1093 case Break_kind:
1094 case Continue_kind:
1095 /* nothing to do here */
1096 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001097 case With_kind:
1098 if (!symtable_new_tmpname(st))
1099 return 0;
1100 VISIT(st, expr, s->v.With.context_expr);
1101 if (s->v.With.optional_vars) {
1102 if (!symtable_new_tmpname(st))
1103 return 0;
1104 VISIT(st, expr, s->v.With.optional_vars);
1105 }
1106 VISIT_SEQ(st, stmt, s->v.With.body);
1107 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108 }
1109 return 1;
1110}
1111
1112static int
1113symtable_visit_expr(struct symtable *st, expr_ty e)
1114{
1115 switch (e->kind) {
1116 case BoolOp_kind:
1117 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1118 break;
1119 case BinOp_kind:
1120 VISIT(st, expr, e->v.BinOp.left);
1121 VISIT(st, expr, e->v.BinOp.right);
1122 break;
1123 case UnaryOp_kind:
1124 VISIT(st, expr, e->v.UnaryOp.operand);
1125 break;
1126 case Lambda_kind: {
Neal Norwitz76059362006-08-19 04:52:03 +00001127 if (!GET_IDENTIFIER(lambda) ||
1128 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 return 0;
1130 if (e->v.Lambda.args->defaults)
1131 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1132 /* XXX how to get line numbers for expressions */
Neal Norwitz76059362006-08-19 04:52:03 +00001133 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 FunctionBlock, (void *)e, 0))
1135 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001136 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1137 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 if (!symtable_exit_block(st, (void *)e))
1139 return 0;
1140 break;
1141 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001142 case IfExp_kind:
1143 VISIT(st, expr, e->v.IfExp.test);
1144 VISIT(st, expr, e->v.IfExp.body);
1145 VISIT(st, expr, e->v.IfExp.orelse);
1146 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 case Dict_kind:
1148 VISIT_SEQ(st, expr, e->v.Dict.keys);
1149 VISIT_SEQ(st, expr, e->v.Dict.values);
1150 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001151 case ListComp_kind:
1152 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 return 0;
1154 VISIT(st, expr, e->v.ListComp.elt);
1155 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1156 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001157 case GeneratorExp_kind:
1158 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 case Yield_kind:
1162 if (e->v.Yield.value)
1163 VISIT(st, expr, e->v.Yield.value);
1164 st->st_cur->ste_generator = 1;
Georg Brandlddbaa662006-06-04 21:56:52 +00001165 if (st->st_cur->ste_returns_value) {
1166 PyErr_SetString(PyExc_SyntaxError,
1167 RETURN_VAL_IN_GENERATOR);
1168 PyErr_SyntaxLocation(st->st_filename,
1169 e->lineno);
1170 return 0;
1171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 break;
1173 case Compare_kind:
1174 VISIT(st, expr, e->v.Compare.left);
1175 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1176 break;
1177 case Call_kind:
1178 VISIT(st, expr, e->v.Call.func);
1179 VISIT_SEQ(st, expr, e->v.Call.args);
1180 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1181 if (e->v.Call.starargs)
1182 VISIT(st, expr, e->v.Call.starargs);
1183 if (e->v.Call.kwargs)
1184 VISIT(st, expr, e->v.Call.kwargs);
1185 break;
1186 case Repr_kind:
1187 VISIT(st, expr, e->v.Repr.value);
1188 break;
1189 case Num_kind:
1190 case Str_kind:
1191 /* Nothing to do here. */
1192 break;
1193 /* The following exprs can be assignment targets. */
1194 case Attribute_kind:
1195 VISIT(st, expr, e->v.Attribute.value);
1196 break;
1197 case Subscript_kind:
1198 VISIT(st, expr, e->v.Subscript.value);
1199 VISIT(st, slice, e->v.Subscript.slice);
1200 break;
1201 case Name_kind:
1202 if (!symtable_add_def(st, e->v.Name.id,
1203 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1204 return 0;
1205 break;
1206 /* child nodes of List and Tuple will have expr_context set */
1207 case List_kind:
1208 VISIT_SEQ(st, expr, e->v.List.elts);
1209 break;
1210 case Tuple_kind:
1211 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1212 break;
1213 }
1214 return 1;
1215}
1216
1217static int
1218symtable_implicit_arg(struct symtable *st, int pos)
1219{
1220 PyObject *id = PyString_FromFormat(".%d", pos);
1221 if (id == NULL)
1222 return 0;
1223 if (!symtable_add_def(st, id, DEF_PARAM)) {
1224 Py_DECREF(id);
1225 return 0;
1226 }
1227 Py_DECREF(id);
1228 return 1;
1229}
1230
1231static int
1232symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1233{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001234 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235
1236 /* go through all the toplevel arguments first */
1237 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001238 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239 if (arg->kind == Name_kind) {
1240 assert(arg->v.Name.ctx == Param ||
1241 (arg->v.Name.ctx == Store && !toplevel));
1242 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1243 return 0;
1244 }
1245 else if (arg->kind == Tuple_kind) {
1246 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 if (toplevel) {
1248 if (!symtable_implicit_arg(st, i))
1249 return 0;
1250 }
1251 }
1252 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001253 PyErr_SetString(PyExc_SyntaxError,
1254 "invalid expression in parameter list");
1255 PyErr_SyntaxLocation(st->st_filename,
1256 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 return 0;
1258 }
1259 }
1260
1261 if (!toplevel) {
1262 if (!symtable_visit_params_nested(st, args))
1263 return 0;
1264 }
1265
1266 return 1;
1267}
1268
1269static int
1270symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1271{
1272 int i;
1273 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001274 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 if (arg->kind == Tuple_kind &&
1276 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1277 return 0;
1278 }
1279
1280 return 1;
1281}
1282
1283static int
1284symtable_visit_arguments(struct symtable *st, arguments_ty a)
1285{
1286 /* skip default arguments inside function block
1287 XXX should ast be different?
1288 */
1289 if (a->args && !symtable_visit_params(st, a->args, 1))
1290 return 0;
1291 if (a->vararg) {
1292 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1293 return 0;
1294 st->st_cur->ste_varargs = 1;
1295 }
1296 if (a->kwarg) {
1297 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1298 return 0;
1299 st->st_cur->ste_varkeywords = 1;
1300 }
1301 if (a->args && !symtable_visit_params_nested(st, a->args))
1302 return 0;
1303 return 1;
1304}
1305
1306
1307static int
1308symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1309{
1310 if (eh->type)
1311 VISIT(st, expr, eh->type);
1312 if (eh->name)
1313 VISIT(st, expr, eh->name);
1314 VISIT_SEQ(st, stmt, eh->body);
1315 return 1;
1316}
1317
1318
1319static int
1320symtable_visit_alias(struct symtable *st, alias_ty a)
1321{
1322 /* Compute store_name, the name actually bound by the import
1323 operation. It is diferent than a->name when a->name is a
1324 dotted package name (e.g. spam.eggs)
1325 */
1326 PyObject *store_name;
1327 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1328 const char *base = PyString_AS_STRING(name);
1329 char *dot = strchr(base, '.');
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001330 if (dot) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 store_name = PyString_FromStringAndSize(base, dot - base);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001332 if (!store_name)
1333 return 0;
1334 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 else {
1336 store_name = name;
1337 Py_INCREF(store_name);
1338 }
1339 if (strcmp(PyString_AS_STRING(name), "*")) {
1340 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1341 Py_DECREF(store_name);
1342 return r;
1343 }
1344 else {
1345 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001346 int lineno = st->st_cur->ste_lineno;
1347 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001348 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001350 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 }
1352 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001353 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 return 1;
1355 }
1356}
1357
1358
1359static int
1360symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1361{
1362 VISIT(st, expr, lc->target);
1363 VISIT(st, expr, lc->iter);
1364 VISIT_SEQ(st, expr, lc->ifs);
1365 return 1;
1366}
1367
1368
1369static int
1370symtable_visit_keyword(struct symtable *st, keyword_ty k)
1371{
1372 VISIT(st, expr, k->value);
1373 return 1;
1374}
1375
1376
1377static int
1378symtable_visit_slice(struct symtable *st, slice_ty s)
1379{
1380 switch (s->kind) {
1381 case Slice_kind:
1382 if (s->v.Slice.lower)
1383 VISIT(st, expr, s->v.Slice.lower)
1384 if (s->v.Slice.upper)
1385 VISIT(st, expr, s->v.Slice.upper)
1386 if (s->v.Slice.step)
1387 VISIT(st, expr, s->v.Slice.step)
1388 break;
1389 case ExtSlice_kind:
1390 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1391 break;
1392 case Index_kind:
1393 VISIT(st, expr, s->v.Index.value)
1394 break;
1395 case Ellipsis_kind:
1396 break;
1397 }
1398 return 1;
1399}
1400
1401static int
1402symtable_visit_genexp(struct symtable *st, expr_ty e)
1403{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 comprehension_ty outermost = ((comprehension_ty)
1405 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1406 /* Outermost iterator is evaluated in current scope */
1407 VISIT(st, expr, outermost->iter);
1408 /* Create generator scope for the rest */
Neal Norwitz76059362006-08-19 04:52:03 +00001409 if (!GET_IDENTIFIER(genexpr) ||
1410 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 return 0;
1412 }
1413 st->st_cur->ste_generator = 1;
1414 /* Outermost iter is received as an argument */
1415 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001416 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 return 0;
1418 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001419 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1420 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1421 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1422 e->v.GeneratorExp.generators, 1, (void*)e);
1423 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Neal Norwitz76059362006-08-19 04:52:03 +00001424 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425}