blob: c8eab5807689ef4d933e4e49afe8b34259855c6b [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++)
230 if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
231 goto error;
232 break;
233 case Expression_kind:
234 if (!symtable_visit_expr(st, mod->v.Expression.body))
235 goto error;
236 break;
237 case Interactive_kind:
238 seq = mod->v.Interactive.body;
239 for (i = 0; i < asdl_seq_LEN(seq); i++)
240 if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
241 goto error;
242 break;
243 case Suite_kind:
244 PyErr_SetString(PyExc_RuntimeError,
245 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000246 goto error;
247 }
248 if (!symtable_exit_block(st, (void *)mod)) {
249 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250 return NULL;
251 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 if (symtable_analyze(st))
253 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000254 PySymtable_Free(st);
255 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000257 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 PySymtable_Free(st);
259 return NULL;
260}
261
262void
263PySymtable_Free(struct symtable *st)
264{
265 Py_XDECREF(st->st_symbols);
266 Py_XDECREF(st->st_stack);
267 PyMem_Free((void *)st);
268}
269
270PySTEntryObject *
271PySymtable_Lookup(struct symtable *st, void *key)
272{
273 PyObject *k, *v;
274
275 k = PyLong_FromVoidPtr(key);
276 if (k == NULL)
277 return NULL;
278 v = PyDict_GetItem(st->st_symbols, k);
279 if (v) {
280 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282 }
283 else {
284 PyErr_SetString(PyExc_KeyError,
285 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000287
288 Py_DECREF(k);
289 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290}
291
292int
293PyST_GetScope(PySTEntryObject *ste, PyObject *name)
294{
295 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
296 if (!v)
297 return 0;
298 assert(PyInt_Check(v));
299 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
300}
301
302
303/* Analyze raw symbol information to determine scope of each name.
304
305 The next several functions are helpers for PySymtable_Analyze(),
306 which determines whether a name is local, global, or free. In addition,
307 it determines which local variables are cell variables; they provide
308 bindings that are used for free variables in enclosed blocks.
309
310 There are also two kinds of free variables, implicit and explicit. An
311 explicit global is declared with the global statement. An implicit
312 global is a free variable for which the compiler has found no binding
313 in an enclosing function scope. The implicit global is either a global
314 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
315 to handle these names to implement slightly odd semantics. In such a
316 block, the name is treated as global until it is assigned to; then it
317 is treated as a local.
318
319 The symbol table requires two passes to determine the scope of each name.
320 The first pass collects raw facts from the AST: the name is a parameter
321 here, the name is used by not defined here, etc. The second pass analyzes
322 these facts during a pass over the PySTEntryObjects created during pass 1.
323
324 When a function is entered during the second pass, the parent passes
325 the set of all name bindings visible to its children. These bindings
326 are used to determine if the variable is free or an implicit global.
327 After doing the local analysis, it analyzes each of its child blocks
328 using an updated set of name bindings.
329
330 The children update the free variable set. If a local variable is free
331 in a child, the variable is marked as a cell. The current function must
332 provide runtime storage for the variable that may outlive the function's
333 frame. Cell variables are removed from the free set before the analyze
334 function returns to its parent.
335
336 The sets of bound and free variables are implemented as dictionaries
337 mapping strings to None.
338*/
339
340#define SET_SCOPE(DICT, NAME, I) { \
341 PyObject *o = PyInt_FromLong(I); \
342 if (!o) \
343 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000344 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
345 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000347 } \
348 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349}
350
351/* Decide on scope of name, given flags.
352
353 The dicts passed in as arguments are modified as necessary.
354 ste is passed so that flags can be updated.
355*/
356
357static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000358analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359 PyObject *bound, PyObject *local, PyObject *free,
360 PyObject *global)
361{
362 if (flags & DEF_GLOBAL) {
363 if (flags & DEF_PARAM) {
364 PyErr_Format(PyExc_SyntaxError,
365 "name '%s' is local and global",
366 PyString_AS_STRING(name));
367 return 0;
368 }
369 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
370 if (PyDict_SetItem(global, name, Py_None) < 0)
371 return 0;
372 if (bound && PyDict_GetItem(bound, name)) {
373 if (PyDict_DelItem(bound, name) < 0)
374 return 0;
375 }
376 return 1;
377 }
378 if (flags & DEF_BOUND) {
379 SET_SCOPE(dict, name, LOCAL);
380 if (PyDict_SetItem(local, name, Py_None) < 0)
381 return 0;
382 if (PyDict_GetItem(global, name)) {
383 if (PyDict_DelItem(global, name) < 0)
384 return 0;
385 }
386 return 1;
387 }
388 /* If an enclosing block has a binding for this name, it
389 is a free variable rather than a global variable.
390 Note that having a non-NULL bound implies that the block
391 is nested.
392 */
393 if (bound && PyDict_GetItem(bound, name)) {
394 SET_SCOPE(dict, name, FREE);
395 ste->ste_free = 1;
396 if (PyDict_SetItem(free, name, Py_None) < 0)
397 return 0;
398 return 1;
399 }
400 /* If a parent has a global statement, then call it global
401 explicit? It could also be global implicit.
402 */
403 else if (global && PyDict_GetItem(global, name)) {
404 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
405 return 1;
406 }
407 else {
408 if (ste->ste_nested)
409 ste->ste_free = 1;
410 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
411 return 1;
412 }
413 return 0; /* Can't get here */
414}
415
416#undef SET_SCOPE
417
418/* If a name is defined in free and also in locals, then this block
419 provides the binding for the free variable. The name should be
420 marked CELL in this block and removed from the free list.
421
422 Note that the current block's free variables are included in free.
423 That's safe because no name can be free and local in the same scope.
424*/
425
426static int
427analyze_cells(PyObject *scope, PyObject *free)
428{
429 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000430 int success = 0;
431 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432
433 w = PyInt_FromLong(CELL);
434 if (!w)
435 return 0;
436 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000437 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000438 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000439 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440 if (flags != LOCAL)
441 continue;
442 if (!PyDict_GetItem(free, name))
443 continue;
444 /* Replace LOCAL with CELL for this name, and remove
445 from free. It is safe to replace the value of name
446 in the dict, because it will not cause a resize.
447 */
448 if (PyDict_SetItem(scope, name, w) < 0)
449 goto error;
450 if (!PyDict_DelItem(free, name) < 0)
451 goto error;
452 }
453 success = 1;
454 error:
455 Py_DECREF(w);
456 return success;
457}
458
459/* Check for illegal statements in unoptimized namespaces */
460static int
461check_unoptimized(const PySTEntryObject* ste) {
462 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000463 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000465 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466 || !(ste->ste_free || ste->ste_child_free))
467 return 1;
468
Armin Rigo31441302005-10-21 12:57:31 +0000469 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470 "contains a nested function with free variables" :
471 "is a nested function");
472
473 switch (ste->ste_unoptimized) {
474 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
475 case OPT_EXEC: /* qualified exec is fine */
476 return 1;
477 case OPT_IMPORT_STAR:
478 PyOS_snprintf(buf, sizeof(buf),
479 "import * is not allowed in function '%.100s' "
480 "because it is %s",
481 PyString_AS_STRING(ste->ste_name), trailer);
482 break;
483 case OPT_BARE_EXEC:
484 PyOS_snprintf(buf, sizeof(buf),
485 "unqualified exec is not allowed in function "
486 "'%.100s' it %s",
487 PyString_AS_STRING(ste->ste_name), trailer);
488 break;
489 default:
490 PyOS_snprintf(buf, sizeof(buf),
491 "function '%.100s' uses import * and bare exec, "
492 "which are illegal because it %s",
493 PyString_AS_STRING(ste->ste_name), trailer);
494 break;
495 }
496
497 PyErr_SetString(PyExc_SyntaxError, buf);
498 PyErr_SyntaxLocation(ste->ste_table->st_filename,
499 ste->ste_opt_lineno);
500 return 0;
501}
502
503/* Enter the final scope information into the st_symbols dict.
504 *
505 * All arguments are dicts. Modifies symbols, others are read-only.
506*/
507static int
508update_symbols(PyObject *symbols, PyObject *scope,
509 PyObject *bound, PyObject *free, int class)
510{
511 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000512 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513
514 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000515 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516 assert(PyInt_Check(v));
517 flags = PyInt_AS_LONG(v);
518 w = PyDict_GetItem(scope, name);
519 assert(w && PyInt_Check(w));
520 i = PyInt_AS_LONG(w);
521 flags |= (i << SCOPE_OFF);
522 u = PyInt_FromLong(flags);
523 if (PyDict_SetItem(symbols, name, u) < 0) {
524 Py_DECREF(u);
525 return 0;
526 }
527 Py_DECREF(u);
528 }
529
530 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
531 if (!free_value)
532 return 0;
533
534 /* add a free variable when it's only use is for creating a closure */
535 pos = 0;
536 while (PyDict_Next(free, &pos, &name, &v)) {
537 PyObject *o = PyDict_GetItem(symbols, name);
538
539 if (o) {
540 /* It could be a free variable in a method of
541 the class that has the same name as a local
542 or global in the class scope.
543 */
544 if (class &&
545 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000546 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 o = PyInt_FromLong(i);
548 if (!o) {
549 Py_DECREF(free_value);
550 return 0;
551 }
552 if (PyDict_SetItem(symbols, name, o) < 0) {
553 Py_DECREF(o);
554 Py_DECREF(free_value);
555 return 0;
556 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000557 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000558 }
559 /* else it's not free, probably a cell */
560 continue;
561 }
562 if (!PyDict_GetItem(bound, name))
563 continue; /* it's a global */
564
565 if (PyDict_SetItem(symbols, name, free_value) < 0) {
566 Py_DECREF(free_value);
567 return 0;
568 }
569 }
570 Py_DECREF(free_value);
571 return 1;
572}
573
574/* Make final symbol table decisions for block of ste.
575 Arguments:
576 ste -- current symtable entry (input/output)
577 bound -- set of variables bound in enclosing scopes (input)
578 free -- set of free variables in enclosed scopes (output)
579 globals -- set of declared global variables in enclosing scopes (input)
580*/
581
582static int
583analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
584 PyObject *global)
585{
586 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
587 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000588 int i, success = 0;
589 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
591 local = PyDict_New();
592 if (!local)
593 goto error;
594 scope = PyDict_New();
595 if (!scope)
596 goto error;
597 newglobal = PyDict_New();
598 if (!newglobal)
599 goto error;
600 newfree = PyDict_New();
601 if (!newfree)
602 goto error;
603 newbound = PyDict_New();
604 if (!newbound)
605 goto error;
606
607 if (ste->ste_type == ClassBlock) {
608 /* make a copy of globals before calling analyze_name(),
609 because global statements in the class have no effect
610 on nested functions.
611 */
612 if (PyDict_Update(newglobal, global) < 0)
613 goto error;
614 if (bound)
615 if (PyDict_Update(newbound, bound) < 0)
616 goto error;
617 }
618
619 assert(PySTEntry_Check(ste));
620 assert(PyDict_Check(ste->ste_symbols));
621 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000622 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 if (!analyze_name(ste, scope, name, flags, bound, local, free,
624 global))
625 goto error;
626 }
627
628 if (ste->ste_type != ClassBlock) {
629 if (ste->ste_type == FunctionBlock) {
630 if (PyDict_Update(newbound, local) < 0)
631 goto error;
632 }
633 if (bound) {
634 if (PyDict_Update(newbound, bound) < 0)
635 goto error;
636 }
637 if (PyDict_Update(newglobal, global) < 0)
638 goto error;
639 }
640
641 /* Recursively call analyze_block() on each child block */
642 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
643 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000644 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000646 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 if (!analyze_block(entry, newbound, newfree, newglobal))
648 goto error;
649 if (entry->ste_free || entry->ste_child_free)
650 ste->ste_child_free = 1;
651 }
652
653 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
654 goto error;
655 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
656 ste->ste_type == ClassBlock))
657 goto error;
658 if (!check_unoptimized(ste))
659 goto error;
660
661 if (PyDict_Update(free, newfree) < 0)
662 goto error;
663 success = 1;
664 error:
665 Py_XDECREF(local);
666 Py_XDECREF(scope);
667 Py_XDECREF(newbound);
668 Py_XDECREF(newglobal);
669 Py_XDECREF(newfree);
670 if (!success)
671 assert(PyErr_Occurred());
672 return success;
673}
674
675static int
676symtable_analyze(struct symtable *st)
677{
678 PyObject *free, *global;
679 int r;
680
681 free = PyDict_New();
682 if (!free)
683 return 0;
684 global = PyDict_New();
685 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000686 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 return 0;
688 }
689 r = analyze_block(st->st_top, NULL, free, global);
690 Py_DECREF(free);
691 Py_DECREF(global);
692 return r;
693}
694
695
696static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000697symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698{
699 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000700 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
702 PyErr_SetString(PyExc_SyntaxError, msg);
703 PyErr_SyntaxLocation(st->st_filename,
704 st->st_cur->ste_lineno);
705 }
706 return 0;
707 }
708 return 1;
709}
710
711/* symtable_enter_block() gets a reference via PySTEntry_New().
712 This reference is released when the block is exited, via the DECREF
713 in symtable_exit_block().
714*/
715
716static int
717symtable_exit_block(struct symtable *st, void *ast)
718{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000719 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720
721 Py_DECREF(st->st_cur);
722 end = PyList_GET_SIZE(st->st_stack) - 1;
723 if (end >= 0) {
724 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
725 end);
726 Py_INCREF(st->st_cur);
727 if (PySequence_DelItem(st->st_stack, end) < 0)
728 return 0;
729 }
730 return 1;
731}
732
733static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000734symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735 void *ast, int lineno)
736{
737 PySTEntryObject *prev = NULL;
738
739 if (st->st_cur) {
740 prev = st->st_cur;
741 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742 return 0;
743 }
744 Py_DECREF(st->st_cur);
745 }
746 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
747 if (name == GET_IDENTIFIER(top))
748 st->st_global = st->st_cur->ste_symbols;
749 if (prev) {
750 if (PyList_Append(prev->ste_children,
751 (PyObject *)st->st_cur) < 0) {
752 return 0;
753 }
754 }
755 return 1;
756}
757
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000758static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759symtable_lookup(struct symtable *st, PyObject *name)
760{
761 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000762 PyObject *mangled = _Py_Mangle(st->st_private, name);
763 if (!mangled)
764 return 0;
765 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
766 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 if (!o)
768 return 0;
769 return PyInt_AsLong(o);
770}
771
772static int
773symtable_add_def(struct symtable *st, PyObject *name, int flag)
774{
775 PyObject *o;
776 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000777 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000778 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000780 if (!mangled)
781 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000783 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 val = PyInt_AS_LONG(o);
785 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000786 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
788 PyString_AsString(name));
789 PyErr_SyntaxLocation(st->st_filename,
790 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000791 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 }
793 val |= flag;
794 } else
795 val = flag;
796 o = PyInt_FromLong(val);
797 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000798 goto error;
799 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000801 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 }
803 Py_DECREF(o);
804
805 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000806 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
807 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 } else if (flag & DEF_GLOBAL) {
809 /* XXX need to update DEF_GLOBAL for other flags too;
810 perhaps only DEF_FREE_GLOBAL */
811 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000812 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 val |= PyInt_AS_LONG(o);
814 }
815 o = PyInt_FromLong(val);
816 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000817 goto error;
818 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000820 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 }
822 Py_DECREF(o);
823 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000824 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000826
827error:
828 Py_DECREF(mangled);
829 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830}
831
832/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
833 They use the ASDL name to synthesize the name of the C type and the visit
834 function.
835
836 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
837 useful if the first node in the sequence requires special treatment.
838*/
839
840#define VISIT(ST, TYPE, V) \
841 if (!symtable_visit_ ## TYPE((ST), (V))) \
842 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000843
844#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
845 if (!symtable_visit_ ## TYPE((ST), (V))) { \
846 symtable_exit_block((ST), (S)); \
847 return 0; \
848 }
849
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850#define VISIT_SEQ(ST, TYPE, SEQ) { \
851 int i; \
852 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
853 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
854 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
855 if (!symtable_visit_ ## TYPE((ST), elt)) \
856 return 0; \
857 } \
858}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000859
860#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
861 int i; \
862 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
863 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
864 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
865 if (!symtable_visit_ ## TYPE((ST), elt)) { \
866 symtable_exit_block((ST), (S)); \
867 return 0; \
868 } \
869 } \
870}
871
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
873 int i; \
874 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
875 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
876 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
877 if (!symtable_visit_ ## TYPE((ST), elt)) \
878 return 0; \
879 } \
880}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000881
882#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
883 int i; \
884 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
885 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
886 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
887 if (!symtable_visit_ ## TYPE((ST), elt)) { \
888 symtable_exit_block((ST), (S)); \
889 return 0; \
890 } \
891 } \
892}
893
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000895symtable_new_tmpname(struct symtable *st)
896{
897 char tmpname[256];
898 identifier tmp;
899
900 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
901 ++st->st_cur->ste_tmpname);
902 tmp = PyString_InternFromString(tmpname);
903 if (!symtable_add_def(st, tmp, DEF_LOCAL))
904 return 0;
905 Py_DECREF(tmp);
906 return 1;
907}
908
909static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910symtable_visit_stmt(struct symtable *st, stmt_ty s)
911{
912 switch (s->kind) {
913 case FunctionDef_kind:
914 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
915 return 0;
916 if (s->v.FunctionDef.args->defaults)
917 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
918 if (s->v.FunctionDef.decorators)
919 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
920 if (!symtable_enter_block(st, s->v.FunctionDef.name,
921 FunctionBlock, (void *)s, s->lineno))
922 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000923 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
924 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925 if (!symtable_exit_block(st, s))
926 return 0;
927 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000928 case ClassDef_kind: {
929 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
931 return 0;
932 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
933 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
934 (void *)s, s->lineno))
935 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000936 tmp = st->st_private;
937 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000938 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000939 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 if (!symtable_exit_block(st, s))
941 return 0;
942 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000943 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944 case Return_kind:
945 if (s->v.Return.value)
946 VISIT(st, expr, s->v.Return.value);
947 break;
948 case Delete_kind:
949 VISIT_SEQ(st, expr, s->v.Delete.targets);
950 break;
951 case Assign_kind:
952 VISIT_SEQ(st, expr, s->v.Assign.targets);
953 VISIT(st, expr, s->v.Assign.value);
954 break;
955 case AugAssign_kind:
956 VISIT(st, expr, s->v.AugAssign.target);
957 VISIT(st, expr, s->v.AugAssign.value);
958 break;
959 case Print_kind:
960 if (s->v.Print.dest)
961 VISIT(st, expr, s->v.Print.dest);
962 VISIT_SEQ(st, expr, s->v.Print.values);
963 break;
964 case For_kind:
965 VISIT(st, expr, s->v.For.target);
966 VISIT(st, expr, s->v.For.iter);
967 VISIT_SEQ(st, stmt, s->v.For.body);
968 if (s->v.For.orelse)
969 VISIT_SEQ(st, stmt, s->v.For.orelse);
970 break;
971 case While_kind:
972 VISIT(st, expr, s->v.While.test);
973 VISIT_SEQ(st, stmt, s->v.While.body);
974 if (s->v.While.orelse)
975 VISIT_SEQ(st, stmt, s->v.While.orelse);
976 break;
977 case If_kind:
978 /* XXX if 0: and lookup_yield() hacks */
979 VISIT(st, expr, s->v.If.test);
980 VISIT_SEQ(st, stmt, s->v.If.body);
981 if (s->v.If.orelse)
982 VISIT_SEQ(st, stmt, s->v.If.orelse);
983 break;
984 case Raise_kind:
985 if (s->v.Raise.type) {
986 VISIT(st, expr, s->v.Raise.type);
987 if (s->v.Raise.inst) {
988 VISIT(st, expr, s->v.Raise.inst);
989 if (s->v.Raise.tback)
990 VISIT(st, expr, s->v.Raise.tback);
991 }
992 }
993 break;
994 case TryExcept_kind:
995 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
996 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
997 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
998 break;
999 case TryFinally_kind:
1000 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1001 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1002 break;
1003 case Assert_kind:
1004 VISIT(st, expr, s->v.Assert.test);
1005 if (s->v.Assert.msg)
1006 VISIT(st, expr, s->v.Assert.msg);
1007 break;
1008 case Import_kind:
1009 VISIT_SEQ(st, alias, s->v.Import.names);
1010 /* XXX Don't have the lineno available inside
1011 visit_alias */
1012 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1013 st->st_cur->ste_opt_lineno = s->lineno;
1014 break;
1015 case ImportFrom_kind:
1016 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1017 /* XXX Don't have the lineno available inside
1018 visit_alias */
1019 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1020 st->st_cur->ste_opt_lineno = s->lineno;
1021 break;
1022 case Exec_kind:
1023 VISIT(st, expr, s->v.Exec.body);
1024 if (!st->st_cur->ste_opt_lineno)
1025 st->st_cur->ste_opt_lineno = s->lineno;
1026 if (s->v.Exec.globals) {
1027 st->st_cur->ste_unoptimized |= OPT_EXEC;
1028 VISIT(st, expr, s->v.Exec.globals);
1029 if (s->v.Exec.locals)
1030 VISIT(st, expr, s->v.Exec.locals);
1031 } else {
1032 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1033 }
1034 break;
1035 case Global_kind: {
1036 int i;
1037 asdl_seq *seq = s->v.Global.names;
1038 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1039 identifier name = asdl_seq_GET(seq, i);
1040 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001041 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 if (cur < 0)
1043 return 0;
1044 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001045 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046 if (cur & DEF_LOCAL)
1047 PyOS_snprintf(buf, sizeof(buf),
1048 GLOBAL_AFTER_ASSIGN,
1049 c_name);
1050 else
1051 PyOS_snprintf(buf, sizeof(buf),
1052 GLOBAL_AFTER_USE,
1053 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001054 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 return 0;
1056 }
1057 if (!symtable_add_def(st, name, DEF_GLOBAL))
1058 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 break;
1061 }
1062 case Expr_kind:
1063 VISIT(st, expr, s->v.Expr.value);
1064 break;
1065 case Pass_kind:
1066 case Break_kind:
1067 case Continue_kind:
1068 /* nothing to do here */
1069 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001070 case With_kind:
1071 if (!symtable_new_tmpname(st))
1072 return 0;
1073 VISIT(st, expr, s->v.With.context_expr);
1074 if (s->v.With.optional_vars) {
1075 if (!symtable_new_tmpname(st))
1076 return 0;
1077 VISIT(st, expr, s->v.With.optional_vars);
1078 }
1079 VISIT_SEQ(st, stmt, s->v.With.body);
1080 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 }
1082 return 1;
1083}
1084
1085static int
1086symtable_visit_expr(struct symtable *st, expr_ty e)
1087{
1088 switch (e->kind) {
1089 case BoolOp_kind:
1090 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1091 break;
1092 case BinOp_kind:
1093 VISIT(st, expr, e->v.BinOp.left);
1094 VISIT(st, expr, e->v.BinOp.right);
1095 break;
1096 case UnaryOp_kind:
1097 VISIT(st, expr, e->v.UnaryOp.operand);
1098 break;
1099 case Lambda_kind: {
1100 if (!symtable_add_def(st, GET_IDENTIFIER(lambda), DEF_LOCAL))
1101 return 0;
1102 if (e->v.Lambda.args->defaults)
1103 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1104 /* XXX how to get line numbers for expressions */
1105 if (!symtable_enter_block(st, GET_IDENTIFIER(lambda),
1106 FunctionBlock, (void *)e, 0))
1107 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001108 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1109 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110 if (!symtable_exit_block(st, (void *)e))
1111 return 0;
1112 break;
1113 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001114 case IfExp_kind:
1115 VISIT(st, expr, e->v.IfExp.test);
1116 VISIT(st, expr, e->v.IfExp.body);
1117 VISIT(st, expr, e->v.IfExp.orelse);
1118 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 case Dict_kind:
1120 VISIT_SEQ(st, expr, e->v.Dict.keys);
1121 VISIT_SEQ(st, expr, e->v.Dict.values);
1122 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001123 case ListComp_kind:
1124 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 return 0;
1126 VISIT(st, expr, e->v.ListComp.elt);
1127 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1128 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001129 case GeneratorExp_kind:
1130 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 case Yield_kind:
1134 if (e->v.Yield.value)
1135 VISIT(st, expr, e->v.Yield.value);
1136 st->st_cur->ste_generator = 1;
1137 break;
1138 case Compare_kind:
1139 VISIT(st, expr, e->v.Compare.left);
1140 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1141 break;
1142 case Call_kind:
1143 VISIT(st, expr, e->v.Call.func);
1144 VISIT_SEQ(st, expr, e->v.Call.args);
1145 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1146 if (e->v.Call.starargs)
1147 VISIT(st, expr, e->v.Call.starargs);
1148 if (e->v.Call.kwargs)
1149 VISIT(st, expr, e->v.Call.kwargs);
1150 break;
1151 case Repr_kind:
1152 VISIT(st, expr, e->v.Repr.value);
1153 break;
1154 case Num_kind:
1155 case Str_kind:
1156 /* Nothing to do here. */
1157 break;
1158 /* The following exprs can be assignment targets. */
1159 case Attribute_kind:
1160 VISIT(st, expr, e->v.Attribute.value);
1161 break;
1162 case Subscript_kind:
1163 VISIT(st, expr, e->v.Subscript.value);
1164 VISIT(st, slice, e->v.Subscript.slice);
1165 break;
1166 case Name_kind:
1167 if (!symtable_add_def(st, e->v.Name.id,
1168 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1169 return 0;
1170 break;
1171 /* child nodes of List and Tuple will have expr_context set */
1172 case List_kind:
1173 VISIT_SEQ(st, expr, e->v.List.elts);
1174 break;
1175 case Tuple_kind:
1176 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1177 break;
1178 }
1179 return 1;
1180}
1181
1182static int
1183symtable_implicit_arg(struct symtable *st, int pos)
1184{
1185 PyObject *id = PyString_FromFormat(".%d", pos);
1186 if (id == NULL)
1187 return 0;
1188 if (!symtable_add_def(st, id, DEF_PARAM)) {
1189 Py_DECREF(id);
1190 return 0;
1191 }
1192 Py_DECREF(id);
1193 return 1;
1194}
1195
1196static int
1197symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1198{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001199 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200
1201 /* go through all the toplevel arguments first */
1202 for (i = 0; i < asdl_seq_LEN(args); i++) {
1203 expr_ty arg = asdl_seq_GET(args, i);
1204 if (arg->kind == Name_kind) {
1205 assert(arg->v.Name.ctx == Param ||
1206 (arg->v.Name.ctx == Store && !toplevel));
1207 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1208 return 0;
1209 }
1210 else if (arg->kind == Tuple_kind) {
1211 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 if (toplevel) {
1213 if (!symtable_implicit_arg(st, i))
1214 return 0;
1215 }
1216 }
1217 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001218 PyErr_SetString(PyExc_SyntaxError,
1219 "invalid expression in parameter list");
1220 PyErr_SyntaxLocation(st->st_filename,
1221 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 return 0;
1223 }
1224 }
1225
1226 if (!toplevel) {
1227 if (!symtable_visit_params_nested(st, args))
1228 return 0;
1229 }
1230
1231 return 1;
1232}
1233
1234static int
1235symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1236{
1237 int i;
1238 for (i = 0; i < asdl_seq_LEN(args); i++) {
1239 expr_ty arg = asdl_seq_GET(args, i);
1240 if (arg->kind == Tuple_kind &&
1241 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1242 return 0;
1243 }
1244
1245 return 1;
1246}
1247
1248static int
1249symtable_visit_arguments(struct symtable *st, arguments_ty a)
1250{
1251 /* skip default arguments inside function block
1252 XXX should ast be different?
1253 */
1254 if (a->args && !symtable_visit_params(st, a->args, 1))
1255 return 0;
1256 if (a->vararg) {
1257 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1258 return 0;
1259 st->st_cur->ste_varargs = 1;
1260 }
1261 if (a->kwarg) {
1262 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1263 return 0;
1264 st->st_cur->ste_varkeywords = 1;
1265 }
1266 if (a->args && !symtable_visit_params_nested(st, a->args))
1267 return 0;
1268 return 1;
1269}
1270
1271
1272static int
1273symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1274{
1275 if (eh->type)
1276 VISIT(st, expr, eh->type);
1277 if (eh->name)
1278 VISIT(st, expr, eh->name);
1279 VISIT_SEQ(st, stmt, eh->body);
1280 return 1;
1281}
1282
1283
1284static int
1285symtable_visit_alias(struct symtable *st, alias_ty a)
1286{
1287 /* Compute store_name, the name actually bound by the import
1288 operation. It is diferent than a->name when a->name is a
1289 dotted package name (e.g. spam.eggs)
1290 */
1291 PyObject *store_name;
1292 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1293 const char *base = PyString_AS_STRING(name);
1294 char *dot = strchr(base, '.');
1295 if (dot)
1296 store_name = PyString_FromStringAndSize(base, dot - base);
1297 else {
1298 store_name = name;
1299 Py_INCREF(store_name);
1300 }
1301 if (strcmp(PyString_AS_STRING(name), "*")) {
1302 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1303 Py_DECREF(store_name);
1304 return r;
1305 }
1306 else {
1307 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001308 int lineno = st->st_cur->ste_lineno;
1309 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001310 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001312 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 }
1314 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001315 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 return 1;
1317 }
1318}
1319
1320
1321static int
1322symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1323{
1324 VISIT(st, expr, lc->target);
1325 VISIT(st, expr, lc->iter);
1326 VISIT_SEQ(st, expr, lc->ifs);
1327 return 1;
1328}
1329
1330
1331static int
1332symtable_visit_keyword(struct symtable *st, keyword_ty k)
1333{
1334 VISIT(st, expr, k->value);
1335 return 1;
1336}
1337
1338
1339static int
1340symtable_visit_slice(struct symtable *st, slice_ty s)
1341{
1342 switch (s->kind) {
1343 case Slice_kind:
1344 if (s->v.Slice.lower)
1345 VISIT(st, expr, s->v.Slice.lower)
1346 if (s->v.Slice.upper)
1347 VISIT(st, expr, s->v.Slice.upper)
1348 if (s->v.Slice.step)
1349 VISIT(st, expr, s->v.Slice.step)
1350 break;
1351 case ExtSlice_kind:
1352 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1353 break;
1354 case Index_kind:
1355 VISIT(st, expr, s->v.Index.value)
1356 break;
1357 case Ellipsis_kind:
1358 break;
1359 }
1360 return 1;
1361}
1362
1363static int
1364symtable_visit_genexp(struct symtable *st, expr_ty e)
1365{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366 comprehension_ty outermost = ((comprehension_ty)
1367 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1368 /* Outermost iterator is evaluated in current scope */
1369 VISIT(st, expr, outermost->iter);
1370 /* Create generator scope for the rest */
Nick Coghlan99b25332005-11-16 12:45:24 +00001371 if (!symtable_enter_block(st, GET_IDENTIFIER(genexpr),
1372 FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 return 0;
1374 }
1375 st->st_cur->ste_generator = 1;
1376 /* Outermost iter is received as an argument */
1377 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001378 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 return 0;
1380 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001381 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1382 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1383 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1384 e->v.GeneratorExp.generators, 1, (void*)e);
1385 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 if (!symtable_exit_block(st, (void *)e))
1387 return 0;
1388 return 1;
1389}