blob: bc0fc3364a7fd8fa394cb78e8736b0b7b34ebdf7 [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{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000718 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719
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
Guido van Rossumc2e20742006-02-27 22:32:47 +0000894symtable_new_tmpname(struct symtable *st)
895{
896 char tmpname[256];
897 identifier tmp;
898
899 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
900 ++st->st_cur->ste_tmpname);
901 tmp = PyString_InternFromString(tmpname);
902 if (!symtable_add_def(st, tmp, DEF_LOCAL))
903 return 0;
904 Py_DECREF(tmp);
905 return 1;
906}
907
908static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909symtable_visit_stmt(struct symtable *st, stmt_ty s)
910{
911 switch (s->kind) {
912 case FunctionDef_kind:
913 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
914 return 0;
915 if (s->v.FunctionDef.args->defaults)
916 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
917 if (s->v.FunctionDef.decorators)
918 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
919 if (!symtable_enter_block(st, s->v.FunctionDef.name,
920 FunctionBlock, (void *)s, s->lineno))
921 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000922 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
923 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
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 case ClassDef_kind: {
928 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
930 return 0;
931 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
932 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
933 (void *)s, s->lineno))
934 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000935 tmp = st->st_private;
936 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000937 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000938 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 if (!symtable_exit_block(st, s))
940 return 0;
941 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000942 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 case Return_kind:
944 if (s->v.Return.value)
945 VISIT(st, expr, s->v.Return.value);
946 break;
947 case Delete_kind:
948 VISIT_SEQ(st, expr, s->v.Delete.targets);
949 break;
950 case Assign_kind:
951 VISIT_SEQ(st, expr, s->v.Assign.targets);
952 VISIT(st, expr, s->v.Assign.value);
953 break;
954 case AugAssign_kind:
955 VISIT(st, expr, s->v.AugAssign.target);
956 VISIT(st, expr, s->v.AugAssign.value);
957 break;
958 case Print_kind:
959 if (s->v.Print.dest)
960 VISIT(st, expr, s->v.Print.dest);
961 VISIT_SEQ(st, expr, s->v.Print.values);
962 break;
963 case For_kind:
964 VISIT(st, expr, s->v.For.target);
965 VISIT(st, expr, s->v.For.iter);
966 VISIT_SEQ(st, stmt, s->v.For.body);
967 if (s->v.For.orelse)
968 VISIT_SEQ(st, stmt, s->v.For.orelse);
969 break;
970 case While_kind:
971 VISIT(st, expr, s->v.While.test);
972 VISIT_SEQ(st, stmt, s->v.While.body);
973 if (s->v.While.orelse)
974 VISIT_SEQ(st, stmt, s->v.While.orelse);
975 break;
976 case If_kind:
977 /* XXX if 0: and lookup_yield() hacks */
978 VISIT(st, expr, s->v.If.test);
979 VISIT_SEQ(st, stmt, s->v.If.body);
980 if (s->v.If.orelse)
981 VISIT_SEQ(st, stmt, s->v.If.orelse);
982 break;
983 case Raise_kind:
984 if (s->v.Raise.type) {
985 VISIT(st, expr, s->v.Raise.type);
986 if (s->v.Raise.inst) {
987 VISIT(st, expr, s->v.Raise.inst);
988 if (s->v.Raise.tback)
989 VISIT(st, expr, s->v.Raise.tback);
990 }
991 }
992 break;
993 case TryExcept_kind:
994 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
995 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
996 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
997 break;
998 case TryFinally_kind:
999 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1000 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1001 break;
1002 case Assert_kind:
1003 VISIT(st, expr, s->v.Assert.test);
1004 if (s->v.Assert.msg)
1005 VISIT(st, expr, s->v.Assert.msg);
1006 break;
1007 case Import_kind:
1008 VISIT_SEQ(st, alias, s->v.Import.names);
1009 /* XXX Don't have the lineno available inside
1010 visit_alias */
1011 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1012 st->st_cur->ste_opt_lineno = s->lineno;
1013 break;
1014 case ImportFrom_kind:
1015 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1016 /* XXX Don't have the lineno available inside
1017 visit_alias */
1018 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1019 st->st_cur->ste_opt_lineno = s->lineno;
1020 break;
1021 case Exec_kind:
1022 VISIT(st, expr, s->v.Exec.body);
1023 if (!st->st_cur->ste_opt_lineno)
1024 st->st_cur->ste_opt_lineno = s->lineno;
1025 if (s->v.Exec.globals) {
1026 st->st_cur->ste_unoptimized |= OPT_EXEC;
1027 VISIT(st, expr, s->v.Exec.globals);
1028 if (s->v.Exec.locals)
1029 VISIT(st, expr, s->v.Exec.locals);
1030 } else {
1031 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1032 }
1033 break;
1034 case Global_kind: {
1035 int i;
1036 asdl_seq *seq = s->v.Global.names;
1037 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1038 identifier name = asdl_seq_GET(seq, i);
1039 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001040 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 if (cur < 0)
1042 return 0;
1043 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001044 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 if (cur & DEF_LOCAL)
1046 PyOS_snprintf(buf, sizeof(buf),
1047 GLOBAL_AFTER_ASSIGN,
1048 c_name);
1049 else
1050 PyOS_snprintf(buf, sizeof(buf),
1051 GLOBAL_AFTER_USE,
1052 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001053 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054 return 0;
1055 }
1056 if (!symtable_add_def(st, name, DEF_GLOBAL))
1057 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 break;
1060 }
1061 case Expr_kind:
1062 VISIT(st, expr, s->v.Expr.value);
1063 break;
1064 case Pass_kind:
1065 case Break_kind:
1066 case Continue_kind:
1067 /* nothing to do here */
1068 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001069 case With_kind:
1070 if (!symtable_new_tmpname(st))
1071 return 0;
1072 VISIT(st, expr, s->v.With.context_expr);
1073 if (s->v.With.optional_vars) {
1074 if (!symtable_new_tmpname(st))
1075 return 0;
1076 VISIT(st, expr, s->v.With.optional_vars);
1077 }
1078 VISIT_SEQ(st, stmt, s->v.With.body);
1079 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 }
1081 return 1;
1082}
1083
1084static int
1085symtable_visit_expr(struct symtable *st, expr_ty e)
1086{
1087 switch (e->kind) {
1088 case BoolOp_kind:
1089 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1090 break;
1091 case BinOp_kind:
1092 VISIT(st, expr, e->v.BinOp.left);
1093 VISIT(st, expr, e->v.BinOp.right);
1094 break;
1095 case UnaryOp_kind:
1096 VISIT(st, expr, e->v.UnaryOp.operand);
1097 break;
1098 case Lambda_kind: {
1099 if (!symtable_add_def(st, GET_IDENTIFIER(lambda), DEF_LOCAL))
1100 return 0;
1101 if (e->v.Lambda.args->defaults)
1102 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1103 /* XXX how to get line numbers for expressions */
1104 if (!symtable_enter_block(st, GET_IDENTIFIER(lambda),
1105 FunctionBlock, (void *)e, 0))
1106 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001107 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1108 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109 if (!symtable_exit_block(st, (void *)e))
1110 return 0;
1111 break;
1112 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001113 case IfExp_kind:
1114 VISIT(st, expr, e->v.IfExp.test);
1115 VISIT(st, expr, e->v.IfExp.body);
1116 VISIT(st, expr, e->v.IfExp.orelse);
1117 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118 case Dict_kind:
1119 VISIT_SEQ(st, expr, e->v.Dict.keys);
1120 VISIT_SEQ(st, expr, e->v.Dict.values);
1121 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001122 case ListComp_kind:
1123 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 return 0;
1125 VISIT(st, expr, e->v.ListComp.elt);
1126 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1127 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001128 case GeneratorExp_kind:
1129 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 case Yield_kind:
1133 if (e->v.Yield.value)
1134 VISIT(st, expr, e->v.Yield.value);
1135 st->st_cur->ste_generator = 1;
1136 break;
1137 case Compare_kind:
1138 VISIT(st, expr, e->v.Compare.left);
1139 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1140 break;
1141 case Call_kind:
1142 VISIT(st, expr, e->v.Call.func);
1143 VISIT_SEQ(st, expr, e->v.Call.args);
1144 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1145 if (e->v.Call.starargs)
1146 VISIT(st, expr, e->v.Call.starargs);
1147 if (e->v.Call.kwargs)
1148 VISIT(st, expr, e->v.Call.kwargs);
1149 break;
1150 case Repr_kind:
1151 VISIT(st, expr, e->v.Repr.value);
1152 break;
1153 case Num_kind:
1154 case Str_kind:
1155 /* Nothing to do here. */
1156 break;
1157 /* The following exprs can be assignment targets. */
1158 case Attribute_kind:
1159 VISIT(st, expr, e->v.Attribute.value);
1160 break;
1161 case Subscript_kind:
1162 VISIT(st, expr, e->v.Subscript.value);
1163 VISIT(st, slice, e->v.Subscript.slice);
1164 break;
1165 case Name_kind:
1166 if (!symtable_add_def(st, e->v.Name.id,
1167 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1168 return 0;
1169 break;
1170 /* child nodes of List and Tuple will have expr_context set */
1171 case List_kind:
1172 VISIT_SEQ(st, expr, e->v.List.elts);
1173 break;
1174 case Tuple_kind:
1175 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1176 break;
1177 }
1178 return 1;
1179}
1180
1181static int
1182symtable_implicit_arg(struct symtable *st, int pos)
1183{
1184 PyObject *id = PyString_FromFormat(".%d", pos);
1185 if (id == NULL)
1186 return 0;
1187 if (!symtable_add_def(st, id, DEF_PARAM)) {
1188 Py_DECREF(id);
1189 return 0;
1190 }
1191 Py_DECREF(id);
1192 return 1;
1193}
1194
1195static int
1196symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1197{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001198 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199
1200 /* go through all the toplevel arguments first */
1201 for (i = 0; i < asdl_seq_LEN(args); i++) {
1202 expr_ty arg = asdl_seq_GET(args, i);
1203 if (arg->kind == Name_kind) {
1204 assert(arg->v.Name.ctx == Param ||
1205 (arg->v.Name.ctx == Store && !toplevel));
1206 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1207 return 0;
1208 }
1209 else if (arg->kind == Tuple_kind) {
1210 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 if (toplevel) {
1212 if (!symtable_implicit_arg(st, i))
1213 return 0;
1214 }
1215 }
1216 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001217 PyErr_SetString(PyExc_SyntaxError,
1218 "invalid expression in parameter list");
1219 PyErr_SyntaxLocation(st->st_filename,
1220 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 return 0;
1222 }
1223 }
1224
1225 if (!toplevel) {
1226 if (!symtable_visit_params_nested(st, args))
1227 return 0;
1228 }
1229
1230 return 1;
1231}
1232
1233static int
1234symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1235{
1236 int i;
1237 for (i = 0; i < asdl_seq_LEN(args); i++) {
1238 expr_ty arg = asdl_seq_GET(args, i);
1239 if (arg->kind == Tuple_kind &&
1240 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1241 return 0;
1242 }
1243
1244 return 1;
1245}
1246
1247static int
1248symtable_visit_arguments(struct symtable *st, arguments_ty a)
1249{
1250 /* skip default arguments inside function block
1251 XXX should ast be different?
1252 */
1253 if (a->args && !symtable_visit_params(st, a->args, 1))
1254 return 0;
1255 if (a->vararg) {
1256 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1257 return 0;
1258 st->st_cur->ste_varargs = 1;
1259 }
1260 if (a->kwarg) {
1261 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1262 return 0;
1263 st->st_cur->ste_varkeywords = 1;
1264 }
1265 if (a->args && !symtable_visit_params_nested(st, a->args))
1266 return 0;
1267 return 1;
1268}
1269
1270
1271static int
1272symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1273{
1274 if (eh->type)
1275 VISIT(st, expr, eh->type);
1276 if (eh->name)
1277 VISIT(st, expr, eh->name);
1278 VISIT_SEQ(st, stmt, eh->body);
1279 return 1;
1280}
1281
1282
1283static int
1284symtable_visit_alias(struct symtable *st, alias_ty a)
1285{
1286 /* Compute store_name, the name actually bound by the import
1287 operation. It is diferent than a->name when a->name is a
1288 dotted package name (e.g. spam.eggs)
1289 */
1290 PyObject *store_name;
1291 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1292 const char *base = PyString_AS_STRING(name);
1293 char *dot = strchr(base, '.');
1294 if (dot)
1295 store_name = PyString_FromStringAndSize(base, dot - base);
1296 else {
1297 store_name = name;
1298 Py_INCREF(store_name);
1299 }
1300 if (strcmp(PyString_AS_STRING(name), "*")) {
1301 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1302 Py_DECREF(store_name);
1303 return r;
1304 }
1305 else {
1306 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001307 int lineno = st->st_cur->ste_lineno;
1308 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001309 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001311 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 }
1313 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001314 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315 return 1;
1316 }
1317}
1318
1319
1320static int
1321symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1322{
1323 VISIT(st, expr, lc->target);
1324 VISIT(st, expr, lc->iter);
1325 VISIT_SEQ(st, expr, lc->ifs);
1326 return 1;
1327}
1328
1329
1330static int
1331symtable_visit_keyword(struct symtable *st, keyword_ty k)
1332{
1333 VISIT(st, expr, k->value);
1334 return 1;
1335}
1336
1337
1338static int
1339symtable_visit_slice(struct symtable *st, slice_ty s)
1340{
1341 switch (s->kind) {
1342 case Slice_kind:
1343 if (s->v.Slice.lower)
1344 VISIT(st, expr, s->v.Slice.lower)
1345 if (s->v.Slice.upper)
1346 VISIT(st, expr, s->v.Slice.upper)
1347 if (s->v.Slice.step)
1348 VISIT(st, expr, s->v.Slice.step)
1349 break;
1350 case ExtSlice_kind:
1351 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1352 break;
1353 case Index_kind:
1354 VISIT(st, expr, s->v.Index.value)
1355 break;
1356 case Ellipsis_kind:
1357 break;
1358 }
1359 return 1;
1360}
1361
1362static int
1363symtable_visit_genexp(struct symtable *st, expr_ty e)
1364{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 comprehension_ty outermost = ((comprehension_ty)
1366 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1367 /* Outermost iterator is evaluated in current scope */
1368 VISIT(st, expr, outermost->iter);
1369 /* Create generator scope for the rest */
Nick Coghlan99b25332005-11-16 12:45:24 +00001370 if (!symtable_enter_block(st, GET_IDENTIFIER(genexpr),
1371 FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 return 0;
1373 }
1374 st->st_cur->ste_generator = 1;
1375 /* Outermost iter is received as an argument */
1376 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001377 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 return 0;
1379 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001380 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1381 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1382 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1383 e->v.GeneratorExp.generators, 1, (void*)e);
1384 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 if (!symtable_exit_block(st, (void *)e))
1386 return 0;
1387 return 1;
1388}