blob: 184723d5dcc18388e378906aa127df59dd468c8b [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
16
Neal Norwitz090b3dd2006-02-28 22:36:46 +000017/* XXX(nnorwitz): change name since static? */
18static PySTEntryObject *
Neal Norwitz62c2fac2005-10-24 00:30:44 +000019PySTEntry_New(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000021{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 PySTEntryObject *ste = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000023 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000024
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000025 k = PyLong_FromVoidPtr(key);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000026 if (k == NULL)
27 goto fail;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028 ste = (PySTEntryObject *)PyObject_New(PySTEntryObject,
29 &PySTEntry_Type);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000030 ste->ste_table = st;
31 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000032 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000033
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034 ste->ste_name = name;
35 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000036
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000037 ste->ste_symbols = NULL;
38 ste->ste_varnames = NULL;
39 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000040
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000041 ste->ste_symbols = PyDict_New();
42 if (ste->ste_symbols == NULL)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000043 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000044
45 ste->ste_varnames = PyList_New(0);
46 if (ste->ste_varnames == NULL)
47 goto fail;
48
49 ste->ste_children = PyList_New(0);
50 if (ste->ste_children == NULL)
51 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000052
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053 ste->ste_type = block;
54 ste->ste_unoptimized = 0;
55 ste->ste_nested = 0;
56 ste->ste_free = 0;
57 ste->ste_varargs = 0;
58 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000059 ste->ste_opt_lineno = 0;
Jeremy Hylton4d508ad2003-05-21 17:34:50 +000060 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000061 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000063 if (st->st_cur != NULL &&
64 (st->st_cur->ste_nested ||
65 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000066 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000067 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000068 ste->ste_generator = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069
70 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
71 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000072
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000073 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000074 fail:
75 Py_XDECREF(ste);
76 return NULL;
77}
78
79static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000080ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000081{
82 char buf[256];
83
Barry Warsaw4b4ab202001-11-28 21:36:28 +000084 PyOS_snprintf(buf, sizeof(buf),
85 "<symtable entry %.100s(%ld), line %d>",
86 PyString_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088 return PyString_FromString(buf);
89}
90
91static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093{
94 ste->ste_table = NULL;
95 Py_XDECREF(ste->ste_id);
96 Py_XDECREF(ste->ste_name);
97 Py_XDECREF(ste->ste_symbols);
98 Py_XDECREF(ste->ste_varnames);
99 Py_XDECREF(ste->ste_children);
100 PyObject_Del(ste);
101}
102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000104
Guido van Rossum6f799372001-09-20 20:46:19 +0000105static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000106 {"id", T_OBJECT, OFF(ste_id), READONLY},
107 {"name", T_OBJECT, OFF(ste_name), READONLY},
108 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
109 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
110 {"children", T_OBJECT, OFF(ste_children), READONLY},
111 {"type", T_INT, OFF(ste_type), READONLY},
112 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000113 {NULL}
114};
115
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000116PyTypeObject PySTEntry_Type = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000117 PyObject_HEAD_INIT(&PyType_Type)
118 0,
119 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121 0,
122 (destructor)ste_dealloc, /* tp_dealloc */
123 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000124 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000125 0, /* tp_setattr */
126 0, /* tp_compare */
127 (reprfunc)ste_repr, /* tp_repr */
128 0, /* tp_as_number */
129 0, /* tp_as_sequence */
130 0, /* tp_as_mapping */
131 0, /* tp_hash */
132 0, /* tp_call */
133 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000134 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000135 0, /* tp_setattro */
136 0, /* tp_as_buffer */
137 Py_TPFLAGS_DEFAULT, /* tp_flags */
138 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000139 0, /* tp_traverse */
140 0, /* tp_clear */
141 0, /* tp_richcompare */
142 0, /* tp_weaklistoffset */
143 0, /* tp_iter */
144 0, /* tp_iternext */
145 0, /* tp_methods */
146 ste_memberlist, /* tp_members */
147 0, /* tp_getset */
148 0, /* tp_base */
149 0, /* tp_dict */
150 0, /* tp_descr_get */
151 0, /* tp_descr_set */
152 0, /* tp_dictoffset */
153 0, /* tp_init */
154 0, /* tp_alloc */
155 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000156};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000157
158static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000159static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000161 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162static int symtable_exit_block(struct symtable *st, void *ast);
163static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
164static int symtable_visit_expr(struct symtable *st, expr_ty s);
165static int symtable_visit_genexp(struct symtable *st, expr_ty s);
166static int symtable_visit_arguments(struct symtable *st, arguments_ty);
167static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
168static int symtable_visit_alias(struct symtable *st, alias_ty);
169static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
170static int symtable_visit_keyword(struct symtable *st, keyword_ty);
171static int symtable_visit_slice(struct symtable *st, slice_ty);
172static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
173static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
174static int symtable_implicit_arg(struct symtable *st, int pos);
175
176
Nick Coghlan99b25332005-11-16 12:45:24 +0000177static identifier top = NULL, lambda = NULL, genexpr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178
179#define GET_IDENTIFIER(VAR) \
180 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
181
182#define DUPLICATE_ARGUMENT \
183"duplicate argument '%s' in function definition"
184
185static struct symtable *
186symtable_new(void)
187{
188 struct symtable *st;
189
190 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
191 if (st == NULL)
192 return NULL;
193
194 st->st_filename = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000195 st->st_symbols = NULL;
196
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197 if ((st->st_stack = PyList_New(0)) == NULL)
198 goto fail;
199 if ((st->st_symbols = PyDict_New()) == NULL)
200 goto fail;
201 st->st_cur = NULL;
202 st->st_tmpname = 0;
203 st->st_private = NULL;
204 return st;
205 fail:
206 PySymtable_Free(st);
207 return NULL;
208}
209
210struct symtable *
211PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
212{
213 struct symtable *st = symtable_new();
214 asdl_seq *seq;
215 int i;
216
217 if (st == NULL)
218 return st;
219 st->st_filename = filename;
220 st->st_future = future;
221 symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock,
222 (void *)mod, 0);
223 st->st_top = st->st_cur;
224 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
225 /* Any other top-level initialization? */
226 switch (mod->kind) {
227 case Module_kind:
228 seq = mod->v.Module.body;
229 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000230 if (!symtable_visit_stmt(st,
231 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 goto error;
233 break;
234 case Expression_kind:
235 if (!symtable_visit_expr(st, mod->v.Expression.body))
236 goto error;
237 break;
238 case Interactive_kind:
239 seq = mod->v.Interactive.body;
240 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000241 if (!symtable_visit_stmt(st,
242 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243 goto error;
244 break;
245 case Suite_kind:
246 PyErr_SetString(PyExc_RuntimeError,
247 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000248 goto error;
249 }
250 if (!symtable_exit_block(st, (void *)mod)) {
251 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 return NULL;
253 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254 if (symtable_analyze(st))
255 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000256 PySymtable_Free(st);
257 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000259 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260 PySymtable_Free(st);
261 return NULL;
262}
263
264void
265PySymtable_Free(struct symtable *st)
266{
267 Py_XDECREF(st->st_symbols);
268 Py_XDECREF(st->st_stack);
269 PyMem_Free((void *)st);
270}
271
272PySTEntryObject *
273PySymtable_Lookup(struct symtable *st, void *key)
274{
275 PyObject *k, *v;
276
277 k = PyLong_FromVoidPtr(key);
278 if (k == NULL)
279 return NULL;
280 v = PyDict_GetItem(st->st_symbols, k);
281 if (v) {
282 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284 }
285 else {
286 PyErr_SetString(PyExc_KeyError,
287 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000289
290 Py_DECREF(k);
291 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292}
293
294int
295PyST_GetScope(PySTEntryObject *ste, PyObject *name)
296{
297 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
298 if (!v)
299 return 0;
300 assert(PyInt_Check(v));
301 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
302}
303
304
305/* Analyze raw symbol information to determine scope of each name.
306
307 The next several functions are helpers for PySymtable_Analyze(),
308 which determines whether a name is local, global, or free. In addition,
309 it determines which local variables are cell variables; they provide
310 bindings that are used for free variables in enclosed blocks.
311
312 There are also two kinds of free variables, implicit and explicit. An
313 explicit global is declared with the global statement. An implicit
314 global is a free variable for which the compiler has found no binding
315 in an enclosing function scope. The implicit global is either a global
316 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
317 to handle these names to implement slightly odd semantics. In such a
318 block, the name is treated as global until it is assigned to; then it
319 is treated as a local.
320
321 The symbol table requires two passes to determine the scope of each name.
322 The first pass collects raw facts from the AST: the name is a parameter
323 here, the name is used by not defined here, etc. The second pass analyzes
324 these facts during a pass over the PySTEntryObjects created during pass 1.
325
326 When a function is entered during the second pass, the parent passes
327 the set of all name bindings visible to its children. These bindings
328 are used to determine if the variable is free or an implicit global.
329 After doing the local analysis, it analyzes each of its child blocks
330 using an updated set of name bindings.
331
332 The children update the free variable set. If a local variable is free
333 in a child, the variable is marked as a cell. The current function must
334 provide runtime storage for the variable that may outlive the function's
335 frame. Cell variables are removed from the free set before the analyze
336 function returns to its parent.
337
338 The sets of bound and free variables are implemented as dictionaries
339 mapping strings to None.
340*/
341
342#define SET_SCOPE(DICT, NAME, I) { \
343 PyObject *o = PyInt_FromLong(I); \
344 if (!o) \
345 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000346 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
347 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000349 } \
350 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351}
352
353/* Decide on scope of name, given flags.
354
355 The dicts passed in as arguments are modified as necessary.
356 ste is passed so that flags can be updated.
357*/
358
359static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000360analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361 PyObject *bound, PyObject *local, PyObject *free,
362 PyObject *global)
363{
364 if (flags & DEF_GLOBAL) {
365 if (flags & DEF_PARAM) {
366 PyErr_Format(PyExc_SyntaxError,
367 "name '%s' is local and global",
368 PyString_AS_STRING(name));
369 return 0;
370 }
371 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
372 if (PyDict_SetItem(global, name, Py_None) < 0)
373 return 0;
374 if (bound && PyDict_GetItem(bound, name)) {
375 if (PyDict_DelItem(bound, name) < 0)
376 return 0;
377 }
378 return 1;
379 }
380 if (flags & DEF_BOUND) {
381 SET_SCOPE(dict, name, LOCAL);
382 if (PyDict_SetItem(local, name, Py_None) < 0)
383 return 0;
384 if (PyDict_GetItem(global, name)) {
385 if (PyDict_DelItem(global, name) < 0)
386 return 0;
387 }
388 return 1;
389 }
390 /* If an enclosing block has a binding for this name, it
391 is a free variable rather than a global variable.
392 Note that having a non-NULL bound implies that the block
393 is nested.
394 */
395 if (bound && PyDict_GetItem(bound, name)) {
396 SET_SCOPE(dict, name, FREE);
397 ste->ste_free = 1;
398 if (PyDict_SetItem(free, name, Py_None) < 0)
399 return 0;
400 return 1;
401 }
402 /* If a parent has a global statement, then call it global
403 explicit? It could also be global implicit.
404 */
405 else if (global && PyDict_GetItem(global, name)) {
406 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
407 return 1;
408 }
409 else {
410 if (ste->ste_nested)
411 ste->ste_free = 1;
412 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
413 return 1;
414 }
415 return 0; /* Can't get here */
416}
417
418#undef SET_SCOPE
419
420/* If a name is defined in free and also in locals, then this block
421 provides the binding for the free variable. The name should be
422 marked CELL in this block and removed from the free list.
423
424 Note that the current block's free variables are included in free.
425 That's safe because no name can be free and local in the same scope.
426*/
427
428static int
429analyze_cells(PyObject *scope, PyObject *free)
430{
431 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000432 int success = 0;
433 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434
435 w = PyInt_FromLong(CELL);
436 if (!w)
437 return 0;
438 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000439 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000441 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442 if (flags != LOCAL)
443 continue;
444 if (!PyDict_GetItem(free, name))
445 continue;
446 /* Replace LOCAL with CELL for this name, and remove
447 from free. It is safe to replace the value of name
448 in the dict, because it will not cause a resize.
449 */
450 if (PyDict_SetItem(scope, name, w) < 0)
451 goto error;
452 if (!PyDict_DelItem(free, name) < 0)
453 goto error;
454 }
455 success = 1;
456 error:
457 Py_DECREF(w);
458 return success;
459}
460
461/* Check for illegal statements in unoptimized namespaces */
462static int
463check_unoptimized(const PySTEntryObject* ste) {
464 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000465 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000467 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468 || !(ste->ste_free || ste->ste_child_free))
469 return 1;
470
Armin Rigo31441302005-10-21 12:57:31 +0000471 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472 "contains a nested function with free variables" :
473 "is a nested function");
474
475 switch (ste->ste_unoptimized) {
476 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
477 case OPT_EXEC: /* qualified exec is fine */
478 return 1;
479 case OPT_IMPORT_STAR:
480 PyOS_snprintf(buf, sizeof(buf),
481 "import * is not allowed in function '%.100s' "
482 "because it is %s",
483 PyString_AS_STRING(ste->ste_name), trailer);
484 break;
485 case OPT_BARE_EXEC:
486 PyOS_snprintf(buf, sizeof(buf),
487 "unqualified exec is not allowed in function "
488 "'%.100s' it %s",
489 PyString_AS_STRING(ste->ste_name), trailer);
490 break;
491 default:
492 PyOS_snprintf(buf, sizeof(buf),
493 "function '%.100s' uses import * and bare exec, "
494 "which are illegal because it %s",
495 PyString_AS_STRING(ste->ste_name), trailer);
496 break;
497 }
498
499 PyErr_SetString(PyExc_SyntaxError, buf);
500 PyErr_SyntaxLocation(ste->ste_table->st_filename,
501 ste->ste_opt_lineno);
502 return 0;
503}
504
505/* Enter the final scope information into the st_symbols dict.
506 *
507 * All arguments are dicts. Modifies symbols, others are read-only.
508*/
509static int
510update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000511 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512{
513 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000514 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515
516 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000517 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518 assert(PyInt_Check(v));
519 flags = PyInt_AS_LONG(v);
520 w = PyDict_GetItem(scope, name);
521 assert(w && PyInt_Check(w));
522 i = PyInt_AS_LONG(w);
523 flags |= (i << SCOPE_OFF);
524 u = PyInt_FromLong(flags);
525 if (PyDict_SetItem(symbols, name, u) < 0) {
526 Py_DECREF(u);
527 return 0;
528 }
529 Py_DECREF(u);
530 }
531
532 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
533 if (!free_value)
534 return 0;
535
536 /* add a free variable when it's only use is for creating a closure */
537 pos = 0;
538 while (PyDict_Next(free, &pos, &name, &v)) {
539 PyObject *o = PyDict_GetItem(symbols, name);
540
541 if (o) {
542 /* It could be a free variable in a method of
543 the class that has the same name as a local
544 or global in the class scope.
545 */
Anthony Baxter019aec62006-04-12 04:00:50 +0000546 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000548 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549 o = PyInt_FromLong(i);
550 if (!o) {
551 Py_DECREF(free_value);
552 return 0;
553 }
554 if (PyDict_SetItem(symbols, name, o) < 0) {
555 Py_DECREF(o);
556 Py_DECREF(free_value);
557 return 0;
558 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000559 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560 }
561 /* else it's not free, probably a cell */
562 continue;
563 }
564 if (!PyDict_GetItem(bound, name))
565 continue; /* it's a global */
566
567 if (PyDict_SetItem(symbols, name, free_value) < 0) {
568 Py_DECREF(free_value);
569 return 0;
570 }
571 }
572 Py_DECREF(free_value);
573 return 1;
574}
575
576/* Make final symbol table decisions for block of ste.
577 Arguments:
578 ste -- current symtable entry (input/output)
579 bound -- set of variables bound in enclosing scopes (input)
580 free -- set of free variables in enclosed scopes (output)
581 globals -- set of declared global variables in enclosing scopes (input)
582*/
583
584static int
585analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
586 PyObject *global)
587{
588 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
589 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000590 int i, success = 0;
591 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592
593 local = PyDict_New();
594 if (!local)
595 goto error;
596 scope = PyDict_New();
597 if (!scope)
598 goto error;
599 newglobal = PyDict_New();
600 if (!newglobal)
601 goto error;
602 newfree = PyDict_New();
603 if (!newfree)
604 goto error;
605 newbound = PyDict_New();
606 if (!newbound)
607 goto error;
608
609 if (ste->ste_type == ClassBlock) {
610 /* make a copy of globals before calling analyze_name(),
611 because global statements in the class have no effect
612 on nested functions.
613 */
614 if (PyDict_Update(newglobal, global) < 0)
615 goto error;
616 if (bound)
617 if (PyDict_Update(newbound, bound) < 0)
618 goto error;
619 }
620
621 assert(PySTEntry_Check(ste));
622 assert(PyDict_Check(ste->ste_symbols));
623 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000624 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 if (!analyze_name(ste, scope, name, flags, bound, local, free,
626 global))
627 goto error;
628 }
629
630 if (ste->ste_type != ClassBlock) {
631 if (ste->ste_type == FunctionBlock) {
632 if (PyDict_Update(newbound, local) < 0)
633 goto error;
634 }
635 if (bound) {
636 if (PyDict_Update(newbound, bound) < 0)
637 goto error;
638 }
639 if (PyDict_Update(newglobal, global) < 0)
640 goto error;
641 }
642
643 /* Recursively call analyze_block() on each child block */
644 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
645 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000646 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000648 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649 if (!analyze_block(entry, newbound, newfree, newglobal))
650 goto error;
651 if (entry->ste_free || entry->ste_child_free)
652 ste->ste_child_free = 1;
653 }
654
655 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
656 goto error;
657 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
658 ste->ste_type == ClassBlock))
659 goto error;
660 if (!check_unoptimized(ste))
661 goto error;
662
663 if (PyDict_Update(free, newfree) < 0)
664 goto error;
665 success = 1;
666 error:
667 Py_XDECREF(local);
668 Py_XDECREF(scope);
669 Py_XDECREF(newbound);
670 Py_XDECREF(newglobal);
671 Py_XDECREF(newfree);
672 if (!success)
673 assert(PyErr_Occurred());
674 return success;
675}
676
677static int
678symtable_analyze(struct symtable *st)
679{
680 PyObject *free, *global;
681 int r;
682
683 free = PyDict_New();
684 if (!free)
685 return 0;
686 global = PyDict_New();
687 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000688 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 return 0;
690 }
691 r = analyze_block(st->st_top, NULL, free, global);
692 Py_DECREF(free);
693 Py_DECREF(global);
694 return r;
695}
696
697
698static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000699symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700{
701 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000702 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
704 PyErr_SetString(PyExc_SyntaxError, msg);
705 PyErr_SyntaxLocation(st->st_filename,
706 st->st_cur->ste_lineno);
707 }
708 return 0;
709 }
710 return 1;
711}
712
713/* symtable_enter_block() gets a reference via PySTEntry_New().
714 This reference is released when the block is exited, via the DECREF
715 in symtable_exit_block().
716*/
717
718static int
719symtable_exit_block(struct symtable *st, void *ast)
720{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000721 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722
723 Py_DECREF(st->st_cur);
724 end = PyList_GET_SIZE(st->st_stack) - 1;
725 if (end >= 0) {
726 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
727 end);
728 Py_INCREF(st->st_cur);
729 if (PySequence_DelItem(st->st_stack, end) < 0)
730 return 0;
731 }
732 return 1;
733}
734
735static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000736symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 void *ast, int lineno)
738{
739 PySTEntryObject *prev = NULL;
740
741 if (st->st_cur) {
742 prev = st->st_cur;
743 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 return 0;
745 }
746 Py_DECREF(st->st_cur);
747 }
748 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
749 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++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +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++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +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++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +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++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000897symtable_new_tmpname(struct symtable *st)
898{
899 char tmpname[256];
900 identifier tmp;
901
902 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
903 ++st->st_cur->ste_tmpname);
904 tmp = PyString_InternFromString(tmpname);
905 if (!symtable_add_def(st, tmp, DEF_LOCAL))
906 return 0;
907 Py_DECREF(tmp);
908 return 1;
909}
910
911static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912symtable_visit_stmt(struct symtable *st, stmt_ty s)
913{
914 switch (s->kind) {
915 case FunctionDef_kind:
916 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
917 return 0;
918 if (s->v.FunctionDef.args->defaults)
919 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
920 if (s->v.FunctionDef.decorators)
921 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
922 if (!symtable_enter_block(st, s->v.FunctionDef.name,
923 FunctionBlock, (void *)s, s->lineno))
924 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000925 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
926 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927 if (!symtable_exit_block(st, s))
928 return 0;
929 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000930 case ClassDef_kind: {
931 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
933 return 0;
934 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
935 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
936 (void *)s, s->lineno))
937 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000938 tmp = st->st_private;
939 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000940 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000941 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 if (!symtable_exit_block(st, s))
943 return 0;
944 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000945 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946 case Return_kind:
947 if (s->v.Return.value)
948 VISIT(st, expr, s->v.Return.value);
949 break;
950 case Delete_kind:
951 VISIT_SEQ(st, expr, s->v.Delete.targets);
952 break;
953 case Assign_kind:
954 VISIT_SEQ(st, expr, s->v.Assign.targets);
955 VISIT(st, expr, s->v.Assign.value);
956 break;
957 case AugAssign_kind:
958 VISIT(st, expr, s->v.AugAssign.target);
959 VISIT(st, expr, s->v.AugAssign.value);
960 break;
961 case Print_kind:
962 if (s->v.Print.dest)
963 VISIT(st, expr, s->v.Print.dest);
964 VISIT_SEQ(st, expr, s->v.Print.values);
965 break;
966 case For_kind:
967 VISIT(st, expr, s->v.For.target);
968 VISIT(st, expr, s->v.For.iter);
969 VISIT_SEQ(st, stmt, s->v.For.body);
970 if (s->v.For.orelse)
971 VISIT_SEQ(st, stmt, s->v.For.orelse);
972 break;
973 case While_kind:
974 VISIT(st, expr, s->v.While.test);
975 VISIT_SEQ(st, stmt, s->v.While.body);
976 if (s->v.While.orelse)
977 VISIT_SEQ(st, stmt, s->v.While.orelse);
978 break;
979 case If_kind:
980 /* XXX if 0: and lookup_yield() hacks */
981 VISIT(st, expr, s->v.If.test);
982 VISIT_SEQ(st, stmt, s->v.If.body);
983 if (s->v.If.orelse)
984 VISIT_SEQ(st, stmt, s->v.If.orelse);
985 break;
986 case Raise_kind:
987 if (s->v.Raise.type) {
988 VISIT(st, expr, s->v.Raise.type);
989 if (s->v.Raise.inst) {
990 VISIT(st, expr, s->v.Raise.inst);
991 if (s->v.Raise.tback)
992 VISIT(st, expr, s->v.Raise.tback);
993 }
994 }
995 break;
996 case TryExcept_kind:
997 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
998 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
999 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1000 break;
1001 case TryFinally_kind:
1002 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1003 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1004 break;
1005 case Assert_kind:
1006 VISIT(st, expr, s->v.Assert.test);
1007 if (s->v.Assert.msg)
1008 VISIT(st, expr, s->v.Assert.msg);
1009 break;
1010 case Import_kind:
1011 VISIT_SEQ(st, alias, s->v.Import.names);
1012 /* XXX Don't have the lineno available inside
1013 visit_alias */
1014 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1015 st->st_cur->ste_opt_lineno = s->lineno;
1016 break;
1017 case ImportFrom_kind:
1018 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1019 /* XXX Don't have the lineno available inside
1020 visit_alias */
1021 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1022 st->st_cur->ste_opt_lineno = s->lineno;
1023 break;
1024 case Exec_kind:
1025 VISIT(st, expr, s->v.Exec.body);
1026 if (!st->st_cur->ste_opt_lineno)
1027 st->st_cur->ste_opt_lineno = s->lineno;
1028 if (s->v.Exec.globals) {
1029 st->st_cur->ste_unoptimized |= OPT_EXEC;
1030 VISIT(st, expr, s->v.Exec.globals);
1031 if (s->v.Exec.locals)
1032 VISIT(st, expr, s->v.Exec.locals);
1033 } else {
1034 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1035 }
1036 break;
1037 case Global_kind: {
1038 int i;
1039 asdl_seq *seq = s->v.Global.names;
1040 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001041 identifier name = (identifier)asdl_seq_GET(seq, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001043 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 if (cur < 0)
1045 return 0;
1046 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001047 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048 if (cur & DEF_LOCAL)
1049 PyOS_snprintf(buf, sizeof(buf),
1050 GLOBAL_AFTER_ASSIGN,
1051 c_name);
1052 else
1053 PyOS_snprintf(buf, sizeof(buf),
1054 GLOBAL_AFTER_USE,
1055 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001056 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 return 0;
1058 }
1059 if (!symtable_add_def(st, name, DEF_GLOBAL))
1060 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 break;
1063 }
1064 case Expr_kind:
1065 VISIT(st, expr, s->v.Expr.value);
1066 break;
1067 case Pass_kind:
1068 case Break_kind:
1069 case Continue_kind:
1070 /* nothing to do here */
1071 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001072 case With_kind:
1073 if (!symtable_new_tmpname(st))
1074 return 0;
1075 VISIT(st, expr, s->v.With.context_expr);
1076 if (s->v.With.optional_vars) {
1077 if (!symtable_new_tmpname(st))
1078 return 0;
1079 VISIT(st, expr, s->v.With.optional_vars);
1080 }
1081 VISIT_SEQ(st, stmt, s->v.With.body);
1082 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 }
1084 return 1;
1085}
1086
1087static int
1088symtable_visit_expr(struct symtable *st, expr_ty e)
1089{
1090 switch (e->kind) {
1091 case BoolOp_kind:
1092 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1093 break;
1094 case BinOp_kind:
1095 VISIT(st, expr, e->v.BinOp.left);
1096 VISIT(st, expr, e->v.BinOp.right);
1097 break;
1098 case UnaryOp_kind:
1099 VISIT(st, expr, e->v.UnaryOp.operand);
1100 break;
1101 case Lambda_kind: {
1102 if (!symtable_add_def(st, GET_IDENTIFIER(lambda), DEF_LOCAL))
1103 return 0;
1104 if (e->v.Lambda.args->defaults)
1105 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1106 /* XXX how to get line numbers for expressions */
1107 if (!symtable_enter_block(st, GET_IDENTIFIER(lambda),
1108 FunctionBlock, (void *)e, 0))
1109 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001110 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1111 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 if (!symtable_exit_block(st, (void *)e))
1113 return 0;
1114 break;
1115 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001116 case IfExp_kind:
1117 VISIT(st, expr, e->v.IfExp.test);
1118 VISIT(st, expr, e->v.IfExp.body);
1119 VISIT(st, expr, e->v.IfExp.orelse);
1120 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121 case Dict_kind:
1122 VISIT_SEQ(st, expr, e->v.Dict.keys);
1123 VISIT_SEQ(st, expr, e->v.Dict.values);
1124 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001125 case ListComp_kind:
1126 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 return 0;
1128 VISIT(st, expr, e->v.ListComp.elt);
1129 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1130 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001131 case GeneratorExp_kind:
1132 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 case Yield_kind:
1136 if (e->v.Yield.value)
1137 VISIT(st, expr, e->v.Yield.value);
1138 st->st_cur->ste_generator = 1;
1139 break;
1140 case Compare_kind:
1141 VISIT(st, expr, e->v.Compare.left);
1142 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1143 break;
1144 case Call_kind:
1145 VISIT(st, expr, e->v.Call.func);
1146 VISIT_SEQ(st, expr, e->v.Call.args);
1147 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1148 if (e->v.Call.starargs)
1149 VISIT(st, expr, e->v.Call.starargs);
1150 if (e->v.Call.kwargs)
1151 VISIT(st, expr, e->v.Call.kwargs);
1152 break;
1153 case Repr_kind:
1154 VISIT(st, expr, e->v.Repr.value);
1155 break;
1156 case Num_kind:
1157 case Str_kind:
1158 /* Nothing to do here. */
1159 break;
1160 /* The following exprs can be assignment targets. */
1161 case Attribute_kind:
1162 VISIT(st, expr, e->v.Attribute.value);
1163 break;
1164 case Subscript_kind:
1165 VISIT(st, expr, e->v.Subscript.value);
1166 VISIT(st, slice, e->v.Subscript.slice);
1167 break;
1168 case Name_kind:
1169 if (!symtable_add_def(st, e->v.Name.id,
1170 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1171 return 0;
1172 break;
1173 /* child nodes of List and Tuple will have expr_context set */
1174 case List_kind:
1175 VISIT_SEQ(st, expr, e->v.List.elts);
1176 break;
1177 case Tuple_kind:
1178 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1179 break;
1180 }
1181 return 1;
1182}
1183
1184static int
1185symtable_implicit_arg(struct symtable *st, int pos)
1186{
1187 PyObject *id = PyString_FromFormat(".%d", pos);
1188 if (id == NULL)
1189 return 0;
1190 if (!symtable_add_def(st, id, DEF_PARAM)) {
1191 Py_DECREF(id);
1192 return 0;
1193 }
1194 Py_DECREF(id);
1195 return 1;
1196}
1197
1198static int
1199symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1200{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001201 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202
1203 /* go through all the toplevel arguments first */
1204 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001205 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 if (arg->kind == Name_kind) {
1207 assert(arg->v.Name.ctx == Param ||
1208 (arg->v.Name.ctx == Store && !toplevel));
1209 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1210 return 0;
1211 }
1212 else if (arg->kind == Tuple_kind) {
1213 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 if (toplevel) {
1215 if (!symtable_implicit_arg(st, i))
1216 return 0;
1217 }
1218 }
1219 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001220 PyErr_SetString(PyExc_SyntaxError,
1221 "invalid expression in parameter list");
1222 PyErr_SyntaxLocation(st->st_filename,
1223 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 return 0;
1225 }
1226 }
1227
1228 if (!toplevel) {
1229 if (!symtable_visit_params_nested(st, args))
1230 return 0;
1231 }
1232
1233 return 1;
1234}
1235
1236static int
1237symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1238{
1239 int i;
1240 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001241 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 if (arg->kind == Tuple_kind &&
1243 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1244 return 0;
1245 }
1246
1247 return 1;
1248}
1249
1250static int
1251symtable_visit_arguments(struct symtable *st, arguments_ty a)
1252{
1253 /* skip default arguments inside function block
1254 XXX should ast be different?
1255 */
1256 if (a->args && !symtable_visit_params(st, a->args, 1))
1257 return 0;
1258 if (a->vararg) {
1259 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1260 return 0;
1261 st->st_cur->ste_varargs = 1;
1262 }
1263 if (a->kwarg) {
1264 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1265 return 0;
1266 st->st_cur->ste_varkeywords = 1;
1267 }
1268 if (a->args && !symtable_visit_params_nested(st, a->args))
1269 return 0;
1270 return 1;
1271}
1272
1273
1274static int
1275symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1276{
1277 if (eh->type)
1278 VISIT(st, expr, eh->type);
1279 if (eh->name)
1280 VISIT(st, expr, eh->name);
1281 VISIT_SEQ(st, stmt, eh->body);
1282 return 1;
1283}
1284
1285
1286static int
1287symtable_visit_alias(struct symtable *st, alias_ty a)
1288{
1289 /* Compute store_name, the name actually bound by the import
1290 operation. It is diferent than a->name when a->name is a
1291 dotted package name (e.g. spam.eggs)
1292 */
1293 PyObject *store_name;
1294 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1295 const char *base = PyString_AS_STRING(name);
1296 char *dot = strchr(base, '.');
1297 if (dot)
1298 store_name = PyString_FromStringAndSize(base, dot - base);
1299 else {
1300 store_name = name;
1301 Py_INCREF(store_name);
1302 }
1303 if (strcmp(PyString_AS_STRING(name), "*")) {
1304 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1305 Py_DECREF(store_name);
1306 return r;
1307 }
1308 else {
1309 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001310 int lineno = st->st_cur->ste_lineno;
1311 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001312 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001314 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315 }
1316 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001317 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 return 1;
1319 }
1320}
1321
1322
1323static int
1324symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1325{
1326 VISIT(st, expr, lc->target);
1327 VISIT(st, expr, lc->iter);
1328 VISIT_SEQ(st, expr, lc->ifs);
1329 return 1;
1330}
1331
1332
1333static int
1334symtable_visit_keyword(struct symtable *st, keyword_ty k)
1335{
1336 VISIT(st, expr, k->value);
1337 return 1;
1338}
1339
1340
1341static int
1342symtable_visit_slice(struct symtable *st, slice_ty s)
1343{
1344 switch (s->kind) {
1345 case Slice_kind:
1346 if (s->v.Slice.lower)
1347 VISIT(st, expr, s->v.Slice.lower)
1348 if (s->v.Slice.upper)
1349 VISIT(st, expr, s->v.Slice.upper)
1350 if (s->v.Slice.step)
1351 VISIT(st, expr, s->v.Slice.step)
1352 break;
1353 case ExtSlice_kind:
1354 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1355 break;
1356 case Index_kind:
1357 VISIT(st, expr, s->v.Index.value)
1358 break;
1359 case Ellipsis_kind:
1360 break;
1361 }
1362 return 1;
1363}
1364
1365static int
1366symtable_visit_genexp(struct symtable *st, expr_ty e)
1367{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 comprehension_ty outermost = ((comprehension_ty)
1369 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1370 /* Outermost iterator is evaluated in current scope */
1371 VISIT(st, expr, outermost->iter);
1372 /* Create generator scope for the rest */
Nick Coghlan99b25332005-11-16 12:45:24 +00001373 if (!symtable_enter_block(st, GET_IDENTIFIER(genexpr),
1374 FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 return 0;
1376 }
1377 st->st_cur->ste_generator = 1;
1378 /* Outermost iter is received as an argument */
1379 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001380 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 return 0;
1382 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001383 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1384 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1385 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1386 e->v.GeneratorExp.generators, 1, (void*)e);
1387 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 if (!symtable_exit_block(st, (void *)e))
1389 return 0;
1390 return 1;
1391}