blob: 7f3f5db9e1b1deb039314841682e135a0664c6e3 [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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017PySTEntryObject *
Neal Norwitz62c2fac2005-10-24 00:30:44 +000018PySTEntry_New(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000019 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000020{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021 PySTEntryObject *ste = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000022 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000023
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024 k = PyLong_FromVoidPtr(key);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000025 if (k == NULL)
26 goto fail;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027 ste = (PySTEntryObject *)PyObject_New(PySTEntryObject,
28 &PySTEntry_Type);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000029 ste->ste_table = st;
30 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000031 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000033 ste->ste_name = name;
34 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000035
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000036 ste->ste_symbols = NULL;
37 ste->ste_varnames = NULL;
38 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000039
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000040 ste->ste_symbols = PyDict_New();
41 if (ste->ste_symbols == NULL)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000043
44 ste->ste_varnames = PyList_New(0);
45 if (ste->ste_varnames == NULL)
46 goto fail;
47
48 ste->ste_children = PyList_New(0);
49 if (ste->ste_children == NULL)
50 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052 ste->ste_type = block;
53 ste->ste_unoptimized = 0;
54 ste->ste_nested = 0;
55 ste->ste_free = 0;
56 ste->ste_varargs = 0;
57 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000058 ste->ste_opt_lineno = 0;
Jeremy Hylton4d508ad2003-05-21 17:34:50 +000059 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000060 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000062 if (st->st_cur != NULL &&
63 (st->st_cur->ste_nested ||
64 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000065 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000066 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000067 ste->ste_generator = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000068
69 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
70 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000071
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000072 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000073 fail:
74 Py_XDECREF(ste);
75 return NULL;
76}
77
78static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000079ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000080{
81 char buf[256];
82
Barry Warsaw4b4ab202001-11-28 21:36:28 +000083 PyOS_snprintf(buf, sizeof(buf),
84 "<symtable entry %.100s(%ld), line %d>",
85 PyString_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000087 return PyString_FromString(buf);
88}
89
90static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092{
93 ste->ste_table = NULL;
94 Py_XDECREF(ste->ste_id);
95 Py_XDECREF(ste->ste_name);
96 Py_XDECREF(ste->ste_symbols);
97 Py_XDECREF(ste->ste_varnames);
98 Py_XDECREF(ste->ste_children);
99 PyObject_Del(ste);
100}
101
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000103
Guido van Rossum6f799372001-09-20 20:46:19 +0000104static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000105 {"id", T_OBJECT, OFF(ste_id), READONLY},
106 {"name", T_OBJECT, OFF(ste_name), READONLY},
107 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
108 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
109 {"children", T_OBJECT, OFF(ste_children), READONLY},
110 {"type", T_INT, OFF(ste_type), READONLY},
111 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000112 {NULL}
113};
114
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000115PyTypeObject PySTEntry_Type = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000116 PyObject_HEAD_INIT(&PyType_Type)
117 0,
118 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120 0,
121 (destructor)ste_dealloc, /* tp_dealloc */
122 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000123 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124 0, /* tp_setattr */
125 0, /* tp_compare */
126 (reprfunc)ste_repr, /* tp_repr */
127 0, /* tp_as_number */
128 0, /* tp_as_sequence */
129 0, /* tp_as_mapping */
130 0, /* tp_hash */
131 0, /* tp_call */
132 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000133 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000134 0, /* tp_setattro */
135 0, /* tp_as_buffer */
136 Py_TPFLAGS_DEFAULT, /* tp_flags */
137 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000138 0, /* tp_traverse */
139 0, /* tp_clear */
140 0, /* tp_richcompare */
141 0, /* tp_weaklistoffset */
142 0, /* tp_iter */
143 0, /* tp_iternext */
144 0, /* tp_methods */
145 ste_memberlist, /* tp_members */
146 0, /* tp_getset */
147 0, /* tp_base */
148 0, /* tp_dict */
149 0, /* tp_descr_get */
150 0, /* tp_descr_set */
151 0, /* tp_dictoffset */
152 0, /* tp_init */
153 0, /* tp_alloc */
154 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000155};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000156
157static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000158static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000159static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000160 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161static int symtable_exit_block(struct symtable *st, void *ast);
162static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
163static int symtable_visit_expr(struct symtable *st, expr_ty s);
164static int symtable_visit_genexp(struct symtable *st, expr_ty s);
165static int symtable_visit_arguments(struct symtable *st, arguments_ty);
166static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
167static int symtable_visit_alias(struct symtable *st, alias_ty);
168static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
169static int symtable_visit_keyword(struct symtable *st, keyword_ty);
170static int symtable_visit_slice(struct symtable *st, slice_ty);
171static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
172static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
173static int symtable_implicit_arg(struct symtable *st, int pos);
174
175
Nick Coghlan99b25332005-11-16 12:45:24 +0000176static identifier top = NULL, lambda = NULL, genexpr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177
178#define GET_IDENTIFIER(VAR) \
179 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
180
181#define DUPLICATE_ARGUMENT \
182"duplicate argument '%s' in function definition"
183
184static struct symtable *
185symtable_new(void)
186{
187 struct symtable *st;
188
189 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
190 if (st == NULL)
191 return NULL;
192
193 st->st_filename = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000194 st->st_symbols = NULL;
195
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 if ((st->st_stack = PyList_New(0)) == NULL)
197 goto fail;
198 if ((st->st_symbols = PyDict_New()) == NULL)
199 goto fail;
200 st->st_cur = NULL;
201 st->st_tmpname = 0;
202 st->st_private = NULL;
203 return st;
204 fail:
205 PySymtable_Free(st);
206 return NULL;
207}
208
209struct symtable *
210PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
211{
212 struct symtable *st = symtable_new();
213 asdl_seq *seq;
214 int i;
215
216 if (st == NULL)
217 return st;
218 st->st_filename = filename;
219 st->st_future = future;
220 symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock,
221 (void *)mod, 0);
222 st->st_top = st->st_cur;
223 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
224 /* Any other top-level initialization? */
225 switch (mod->kind) {
226 case Module_kind:
227 seq = mod->v.Module.body;
228 for (i = 0; i < asdl_seq_LEN(seq); i++)
229 if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
230 goto error;
231 break;
232 case Expression_kind:
233 if (!symtable_visit_expr(st, mod->v.Expression.body))
234 goto error;
235 break;
236 case Interactive_kind:
237 seq = mod->v.Interactive.body;
238 for (i = 0; i < asdl_seq_LEN(seq); i++)
239 if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
240 goto error;
241 break;
242 case Suite_kind:
243 PyErr_SetString(PyExc_RuntimeError,
244 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000245 goto error;
246 }
247 if (!symtable_exit_block(st, (void *)mod)) {
248 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249 return NULL;
250 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000251 if (symtable_analyze(st))
252 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000253 PySymtable_Free(st);
254 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000256 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257 PySymtable_Free(st);
258 return NULL;
259}
260
261void
262PySymtable_Free(struct symtable *st)
263{
264 Py_XDECREF(st->st_symbols);
265 Py_XDECREF(st->st_stack);
266 PyMem_Free((void *)st);
267}
268
269PySTEntryObject *
270PySymtable_Lookup(struct symtable *st, void *key)
271{
272 PyObject *k, *v;
273
274 k = PyLong_FromVoidPtr(key);
275 if (k == NULL)
276 return NULL;
277 v = PyDict_GetItem(st->st_symbols, k);
278 if (v) {
279 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281 }
282 else {
283 PyErr_SetString(PyExc_KeyError,
284 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000286
287 Py_DECREF(k);
288 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289}
290
291int
292PyST_GetScope(PySTEntryObject *ste, PyObject *name)
293{
294 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
295 if (!v)
296 return 0;
297 assert(PyInt_Check(v));
298 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
299}
300
301
302/* Analyze raw symbol information to determine scope of each name.
303
304 The next several functions are helpers for PySymtable_Analyze(),
305 which determines whether a name is local, global, or free. In addition,
306 it determines which local variables are cell variables; they provide
307 bindings that are used for free variables in enclosed blocks.
308
309 There are also two kinds of free variables, implicit and explicit. An
310 explicit global is declared with the global statement. An implicit
311 global is a free variable for which the compiler has found no binding
312 in an enclosing function scope. The implicit global is either a global
313 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
314 to handle these names to implement slightly odd semantics. In such a
315 block, the name is treated as global until it is assigned to; then it
316 is treated as a local.
317
318 The symbol table requires two passes to determine the scope of each name.
319 The first pass collects raw facts from the AST: the name is a parameter
320 here, the name is used by not defined here, etc. The second pass analyzes
321 these facts during a pass over the PySTEntryObjects created during pass 1.
322
323 When a function is entered during the second pass, the parent passes
324 the set of all name bindings visible to its children. These bindings
325 are used to determine if the variable is free or an implicit global.
326 After doing the local analysis, it analyzes each of its child blocks
327 using an updated set of name bindings.
328
329 The children update the free variable set. If a local variable is free
330 in a child, the variable is marked as a cell. The current function must
331 provide runtime storage for the variable that may outlive the function's
332 frame. Cell variables are removed from the free set before the analyze
333 function returns to its parent.
334
335 The sets of bound and free variables are implemented as dictionaries
336 mapping strings to None.
337*/
338
339#define SET_SCOPE(DICT, NAME, I) { \
340 PyObject *o = PyInt_FromLong(I); \
341 if (!o) \
342 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000343 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
344 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000346 } \
347 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348}
349
350/* Decide on scope of name, given flags.
351
352 The dicts passed in as arguments are modified as necessary.
353 ste is passed so that flags can be updated.
354*/
355
356static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000357analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358 PyObject *bound, PyObject *local, PyObject *free,
359 PyObject *global)
360{
361 if (flags & DEF_GLOBAL) {
362 if (flags & DEF_PARAM) {
363 PyErr_Format(PyExc_SyntaxError,
364 "name '%s' is local and global",
365 PyString_AS_STRING(name));
366 return 0;
367 }
368 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
369 if (PyDict_SetItem(global, name, Py_None) < 0)
370 return 0;
371 if (bound && PyDict_GetItem(bound, name)) {
372 if (PyDict_DelItem(bound, name) < 0)
373 return 0;
374 }
375 return 1;
376 }
377 if (flags & DEF_BOUND) {
378 SET_SCOPE(dict, name, LOCAL);
379 if (PyDict_SetItem(local, name, Py_None) < 0)
380 return 0;
381 if (PyDict_GetItem(global, name)) {
382 if (PyDict_DelItem(global, name) < 0)
383 return 0;
384 }
385 return 1;
386 }
387 /* If an enclosing block has a binding for this name, it
388 is a free variable rather than a global variable.
389 Note that having a non-NULL bound implies that the block
390 is nested.
391 */
392 if (bound && PyDict_GetItem(bound, name)) {
393 SET_SCOPE(dict, name, FREE);
394 ste->ste_free = 1;
395 if (PyDict_SetItem(free, name, Py_None) < 0)
396 return 0;
397 return 1;
398 }
399 /* If a parent has a global statement, then call it global
400 explicit? It could also be global implicit.
401 */
402 else if (global && PyDict_GetItem(global, name)) {
403 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
404 return 1;
405 }
406 else {
407 if (ste->ste_nested)
408 ste->ste_free = 1;
409 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
410 return 1;
411 }
412 return 0; /* Can't get here */
413}
414
415#undef SET_SCOPE
416
417/* If a name is defined in free and also in locals, then this block
418 provides the binding for the free variable. The name should be
419 marked CELL in this block and removed from the free list.
420
421 Note that the current block's free variables are included in free.
422 That's safe because no name can be free and local in the same scope.
423*/
424
425static int
426analyze_cells(PyObject *scope, PyObject *free)
427{
428 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000429 int success = 0;
430 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431
432 w = PyInt_FromLong(CELL);
433 if (!w)
434 return 0;
435 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000436 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000438 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000439 if (flags != LOCAL)
440 continue;
441 if (!PyDict_GetItem(free, name))
442 continue;
443 /* Replace LOCAL with CELL for this name, and remove
444 from free. It is safe to replace the value of name
445 in the dict, because it will not cause a resize.
446 */
447 if (PyDict_SetItem(scope, name, w) < 0)
448 goto error;
449 if (!PyDict_DelItem(free, name) < 0)
450 goto error;
451 }
452 success = 1;
453 error:
454 Py_DECREF(w);
455 return success;
456}
457
458/* Check for illegal statements in unoptimized namespaces */
459static int
460check_unoptimized(const PySTEntryObject* ste) {
461 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000462 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000464 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465 || !(ste->ste_free || ste->ste_child_free))
466 return 1;
467
Armin Rigo31441302005-10-21 12:57:31 +0000468 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469 "contains a nested function with free variables" :
470 "is a nested function");
471
472 switch (ste->ste_unoptimized) {
473 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
474 case OPT_EXEC: /* qualified exec is fine */
475 return 1;
476 case OPT_IMPORT_STAR:
477 PyOS_snprintf(buf, sizeof(buf),
478 "import * is not allowed in function '%.100s' "
479 "because it is %s",
480 PyString_AS_STRING(ste->ste_name), trailer);
481 break;
482 case OPT_BARE_EXEC:
483 PyOS_snprintf(buf, sizeof(buf),
484 "unqualified exec is not allowed in function "
485 "'%.100s' it %s",
486 PyString_AS_STRING(ste->ste_name), trailer);
487 break;
488 default:
489 PyOS_snprintf(buf, sizeof(buf),
490 "function '%.100s' uses import * and bare exec, "
491 "which are illegal because it %s",
492 PyString_AS_STRING(ste->ste_name), trailer);
493 break;
494 }
495
496 PyErr_SetString(PyExc_SyntaxError, buf);
497 PyErr_SyntaxLocation(ste->ste_table->st_filename,
498 ste->ste_opt_lineno);
499 return 0;
500}
501
502/* Enter the final scope information into the st_symbols dict.
503 *
504 * All arguments are dicts. Modifies symbols, others are read-only.
505*/
506static int
507update_symbols(PyObject *symbols, PyObject *scope,
508 PyObject *bound, PyObject *free, int class)
509{
510 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000511 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512
513 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000514 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 assert(PyInt_Check(v));
516 flags = PyInt_AS_LONG(v);
517 w = PyDict_GetItem(scope, name);
518 assert(w && PyInt_Check(w));
519 i = PyInt_AS_LONG(w);
520 flags |= (i << SCOPE_OFF);
521 u = PyInt_FromLong(flags);
522 if (PyDict_SetItem(symbols, name, u) < 0) {
523 Py_DECREF(u);
524 return 0;
525 }
526 Py_DECREF(u);
527 }
528
529 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
530 if (!free_value)
531 return 0;
532
533 /* add a free variable when it's only use is for creating a closure */
534 pos = 0;
535 while (PyDict_Next(free, &pos, &name, &v)) {
536 PyObject *o = PyDict_GetItem(symbols, name);
537
538 if (o) {
539 /* It could be a free variable in a method of
540 the class that has the same name as a local
541 or global in the class scope.
542 */
543 if (class &&
544 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000545 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546 o = PyInt_FromLong(i);
547 if (!o) {
548 Py_DECREF(free_value);
549 return 0;
550 }
551 if (PyDict_SetItem(symbols, name, o) < 0) {
552 Py_DECREF(o);
553 Py_DECREF(free_value);
554 return 0;
555 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000556 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557 }
558 /* else it's not free, probably a cell */
559 continue;
560 }
561 if (!PyDict_GetItem(bound, name))
562 continue; /* it's a global */
563
564 if (PyDict_SetItem(symbols, name, free_value) < 0) {
565 Py_DECREF(free_value);
566 return 0;
567 }
568 }
569 Py_DECREF(free_value);
570 return 1;
571}
572
573/* Make final symbol table decisions for block of ste.
574 Arguments:
575 ste -- current symtable entry (input/output)
576 bound -- set of variables bound in enclosing scopes (input)
577 free -- set of free variables in enclosed scopes (output)
578 globals -- set of declared global variables in enclosing scopes (input)
579*/
580
581static int
582analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
583 PyObject *global)
584{
585 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
586 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000587 int i, success = 0;
588 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589
590 local = PyDict_New();
591 if (!local)
592 goto error;
593 scope = PyDict_New();
594 if (!scope)
595 goto error;
596 newglobal = PyDict_New();
597 if (!newglobal)
598 goto error;
599 newfree = PyDict_New();
600 if (!newfree)
601 goto error;
602 newbound = PyDict_New();
603 if (!newbound)
604 goto error;
605
606 if (ste->ste_type == ClassBlock) {
607 /* make a copy of globals before calling analyze_name(),
608 because global statements in the class have no effect
609 on nested functions.
610 */
611 if (PyDict_Update(newglobal, global) < 0)
612 goto error;
613 if (bound)
614 if (PyDict_Update(newbound, bound) < 0)
615 goto error;
616 }
617
618 assert(PySTEntry_Check(ste));
619 assert(PyDict_Check(ste->ste_symbols));
620 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000621 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 if (!analyze_name(ste, scope, name, flags, bound, local, free,
623 global))
624 goto error;
625 }
626
627 if (ste->ste_type != ClassBlock) {
628 if (ste->ste_type == FunctionBlock) {
629 if (PyDict_Update(newbound, local) < 0)
630 goto error;
631 }
632 if (bound) {
633 if (PyDict_Update(newbound, bound) < 0)
634 goto error;
635 }
636 if (PyDict_Update(newglobal, global) < 0)
637 goto error;
638 }
639
640 /* Recursively call analyze_block() on each child block */
641 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
642 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000643 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000645 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 if (!analyze_block(entry, newbound, newfree, newglobal))
647 goto error;
648 if (entry->ste_free || entry->ste_child_free)
649 ste->ste_child_free = 1;
650 }
651
652 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
653 goto error;
654 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
655 ste->ste_type == ClassBlock))
656 goto error;
657 if (!check_unoptimized(ste))
658 goto error;
659
660 if (PyDict_Update(free, newfree) < 0)
661 goto error;
662 success = 1;
663 error:
664 Py_XDECREF(local);
665 Py_XDECREF(scope);
666 Py_XDECREF(newbound);
667 Py_XDECREF(newglobal);
668 Py_XDECREF(newfree);
669 if (!success)
670 assert(PyErr_Occurred());
671 return success;
672}
673
674static int
675symtable_analyze(struct symtable *st)
676{
677 PyObject *free, *global;
678 int r;
679
680 free = PyDict_New();
681 if (!free)
682 return 0;
683 global = PyDict_New();
684 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000685 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 return 0;
687 }
688 r = analyze_block(st->st_top, NULL, free, global);
689 Py_DECREF(free);
690 Py_DECREF(global);
691 return r;
692}
693
694
695static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000696symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697{
698 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000699 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
701 PyErr_SetString(PyExc_SyntaxError, msg);
702 PyErr_SyntaxLocation(st->st_filename,
703 st->st_cur->ste_lineno);
704 }
705 return 0;
706 }
707 return 1;
708}
709
710/* symtable_enter_block() gets a reference via PySTEntry_New().
711 This reference is released when the block is exited, via the DECREF
712 in symtable_exit_block().
713*/
714
715static int
716symtable_exit_block(struct symtable *st, void *ast)
717{
718 int end;
719
720 Py_DECREF(st->st_cur);
721 end = PyList_GET_SIZE(st->st_stack) - 1;
722 if (end >= 0) {
723 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
724 end);
725 Py_INCREF(st->st_cur);
726 if (PySequence_DelItem(st->st_stack, end) < 0)
727 return 0;
728 }
729 return 1;
730}
731
732static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000733symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 void *ast, int lineno)
735{
736 PySTEntryObject *prev = NULL;
737
738 if (st->st_cur) {
739 prev = st->st_cur;
740 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741 return 0;
742 }
743 Py_DECREF(st->st_cur);
744 }
745 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
746 if (name == GET_IDENTIFIER(top))
747 st->st_global = st->st_cur->ste_symbols;
748 if (prev) {
749 if (PyList_Append(prev->ste_children,
750 (PyObject *)st->st_cur) < 0) {
751 return 0;
752 }
753 }
754 return 1;
755}
756
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000757static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758symtable_lookup(struct symtable *st, PyObject *name)
759{
760 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000761 PyObject *mangled = _Py_Mangle(st->st_private, name);
762 if (!mangled)
763 return 0;
764 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
765 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 if (!o)
767 return 0;
768 return PyInt_AsLong(o);
769}
770
771static int
772symtable_add_def(struct symtable *st, PyObject *name, int flag)
773{
774 PyObject *o;
775 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000776 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000777 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000779 if (!mangled)
780 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000782 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 val = PyInt_AS_LONG(o);
784 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000785 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
787 PyString_AsString(name));
788 PyErr_SyntaxLocation(st->st_filename,
789 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000790 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 }
792 val |= flag;
793 } else
794 val = flag;
795 o = PyInt_FromLong(val);
796 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000797 goto error;
798 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000800 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 }
802 Py_DECREF(o);
803
804 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000805 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
806 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 } else if (flag & DEF_GLOBAL) {
808 /* XXX need to update DEF_GLOBAL for other flags too;
809 perhaps only DEF_FREE_GLOBAL */
810 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000811 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 val |= PyInt_AS_LONG(o);
813 }
814 o = PyInt_FromLong(val);
815 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000816 goto error;
817 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000819 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 }
821 Py_DECREF(o);
822 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000823 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000825
826error:
827 Py_DECREF(mangled);
828 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829}
830
831/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
832 They use the ASDL name to synthesize the name of the C type and the visit
833 function.
834
835 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
836 useful if the first node in the sequence requires special treatment.
837*/
838
839#define VISIT(ST, TYPE, V) \
840 if (!symtable_visit_ ## TYPE((ST), (V))) \
841 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000842
843#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
844 if (!symtable_visit_ ## TYPE((ST), (V))) { \
845 symtable_exit_block((ST), (S)); \
846 return 0; \
847 }
848
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849#define VISIT_SEQ(ST, TYPE, SEQ) { \
850 int i; \
851 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
852 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
853 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
854 if (!symtable_visit_ ## TYPE((ST), elt)) \
855 return 0; \
856 } \
857}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000858
859#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
860 int i; \
861 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
862 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
863 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
864 if (!symtable_visit_ ## TYPE((ST), elt)) { \
865 symtable_exit_block((ST), (S)); \
866 return 0; \
867 } \
868 } \
869}
870
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
872 int i; \
873 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
874 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
875 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
876 if (!symtable_visit_ ## TYPE((ST), elt)) \
877 return 0; \
878 } \
879}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000880
881#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
882 int i; \
883 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
884 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
885 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
886 if (!symtable_visit_ ## TYPE((ST), elt)) { \
887 symtable_exit_block((ST), (S)); \
888 return 0; \
889 } \
890 } \
891}
892
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893static int
894symtable_visit_stmt(struct symtable *st, stmt_ty s)
895{
896 switch (s->kind) {
897 case FunctionDef_kind:
898 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
899 return 0;
900 if (s->v.FunctionDef.args->defaults)
901 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
902 if (s->v.FunctionDef.decorators)
903 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
904 if (!symtable_enter_block(st, s->v.FunctionDef.name,
905 FunctionBlock, (void *)s, s->lineno))
906 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000907 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
908 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909 if (!symtable_exit_block(st, s))
910 return 0;
911 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000912 case ClassDef_kind: {
913 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
915 return 0;
916 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
917 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
918 (void *)s, s->lineno))
919 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000920 tmp = st->st_private;
921 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000922 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000923 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 if (!symtable_exit_block(st, s))
925 return 0;
926 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000927 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928 case Return_kind:
929 if (s->v.Return.value)
930 VISIT(st, expr, s->v.Return.value);
931 break;
932 case Delete_kind:
933 VISIT_SEQ(st, expr, s->v.Delete.targets);
934 break;
935 case Assign_kind:
936 VISIT_SEQ(st, expr, s->v.Assign.targets);
937 VISIT(st, expr, s->v.Assign.value);
938 break;
939 case AugAssign_kind:
940 VISIT(st, expr, s->v.AugAssign.target);
941 VISIT(st, expr, s->v.AugAssign.value);
942 break;
943 case Print_kind:
944 if (s->v.Print.dest)
945 VISIT(st, expr, s->v.Print.dest);
946 VISIT_SEQ(st, expr, s->v.Print.values);
947 break;
948 case For_kind:
949 VISIT(st, expr, s->v.For.target);
950 VISIT(st, expr, s->v.For.iter);
951 VISIT_SEQ(st, stmt, s->v.For.body);
952 if (s->v.For.orelse)
953 VISIT_SEQ(st, stmt, s->v.For.orelse);
954 break;
955 case While_kind:
956 VISIT(st, expr, s->v.While.test);
957 VISIT_SEQ(st, stmt, s->v.While.body);
958 if (s->v.While.orelse)
959 VISIT_SEQ(st, stmt, s->v.While.orelse);
960 break;
961 case If_kind:
962 /* XXX if 0: and lookup_yield() hacks */
963 VISIT(st, expr, s->v.If.test);
964 VISIT_SEQ(st, stmt, s->v.If.body);
965 if (s->v.If.orelse)
966 VISIT_SEQ(st, stmt, s->v.If.orelse);
967 break;
968 case Raise_kind:
969 if (s->v.Raise.type) {
970 VISIT(st, expr, s->v.Raise.type);
971 if (s->v.Raise.inst) {
972 VISIT(st, expr, s->v.Raise.inst);
973 if (s->v.Raise.tback)
974 VISIT(st, expr, s->v.Raise.tback);
975 }
976 }
977 break;
978 case TryExcept_kind:
979 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
980 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
981 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
982 break;
983 case TryFinally_kind:
984 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
985 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
986 break;
987 case Assert_kind:
988 VISIT(st, expr, s->v.Assert.test);
989 if (s->v.Assert.msg)
990 VISIT(st, expr, s->v.Assert.msg);
991 break;
992 case Import_kind:
993 VISIT_SEQ(st, alias, s->v.Import.names);
994 /* XXX Don't have the lineno available inside
995 visit_alias */
996 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
997 st->st_cur->ste_opt_lineno = s->lineno;
998 break;
999 case ImportFrom_kind:
1000 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1001 /* XXX Don't have the lineno available inside
1002 visit_alias */
1003 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1004 st->st_cur->ste_opt_lineno = s->lineno;
1005 break;
1006 case Exec_kind:
1007 VISIT(st, expr, s->v.Exec.body);
1008 if (!st->st_cur->ste_opt_lineno)
1009 st->st_cur->ste_opt_lineno = s->lineno;
1010 if (s->v.Exec.globals) {
1011 st->st_cur->ste_unoptimized |= OPT_EXEC;
1012 VISIT(st, expr, s->v.Exec.globals);
1013 if (s->v.Exec.locals)
1014 VISIT(st, expr, s->v.Exec.locals);
1015 } else {
1016 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1017 }
1018 break;
1019 case Global_kind: {
1020 int i;
1021 asdl_seq *seq = s->v.Global.names;
1022 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1023 identifier name = asdl_seq_GET(seq, i);
1024 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001025 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 if (cur < 0)
1027 return 0;
1028 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001029 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 if (cur & DEF_LOCAL)
1031 PyOS_snprintf(buf, sizeof(buf),
1032 GLOBAL_AFTER_ASSIGN,
1033 c_name);
1034 else
1035 PyOS_snprintf(buf, sizeof(buf),
1036 GLOBAL_AFTER_USE,
1037 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001038 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 return 0;
1040 }
1041 if (!symtable_add_def(st, name, DEF_GLOBAL))
1042 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 break;
1045 }
1046 case Expr_kind:
1047 VISIT(st, expr, s->v.Expr.value);
1048 break;
1049 case Pass_kind:
1050 case Break_kind:
1051 case Continue_kind:
1052 /* nothing to do here */
1053 break;
1054 }
1055 return 1;
1056}
1057
1058static int
1059symtable_visit_expr(struct symtable *st, expr_ty e)
1060{
1061 switch (e->kind) {
1062 case BoolOp_kind:
1063 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1064 break;
1065 case BinOp_kind:
1066 VISIT(st, expr, e->v.BinOp.left);
1067 VISIT(st, expr, e->v.BinOp.right);
1068 break;
1069 case UnaryOp_kind:
1070 VISIT(st, expr, e->v.UnaryOp.operand);
1071 break;
1072 case Lambda_kind: {
1073 if (!symtable_add_def(st, GET_IDENTIFIER(lambda), DEF_LOCAL))
1074 return 0;
1075 if (e->v.Lambda.args->defaults)
1076 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1077 /* XXX how to get line numbers for expressions */
1078 if (!symtable_enter_block(st, GET_IDENTIFIER(lambda),
1079 FunctionBlock, (void *)e, 0))
1080 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001081 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1082 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 if (!symtable_exit_block(st, (void *)e))
1084 return 0;
1085 break;
1086 }
1087 case Dict_kind:
1088 VISIT_SEQ(st, expr, e->v.Dict.keys);
1089 VISIT_SEQ(st, expr, e->v.Dict.values);
1090 break;
1091 case ListComp_kind: {
1092 char tmpname[256];
1093 identifier tmp;
1094
1095 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1096 ++st->st_cur->ste_tmpname);
Neal Norwitz4737b232005-11-19 23:58:29 +00001097 tmp = PyString_InternFromString(tmpname);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1099 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001100 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101 VISIT(st, expr, e->v.ListComp.elt);
1102 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1103 break;
1104 }
1105 case GeneratorExp_kind: {
1106 if (!symtable_visit_genexp(st, e)) {
1107 return 0;
1108 }
1109 break;
1110 }
1111 case Yield_kind:
1112 if (e->v.Yield.value)
1113 VISIT(st, expr, e->v.Yield.value);
1114 st->st_cur->ste_generator = 1;
1115 break;
1116 case Compare_kind:
1117 VISIT(st, expr, e->v.Compare.left);
1118 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1119 break;
1120 case Call_kind:
1121 VISIT(st, expr, e->v.Call.func);
1122 VISIT_SEQ(st, expr, e->v.Call.args);
1123 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1124 if (e->v.Call.starargs)
1125 VISIT(st, expr, e->v.Call.starargs);
1126 if (e->v.Call.kwargs)
1127 VISIT(st, expr, e->v.Call.kwargs);
1128 break;
1129 case Repr_kind:
1130 VISIT(st, expr, e->v.Repr.value);
1131 break;
1132 case Num_kind:
1133 case Str_kind:
1134 /* Nothing to do here. */
1135 break;
1136 /* The following exprs can be assignment targets. */
1137 case Attribute_kind:
1138 VISIT(st, expr, e->v.Attribute.value);
1139 break;
1140 case Subscript_kind:
1141 VISIT(st, expr, e->v.Subscript.value);
1142 VISIT(st, slice, e->v.Subscript.slice);
1143 break;
1144 case Name_kind:
1145 if (!symtable_add_def(st, e->v.Name.id,
1146 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1147 return 0;
1148 break;
1149 /* child nodes of List and Tuple will have expr_context set */
1150 case List_kind:
1151 VISIT_SEQ(st, expr, e->v.List.elts);
1152 break;
1153 case Tuple_kind:
1154 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1155 break;
1156 }
1157 return 1;
1158}
1159
1160static int
1161symtable_implicit_arg(struct symtable *st, int pos)
1162{
1163 PyObject *id = PyString_FromFormat(".%d", pos);
1164 if (id == NULL)
1165 return 0;
1166 if (!symtable_add_def(st, id, DEF_PARAM)) {
1167 Py_DECREF(id);
1168 return 0;
1169 }
1170 Py_DECREF(id);
1171 return 1;
1172}
1173
1174static int
1175symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1176{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001177 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178
1179 /* go through all the toplevel arguments first */
1180 for (i = 0; i < asdl_seq_LEN(args); i++) {
1181 expr_ty arg = asdl_seq_GET(args, i);
1182 if (arg->kind == Name_kind) {
1183 assert(arg->v.Name.ctx == Param ||
1184 (arg->v.Name.ctx == Store && !toplevel));
1185 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1186 return 0;
1187 }
1188 else if (arg->kind == Tuple_kind) {
1189 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 if (toplevel) {
1191 if (!symtable_implicit_arg(st, i))
1192 return 0;
1193 }
1194 }
1195 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001196 PyErr_SetString(PyExc_SyntaxError,
1197 "invalid expression in parameter list");
1198 PyErr_SyntaxLocation(st->st_filename,
1199 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 return 0;
1201 }
1202 }
1203
1204 if (!toplevel) {
1205 if (!symtable_visit_params_nested(st, args))
1206 return 0;
1207 }
1208
1209 return 1;
1210}
1211
1212static int
1213symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1214{
1215 int i;
1216 for (i = 0; i < asdl_seq_LEN(args); i++) {
1217 expr_ty arg = asdl_seq_GET(args, i);
1218 if (arg->kind == Tuple_kind &&
1219 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1220 return 0;
1221 }
1222
1223 return 1;
1224}
1225
1226static int
1227symtable_visit_arguments(struct symtable *st, arguments_ty a)
1228{
1229 /* skip default arguments inside function block
1230 XXX should ast be different?
1231 */
1232 if (a->args && !symtable_visit_params(st, a->args, 1))
1233 return 0;
1234 if (a->vararg) {
1235 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1236 return 0;
1237 st->st_cur->ste_varargs = 1;
1238 }
1239 if (a->kwarg) {
1240 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1241 return 0;
1242 st->st_cur->ste_varkeywords = 1;
1243 }
1244 if (a->args && !symtable_visit_params_nested(st, a->args))
1245 return 0;
1246 return 1;
1247}
1248
1249
1250static int
1251symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1252{
1253 if (eh->type)
1254 VISIT(st, expr, eh->type);
1255 if (eh->name)
1256 VISIT(st, expr, eh->name);
1257 VISIT_SEQ(st, stmt, eh->body);
1258 return 1;
1259}
1260
1261
1262static int
1263symtable_visit_alias(struct symtable *st, alias_ty a)
1264{
1265 /* Compute store_name, the name actually bound by the import
1266 operation. It is diferent than a->name when a->name is a
1267 dotted package name (e.g. spam.eggs)
1268 */
1269 PyObject *store_name;
1270 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1271 const char *base = PyString_AS_STRING(name);
1272 char *dot = strchr(base, '.');
1273 if (dot)
1274 store_name = PyString_FromStringAndSize(base, dot - base);
1275 else {
1276 store_name = name;
1277 Py_INCREF(store_name);
1278 }
1279 if (strcmp(PyString_AS_STRING(name), "*")) {
1280 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1281 Py_DECREF(store_name);
1282 return r;
1283 }
1284 else {
1285 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001286 int lineno = st->st_cur->ste_lineno;
1287 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001288 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001290 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 }
1292 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001293 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 return 1;
1295 }
1296}
1297
1298
1299static int
1300symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1301{
1302 VISIT(st, expr, lc->target);
1303 VISIT(st, expr, lc->iter);
1304 VISIT_SEQ(st, expr, lc->ifs);
1305 return 1;
1306}
1307
1308
1309static int
1310symtable_visit_keyword(struct symtable *st, keyword_ty k)
1311{
1312 VISIT(st, expr, k->value);
1313 return 1;
1314}
1315
1316
1317static int
1318symtable_visit_slice(struct symtable *st, slice_ty s)
1319{
1320 switch (s->kind) {
1321 case Slice_kind:
1322 if (s->v.Slice.lower)
1323 VISIT(st, expr, s->v.Slice.lower)
1324 if (s->v.Slice.upper)
1325 VISIT(st, expr, s->v.Slice.upper)
1326 if (s->v.Slice.step)
1327 VISIT(st, expr, s->v.Slice.step)
1328 break;
1329 case ExtSlice_kind:
1330 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1331 break;
1332 case Index_kind:
1333 VISIT(st, expr, s->v.Index.value)
1334 break;
1335 case Ellipsis_kind:
1336 break;
1337 }
1338 return 1;
1339}
1340
1341static int
1342symtable_visit_genexp(struct symtable *st, expr_ty e)
1343{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 comprehension_ty outermost = ((comprehension_ty)
1345 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1346 /* Outermost iterator is evaluated in current scope */
1347 VISIT(st, expr, outermost->iter);
1348 /* Create generator scope for the rest */
Nick Coghlan99b25332005-11-16 12:45:24 +00001349 if (!symtable_enter_block(st, GET_IDENTIFIER(genexpr),
1350 FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 return 0;
1352 }
1353 st->st_cur->ste_generator = 1;
1354 /* Outermost iter is received as an argument */
1355 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001356 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 return 0;
1358 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001359 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1360 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1361 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1362 e->v.GeneratorExp.generators, 1, (void*)e);
1363 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 if (!symtable_exit_block(st, (void *)e))
1365 return 0;
1366 return 1;
1367}