blob: 5e67c06a885eb2f6a96071e20923082f5848ab6b [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
3#include "code.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005#include "structmember.h"
6
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#define GLOBAL_AFTER_ASSIGN \
9"name '%.400s' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000010
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011#define GLOBAL_AFTER_USE \
12"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000013
Neal Norwitz5d0ad502005-12-19 04:27:42 +000014#define IMPORT_STAR_WARNING "import * only allowed at module level"
15
Georg Brandlddbaa662006-06-04 21:56:52 +000016#define RETURN_VAL_IN_GENERATOR \
17 "'return' with argument inside generator"
Neal Norwitz5d0ad502005-12-19 04:27:42 +000018
Benjamin Petersone0d12eb2008-08-16 23:29:40 +000019
Neal Norwitz090b3dd2006-02-28 22:36:46 +000020static PySTEntryObject *
Benjamin Petersone0d12eb2008-08-16 23:29:40 +000021ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000023{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024 PySTEntryObject *ste = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000025 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027 k = PyLong_FromVoidPtr(key);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000028 if (k == NULL)
29 goto fail;
Neal Norwitz5becac52008-03-15 22:36:01 +000030 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
31 if (ste == NULL)
32 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000033 ste->ste_table = st;
34 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000036
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037 ste->ste_name = name;
38 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000039
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000040 ste->ste_symbols = NULL;
41 ste->ste_varnames = NULL;
42 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000043
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000044 ste->ste_symbols = PyDict_New();
45 if (ste->ste_symbols == NULL)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000046 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000047
48 ste->ste_varnames = PyList_New(0);
49 if (ste->ste_varnames == NULL)
50 goto fail;
51
52 ste->ste_children = PyList_New(0);
53 if (ste->ste_children == NULL)
54 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056 ste->ste_type = block;
57 ste->ste_unoptimized = 0;
58 ste->ste_nested = 0;
59 ste->ste_free = 0;
60 ste->ste_varargs = 0;
61 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000062 ste->ste_opt_lineno = 0;
Jeremy Hylton4d508ad2003-05-21 17:34:50 +000063 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000064 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000066 if (st->st_cur != NULL &&
67 (st->st_cur->ste_nested ||
68 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000071 ste->ste_generator = 0;
Georg Brandlddbaa662006-06-04 21:56:52 +000072 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000073
74 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
75 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000076
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000078 fail:
79 Py_XDECREF(ste);
80 return NULL;
81}
82
83static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000084ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085{
86 char buf[256];
87
Barry Warsaw4b4ab202001-11-28 21:36:28 +000088 PyOS_snprintf(buf, sizeof(buf),
89 "<symtable entry %.100s(%ld), line %d>",
Gregory P. Smithdd96db62008-06-09 04:58:54 +000090 PyString_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Gregory P. Smithdd96db62008-06-09 04:58:54 +000092 return PyString_FromString(buf);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093}
94
95static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097{
98 ste->ste_table = NULL;
99 Py_XDECREF(ste->ste_id);
100 Py_XDECREF(ste->ste_name);
101 Py_XDECREF(ste->ste_symbols);
102 Py_XDECREF(ste->ste_varnames);
103 Py_XDECREF(ste->ste_children);
104 PyObject_Del(ste);
105}
106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108
Guido van Rossum6f799372001-09-20 20:46:19 +0000109static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000110 {"id", T_OBJECT, OFF(ste_id), READONLY},
111 {"name", T_OBJECT, OFF(ste_name), READONLY},
112 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
113 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
114 {"children", T_OBJECT, OFF(ste_children), READONLY},
115 {"type", T_INT, OFF(ste_type), READONLY},
116 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000117 {NULL}
118};
119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120PyTypeObject PySTEntry_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000121 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000122 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124 0,
125 (destructor)ste_dealloc, /* tp_dealloc */
126 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000127 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000128 0, /* tp_setattr */
129 0, /* tp_compare */
130 (reprfunc)ste_repr, /* tp_repr */
131 0, /* tp_as_number */
132 0, /* tp_as_sequence */
133 0, /* tp_as_mapping */
134 0, /* tp_hash */
135 0, /* tp_call */
136 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000137 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000138 0, /* tp_setattro */
139 0, /* tp_as_buffer */
140 Py_TPFLAGS_DEFAULT, /* tp_flags */
141 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000142 0, /* tp_traverse */
143 0, /* tp_clear */
144 0, /* tp_richcompare */
145 0, /* tp_weaklistoffset */
146 0, /* tp_iter */
147 0, /* tp_iternext */
148 0, /* tp_methods */
149 ste_memberlist, /* tp_members */
150 0, /* tp_getset */
151 0, /* tp_base */
152 0, /* tp_dict */
153 0, /* tp_descr_get */
154 0, /* tp_descr_set */
155 0, /* tp_dictoffset */
156 0, /* tp_init */
157 0, /* tp_alloc */
158 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000159};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160
161static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000162static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000164 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165static int symtable_exit_block(struct symtable *st, void *ast);
166static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
167static int symtable_visit_expr(struct symtable *st, expr_ty s);
168static int symtable_visit_genexp(struct symtable *st, expr_ty s);
169static int symtable_visit_arguments(struct symtable *st, arguments_ty);
170static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
171static int symtable_visit_alias(struct symtable *st, alias_ty);
172static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
173static int symtable_visit_keyword(struct symtable *st, keyword_ty);
174static int symtable_visit_slice(struct symtable *st, slice_ty);
175static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
176static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
177static int symtable_implicit_arg(struct symtable *st, int pos);
178
179
Nick Coghlan99b25332005-11-16 12:45:24 +0000180static identifier top = NULL, lambda = NULL, genexpr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181
182#define GET_IDENTIFIER(VAR) \
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000183 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184
185#define DUPLICATE_ARGUMENT \
186"duplicate argument '%s' in function definition"
187
188static struct symtable *
189symtable_new(void)
190{
191 struct symtable *st;
192
193 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
194 if (st == NULL)
195 return NULL;
196
197 st->st_filename = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000198 st->st_symbols = NULL;
199
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 if ((st->st_stack = PyList_New(0)) == NULL)
201 goto fail;
202 if ((st->st_symbols = PyDict_New()) == NULL)
203 goto fail;
204 st->st_cur = NULL;
205 st->st_tmpname = 0;
206 st->st_private = NULL;
207 return st;
208 fail:
209 PySymtable_Free(st);
210 return NULL;
211}
212
213struct symtable *
214PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
215{
216 struct symtable *st = symtable_new();
217 asdl_seq *seq;
218 int i;
219
220 if (st == NULL)
221 return st;
222 st->st_filename = filename;
223 st->st_future = future;
Neal Norwitz76059362006-08-19 04:52:03 +0000224 if (!GET_IDENTIFIER(top) ||
225 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000226 PySymtable_Free(st);
227 return NULL;
228 }
229
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230 st->st_top = st->st_cur;
231 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
232 /* Any other top-level initialization? */
233 switch (mod->kind) {
234 case Module_kind:
235 seq = mod->v.Module.body;
236 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000237 if (!symtable_visit_stmt(st,
238 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 goto error;
240 break;
241 case Expression_kind:
242 if (!symtable_visit_expr(st, mod->v.Expression.body))
243 goto error;
244 break;
245 case Interactive_kind:
246 seq = mod->v.Interactive.body;
247 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000248 if (!symtable_visit_stmt(st,
249 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250 goto error;
251 break;
252 case Suite_kind:
253 PyErr_SetString(PyExc_RuntimeError,
254 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000255 goto error;
256 }
257 if (!symtable_exit_block(st, (void *)mod)) {
258 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259 return NULL;
260 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 if (symtable_analyze(st))
262 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000263 PySymtable_Free(st);
264 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000266 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267 PySymtable_Free(st);
268 return NULL;
269}
270
271void
272PySymtable_Free(struct symtable *st)
273{
274 Py_XDECREF(st->st_symbols);
275 Py_XDECREF(st->st_stack);
276 PyMem_Free((void *)st);
277}
278
279PySTEntryObject *
280PySymtable_Lookup(struct symtable *st, void *key)
281{
282 PyObject *k, *v;
283
284 k = PyLong_FromVoidPtr(key);
285 if (k == NULL)
286 return NULL;
287 v = PyDict_GetItem(st->st_symbols, k);
288 if (v) {
289 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291 }
292 else {
293 PyErr_SetString(PyExc_KeyError,
294 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000296
297 Py_DECREF(k);
298 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299}
300
301int
302PyST_GetScope(PySTEntryObject *ste, PyObject *name)
303{
304 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
305 if (!v)
306 return 0;
307 assert(PyInt_Check(v));
308 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
309}
310
311
312/* Analyze raw symbol information to determine scope of each name.
313
314 The next several functions are helpers for PySymtable_Analyze(),
315 which determines whether a name is local, global, or free. In addition,
316 it determines which local variables are cell variables; they provide
317 bindings that are used for free variables in enclosed blocks.
318
319 There are also two kinds of free variables, implicit and explicit. An
320 explicit global is declared with the global statement. An implicit
321 global is a free variable for which the compiler has found no binding
322 in an enclosing function scope. The implicit global is either a global
323 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
324 to handle these names to implement slightly odd semantics. In such a
325 block, the name is treated as global until it is assigned to; then it
326 is treated as a local.
327
328 The symbol table requires two passes to determine the scope of each name.
329 The first pass collects raw facts from the AST: the name is a parameter
330 here, the name is used by not defined here, etc. The second pass analyzes
331 these facts during a pass over the PySTEntryObjects created during pass 1.
332
333 When a function is entered during the second pass, the parent passes
334 the set of all name bindings visible to its children. These bindings
335 are used to determine if the variable is free or an implicit global.
336 After doing the local analysis, it analyzes each of its child blocks
337 using an updated set of name bindings.
338
339 The children update the free variable set. If a local variable is free
340 in a child, the variable is marked as a cell. The current function must
341 provide runtime storage for the variable that may outlive the function's
342 frame. Cell variables are removed from the free set before the analyze
343 function returns to its parent.
344
345 The sets of bound and free variables are implemented as dictionaries
346 mapping strings to None.
347*/
348
349#define SET_SCOPE(DICT, NAME, I) { \
350 PyObject *o = PyInt_FromLong(I); \
351 if (!o) \
352 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000353 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
354 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000356 } \
357 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358}
359
360/* Decide on scope of name, given flags.
361
362 The dicts passed in as arguments are modified as necessary.
363 ste is passed so that flags can be updated.
364*/
365
366static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000367analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368 PyObject *bound, PyObject *local, PyObject *free,
369 PyObject *global)
370{
371 if (flags & DEF_GLOBAL) {
372 if (flags & DEF_PARAM) {
373 PyErr_Format(PyExc_SyntaxError,
374 "name '%s' is local and global",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000375 PyString_AS_STRING(name));
Benjamin Peterson08473322008-08-16 22:11:33 +0000376 PyErr_SyntaxLocation(ste->ste_table->st_filename,
377 ste->ste_lineno);
378
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 return 0;
380 }
381 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
382 if (PyDict_SetItem(global, name, Py_None) < 0)
383 return 0;
384 if (bound && PyDict_GetItem(bound, name)) {
385 if (PyDict_DelItem(bound, name) < 0)
386 return 0;
387 }
388 return 1;
389 }
390 if (flags & DEF_BOUND) {
391 SET_SCOPE(dict, name, LOCAL);
392 if (PyDict_SetItem(local, name, Py_None) < 0)
393 return 0;
394 if (PyDict_GetItem(global, name)) {
395 if (PyDict_DelItem(global, name) < 0)
396 return 0;
397 }
398 return 1;
399 }
400 /* If an enclosing block has a binding for this name, it
401 is a free variable rather than a global variable.
402 Note that having a non-NULL bound implies that the block
403 is nested.
404 */
405 if (bound && PyDict_GetItem(bound, name)) {
406 SET_SCOPE(dict, name, FREE);
407 ste->ste_free = 1;
408 if (PyDict_SetItem(free, name, Py_None) < 0)
409 return 0;
410 return 1;
411 }
412 /* If a parent has a global statement, then call it global
413 explicit? It could also be global implicit.
414 */
415 else if (global && PyDict_GetItem(global, name)) {
416 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
417 return 1;
418 }
419 else {
420 if (ste->ste_nested)
421 ste->ste_free = 1;
422 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
423 return 1;
424 }
425 return 0; /* Can't get here */
426}
427
428#undef SET_SCOPE
429
430/* If a name is defined in free and also in locals, then this block
431 provides the binding for the free variable. The name should be
432 marked CELL in this block and removed from the free list.
433
434 Note that the current block's free variables are included in free.
435 That's safe because no name can be free and local in the same scope.
436*/
437
438static int
439analyze_cells(PyObject *scope, PyObject *free)
440{
441 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000442 int success = 0;
443 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444
445 w = PyInt_FromLong(CELL);
446 if (!w)
447 return 0;
448 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000449 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000451 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 if (flags != LOCAL)
453 continue;
454 if (!PyDict_GetItem(free, name))
455 continue;
456 /* Replace LOCAL with CELL for this name, and remove
457 from free. It is safe to replace the value of name
458 in the dict, because it will not cause a resize.
459 */
460 if (PyDict_SetItem(scope, name, w) < 0)
461 goto error;
462 if (!PyDict_DelItem(free, name) < 0)
463 goto error;
464 }
465 success = 1;
466 error:
467 Py_DECREF(w);
468 return success;
469}
470
471/* Check for illegal statements in unoptimized namespaces */
472static int
473check_unoptimized(const PySTEntryObject* ste) {
474 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000475 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000477 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478 || !(ste->ste_free || ste->ste_child_free))
479 return 1;
480
Armin Rigo31441302005-10-21 12:57:31 +0000481 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 "contains a nested function with free variables" :
483 "is a nested function");
484
485 switch (ste->ste_unoptimized) {
486 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
487 case OPT_EXEC: /* qualified exec is fine */
488 return 1;
489 case OPT_IMPORT_STAR:
490 PyOS_snprintf(buf, sizeof(buf),
491 "import * is not allowed in function '%.100s' "
492 "because it is %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000493 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494 break;
495 case OPT_BARE_EXEC:
496 PyOS_snprintf(buf, sizeof(buf),
497 "unqualified exec is not allowed in function "
498 "'%.100s' it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000499 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 break;
501 default:
502 PyOS_snprintf(buf, sizeof(buf),
503 "function '%.100s' uses import * and bare exec, "
504 "which are illegal because it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000505 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 break;
507 }
508
509 PyErr_SetString(PyExc_SyntaxError, buf);
510 PyErr_SyntaxLocation(ste->ste_table->st_filename,
511 ste->ste_opt_lineno);
512 return 0;
513}
514
515/* Enter the final scope information into the st_symbols dict.
516 *
517 * All arguments are dicts. Modifies symbols, others are read-only.
518*/
519static int
520update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000521 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522{
523 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525
526 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000527 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528 assert(PyInt_Check(v));
529 flags = PyInt_AS_LONG(v);
530 w = PyDict_GetItem(scope, name);
531 assert(w && PyInt_Check(w));
532 i = PyInt_AS_LONG(w);
533 flags |= (i << SCOPE_OFF);
534 u = PyInt_FromLong(flags);
Neal Norwitz18b6adf2006-07-23 07:50:36 +0000535 if (!u)
536 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 if (PyDict_SetItem(symbols, name, u) < 0) {
538 Py_DECREF(u);
539 return 0;
540 }
541 Py_DECREF(u);
542 }
543
544 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
545 if (!free_value)
546 return 0;
547
548 /* add a free variable when it's only use is for creating a closure */
549 pos = 0;
550 while (PyDict_Next(free, &pos, &name, &v)) {
551 PyObject *o = PyDict_GetItem(symbols, name);
552
553 if (o) {
554 /* It could be a free variable in a method of
555 the class that has the same name as a local
556 or global in the class scope.
557 */
Anthony Baxter019aec62006-04-12 04:00:50 +0000558 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000560 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561 o = PyInt_FromLong(i);
562 if (!o) {
563 Py_DECREF(free_value);
564 return 0;
565 }
566 if (PyDict_SetItem(symbols, name, o) < 0) {
567 Py_DECREF(o);
568 Py_DECREF(free_value);
569 return 0;
570 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000571 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572 }
573 /* else it's not free, probably a cell */
574 continue;
575 }
576 if (!PyDict_GetItem(bound, name))
577 continue; /* it's a global */
578
579 if (PyDict_SetItem(symbols, name, free_value) < 0) {
580 Py_DECREF(free_value);
581 return 0;
582 }
583 }
584 Py_DECREF(free_value);
585 return 1;
586}
587
588/* Make final symbol table decisions for block of ste.
589 Arguments:
590 ste -- current symtable entry (input/output)
591 bound -- set of variables bound in enclosing scopes (input)
592 free -- set of free variables in enclosed scopes (output)
593 globals -- set of declared global variables in enclosing scopes (input)
594*/
595
596static int
597analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
598 PyObject *global)
599{
600 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
601 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000602 int i, success = 0;
603 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604
605 local = PyDict_New();
606 if (!local)
607 goto error;
608 scope = PyDict_New();
609 if (!scope)
610 goto error;
611 newglobal = PyDict_New();
612 if (!newglobal)
613 goto error;
614 newfree = PyDict_New();
615 if (!newfree)
616 goto error;
617 newbound = PyDict_New();
618 if (!newbound)
619 goto error;
620
621 if (ste->ste_type == ClassBlock) {
622 /* make a copy of globals before calling analyze_name(),
623 because global statements in the class have no effect
624 on nested functions.
625 */
626 if (PyDict_Update(newglobal, global) < 0)
627 goto error;
628 if (bound)
629 if (PyDict_Update(newbound, bound) < 0)
630 goto error;
631 }
632
633 assert(PySTEntry_Check(ste));
634 assert(PyDict_Check(ste->ste_symbols));
635 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000636 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 if (!analyze_name(ste, scope, name, flags, bound, local, free,
638 global))
639 goto error;
640 }
641
642 if (ste->ste_type != ClassBlock) {
643 if (ste->ste_type == FunctionBlock) {
644 if (PyDict_Update(newbound, local) < 0)
645 goto error;
646 }
647 if (bound) {
648 if (PyDict_Update(newbound, bound) < 0)
649 goto error;
650 }
651 if (PyDict_Update(newglobal, global) < 0)
652 goto error;
653 }
654
655 /* Recursively call analyze_block() on each child block */
656 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
657 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000658 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000660 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 if (!analyze_block(entry, newbound, newfree, newglobal))
662 goto error;
663 if (entry->ste_free || entry->ste_child_free)
664 ste->ste_child_free = 1;
665 }
666
667 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
668 goto error;
669 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
670 ste->ste_type == ClassBlock))
671 goto error;
672 if (!check_unoptimized(ste))
673 goto error;
674
675 if (PyDict_Update(free, newfree) < 0)
676 goto error;
677 success = 1;
678 error:
679 Py_XDECREF(local);
680 Py_XDECREF(scope);
681 Py_XDECREF(newbound);
682 Py_XDECREF(newglobal);
683 Py_XDECREF(newfree);
684 if (!success)
685 assert(PyErr_Occurred());
686 return success;
687}
688
689static int
690symtable_analyze(struct symtable *st)
691{
692 PyObject *free, *global;
693 int r;
694
695 free = PyDict_New();
696 if (!free)
697 return 0;
698 global = PyDict_New();
699 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000700 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 return 0;
702 }
703 r = analyze_block(st->st_top, NULL, free, global);
704 Py_DECREF(free);
705 Py_DECREF(global);
706 return r;
707}
708
709
710static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000711symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712{
713 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000714 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
716 PyErr_SetString(PyExc_SyntaxError, msg);
717 PyErr_SyntaxLocation(st->st_filename,
718 st->st_cur->ste_lineno);
719 }
720 return 0;
721 }
722 return 1;
723}
724
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000725/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000726 This reference is released when the block is exited, via the DECREF
727 in symtable_exit_block().
728*/
729
730static int
731symtable_exit_block(struct symtable *st, void *ast)
732{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000733 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000735 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736 end = PyList_GET_SIZE(st->st_stack) - 1;
737 if (end >= 0) {
738 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
739 end);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000740 if (st->st_cur == NULL)
741 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742 Py_INCREF(st->st_cur);
743 if (PySequence_DelItem(st->st_stack, end) < 0)
744 return 0;
745 }
746 return 1;
747}
748
749static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000750symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 void *ast, int lineno)
752{
753 PySTEntryObject *prev = NULL;
754
755 if (st->st_cur) {
756 prev = st->st_cur;
757 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758 return 0;
759 }
760 Py_DECREF(st->st_cur);
761 }
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000762 st->st_cur = ste_new(st, name, block, ast, lineno);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000763 if (st->st_cur == NULL)
764 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 if (name == GET_IDENTIFIER(top))
766 st->st_global = st->st_cur->ste_symbols;
767 if (prev) {
768 if (PyList_Append(prev->ste_children,
769 (PyObject *)st->st_cur) < 0) {
770 return 0;
771 }
772 }
773 return 1;
774}
775
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000776static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777symtable_lookup(struct symtable *st, PyObject *name)
778{
779 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000780 PyObject *mangled = _Py_Mangle(st->st_private, name);
781 if (!mangled)
782 return 0;
783 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
784 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785 if (!o)
786 return 0;
787 return PyInt_AsLong(o);
788}
789
790static int
791symtable_add_def(struct symtable *st, PyObject *name, int flag)
792{
793 PyObject *o;
794 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000795 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000796 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000798 if (!mangled)
799 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000801 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 val = PyInt_AS_LONG(o);
803 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000804 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000806 PyString_AsString(name));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 PyErr_SyntaxLocation(st->st_filename,
808 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000809 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 }
811 val |= flag;
812 } else
813 val = flag;
814 o = PyInt_FromLong(val);
815 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000816 goto error;
817 if (PyDict_SetItem(dict, 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
823 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000824 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
825 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 } else if (flag & DEF_GLOBAL) {
827 /* XXX need to update DEF_GLOBAL for other flags too;
828 perhaps only DEF_FREE_GLOBAL */
829 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000830 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 val |= PyInt_AS_LONG(o);
832 }
833 o = PyInt_FromLong(val);
834 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000835 goto error;
836 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000838 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 }
840 Py_DECREF(o);
841 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000842 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000844
845error:
846 Py_DECREF(mangled);
847 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848}
849
850/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
851 They use the ASDL name to synthesize the name of the C type and the visit
852 function.
853
854 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
855 useful if the first node in the sequence requires special treatment.
856*/
857
858#define VISIT(ST, TYPE, V) \
859 if (!symtable_visit_ ## TYPE((ST), (V))) \
860 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000861
862#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
863 if (!symtable_visit_ ## TYPE((ST), (V))) { \
864 symtable_exit_block((ST), (S)); \
865 return 0; \
866 }
867
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868#define VISIT_SEQ(ST, TYPE, SEQ) { \
869 int i; \
870 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
871 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000872 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 if (!symtable_visit_ ## TYPE((ST), elt)) \
874 return 0; \
875 } \
876}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000877
878#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
879 int i; \
880 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
881 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000882 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000883 if (!symtable_visit_ ## TYPE((ST), elt)) { \
884 symtable_exit_block((ST), (S)); \
885 return 0; \
886 } \
887 } \
888}
889
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
891 int i; \
892 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
893 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000894 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895 if (!symtable_visit_ ## TYPE((ST), elt)) \
896 return 0; \
897 } \
898}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000899
900#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
901 int i; \
902 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
903 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000904 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000905 if (!symtable_visit_ ## TYPE((ST), elt)) { \
906 symtable_exit_block((ST), (S)); \
907 return 0; \
908 } \
909 } \
910}
911
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000913symtable_new_tmpname(struct symtable *st)
914{
915 char tmpname[256];
916 identifier tmp;
917
918 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
919 ++st->st_cur->ste_tmpname);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000920 tmp = PyString_InternFromString(tmpname);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +0000921 if (!tmp)
922 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000923 if (!symtable_add_def(st, tmp, DEF_LOCAL))
924 return 0;
925 Py_DECREF(tmp);
926 return 1;
927}
928
929static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930symtable_visit_stmt(struct symtable *st, stmt_ty s)
931{
932 switch (s->kind) {
933 case FunctionDef_kind:
934 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
935 return 0;
936 if (s->v.FunctionDef.args->defaults)
937 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Christian Heimes5224d282008-02-23 15:01:05 +0000938 if (s->v.FunctionDef.decorator_list)
939 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 if (!symtable_enter_block(st, s->v.FunctionDef.name,
941 FunctionBlock, (void *)s, s->lineno))
942 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000943 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
944 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945 if (!symtable_exit_block(st, s))
946 return 0;
947 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000948 case ClassDef_kind: {
949 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
951 return 0;
952 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Christian Heimes5224d282008-02-23 15:01:05 +0000953 if (s->v.ClassDef.decorator_list)
954 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
956 (void *)s, s->lineno))
957 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000958 tmp = st->st_private;
959 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000960 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000961 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962 if (!symtable_exit_block(st, s))
963 return 0;
964 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000965 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 case Return_kind:
Georg Brandlddbaa662006-06-04 21:56:52 +0000967 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968 VISIT(st, expr, s->v.Return.value);
Georg Brandlddbaa662006-06-04 21:56:52 +0000969 st->st_cur->ste_returns_value = 1;
970 if (st->st_cur->ste_generator) {
971 PyErr_SetString(PyExc_SyntaxError,
972 RETURN_VAL_IN_GENERATOR);
973 PyErr_SyntaxLocation(st->st_filename,
974 s->lineno);
975 return 0;
976 }
977 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 break;
979 case Delete_kind:
980 VISIT_SEQ(st, expr, s->v.Delete.targets);
981 break;
982 case Assign_kind:
983 VISIT_SEQ(st, expr, s->v.Assign.targets);
984 VISIT(st, expr, s->v.Assign.value);
985 break;
986 case AugAssign_kind:
987 VISIT(st, expr, s->v.AugAssign.target);
988 VISIT(st, expr, s->v.AugAssign.value);
989 break;
990 case Print_kind:
991 if (s->v.Print.dest)
992 VISIT(st, expr, s->v.Print.dest);
993 VISIT_SEQ(st, expr, s->v.Print.values);
994 break;
995 case For_kind:
996 VISIT(st, expr, s->v.For.target);
997 VISIT(st, expr, s->v.For.iter);
998 VISIT_SEQ(st, stmt, s->v.For.body);
999 if (s->v.For.orelse)
1000 VISIT_SEQ(st, stmt, s->v.For.orelse);
1001 break;
1002 case While_kind:
1003 VISIT(st, expr, s->v.While.test);
1004 VISIT_SEQ(st, stmt, s->v.While.body);
1005 if (s->v.While.orelse)
1006 VISIT_SEQ(st, stmt, s->v.While.orelse);
1007 break;
1008 case If_kind:
1009 /* XXX if 0: and lookup_yield() hacks */
1010 VISIT(st, expr, s->v.If.test);
1011 VISIT_SEQ(st, stmt, s->v.If.body);
1012 if (s->v.If.orelse)
1013 VISIT_SEQ(st, stmt, s->v.If.orelse);
1014 break;
1015 case Raise_kind:
1016 if (s->v.Raise.type) {
1017 VISIT(st, expr, s->v.Raise.type);
1018 if (s->v.Raise.inst) {
1019 VISIT(st, expr, s->v.Raise.inst);
1020 if (s->v.Raise.tback)
1021 VISIT(st, expr, s->v.Raise.tback);
1022 }
1023 }
1024 break;
1025 case TryExcept_kind:
1026 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1027 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1028 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1029 break;
1030 case TryFinally_kind:
1031 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1032 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1033 break;
1034 case Assert_kind:
1035 VISIT(st, expr, s->v.Assert.test);
1036 if (s->v.Assert.msg)
1037 VISIT(st, expr, s->v.Assert.msg);
1038 break;
1039 case Import_kind:
1040 VISIT_SEQ(st, alias, s->v.Import.names);
1041 /* XXX Don't have the lineno available inside
1042 visit_alias */
1043 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1044 st->st_cur->ste_opt_lineno = s->lineno;
1045 break;
1046 case ImportFrom_kind:
1047 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1048 /* XXX Don't have the lineno available inside
1049 visit_alias */
1050 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1051 st->st_cur->ste_opt_lineno = s->lineno;
1052 break;
1053 case Exec_kind:
1054 VISIT(st, expr, s->v.Exec.body);
1055 if (!st->st_cur->ste_opt_lineno)
1056 st->st_cur->ste_opt_lineno = s->lineno;
1057 if (s->v.Exec.globals) {
1058 st->st_cur->ste_unoptimized |= OPT_EXEC;
1059 VISIT(st, expr, s->v.Exec.globals);
1060 if (s->v.Exec.locals)
1061 VISIT(st, expr, s->v.Exec.locals);
1062 } else {
1063 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1064 }
1065 break;
1066 case Global_kind: {
1067 int i;
1068 asdl_seq *seq = s->v.Global.names;
1069 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001070 identifier name = (identifier)asdl_seq_GET(seq, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001071 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001072 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 if (cur < 0)
1074 return 0;
1075 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001076 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 if (cur & DEF_LOCAL)
1078 PyOS_snprintf(buf, sizeof(buf),
1079 GLOBAL_AFTER_ASSIGN,
1080 c_name);
1081 else
1082 PyOS_snprintf(buf, sizeof(buf),
1083 GLOBAL_AFTER_USE,
1084 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001085 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 return 0;
1087 }
1088 if (!symtable_add_def(st, name, DEF_GLOBAL))
1089 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 break;
1092 }
1093 case Expr_kind:
1094 VISIT(st, expr, s->v.Expr.value);
1095 break;
1096 case Pass_kind:
1097 case Break_kind:
1098 case Continue_kind:
1099 /* nothing to do here */
1100 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001101 case With_kind:
1102 if (!symtable_new_tmpname(st))
1103 return 0;
1104 VISIT(st, expr, s->v.With.context_expr);
1105 if (s->v.With.optional_vars) {
1106 if (!symtable_new_tmpname(st))
1107 return 0;
1108 VISIT(st, expr, s->v.With.optional_vars);
1109 }
1110 VISIT_SEQ(st, stmt, s->v.With.body);
1111 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 }
1113 return 1;
1114}
1115
1116static int
1117symtable_visit_expr(struct symtable *st, expr_ty e)
1118{
1119 switch (e->kind) {
1120 case BoolOp_kind:
1121 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1122 break;
1123 case BinOp_kind:
1124 VISIT(st, expr, e->v.BinOp.left);
1125 VISIT(st, expr, e->v.BinOp.right);
1126 break;
1127 case UnaryOp_kind:
1128 VISIT(st, expr, e->v.UnaryOp.operand);
1129 break;
1130 case Lambda_kind: {
Neal Norwitz76059362006-08-19 04:52:03 +00001131 if (!GET_IDENTIFIER(lambda) ||
1132 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 return 0;
1134 if (e->v.Lambda.args->defaults)
1135 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1136 /* XXX how to get line numbers for expressions */
Neal Norwitz76059362006-08-19 04:52:03 +00001137 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 FunctionBlock, (void *)e, 0))
1139 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001140 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1141 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 if (!symtable_exit_block(st, (void *)e))
1143 return 0;
1144 break;
1145 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001146 case IfExp_kind:
1147 VISIT(st, expr, e->v.IfExp.test);
1148 VISIT(st, expr, e->v.IfExp.body);
1149 VISIT(st, expr, e->v.IfExp.orelse);
1150 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 case Dict_kind:
1152 VISIT_SEQ(st, expr, e->v.Dict.keys);
1153 VISIT_SEQ(st, expr, e->v.Dict.values);
1154 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001155 case ListComp_kind:
1156 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 return 0;
1158 VISIT(st, expr, e->v.ListComp.elt);
1159 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1160 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001161 case GeneratorExp_kind:
1162 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 case Yield_kind:
1166 if (e->v.Yield.value)
1167 VISIT(st, expr, e->v.Yield.value);
1168 st->st_cur->ste_generator = 1;
Georg Brandlddbaa662006-06-04 21:56:52 +00001169 if (st->st_cur->ste_returns_value) {
1170 PyErr_SetString(PyExc_SyntaxError,
1171 RETURN_VAL_IN_GENERATOR);
1172 PyErr_SyntaxLocation(st->st_filename,
1173 e->lineno);
1174 return 0;
1175 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 break;
1177 case Compare_kind:
1178 VISIT(st, expr, e->v.Compare.left);
1179 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1180 break;
1181 case Call_kind:
1182 VISIT(st, expr, e->v.Call.func);
1183 VISIT_SEQ(st, expr, e->v.Call.args);
1184 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1185 if (e->v.Call.starargs)
1186 VISIT(st, expr, e->v.Call.starargs);
1187 if (e->v.Call.kwargs)
1188 VISIT(st, expr, e->v.Call.kwargs);
1189 break;
1190 case Repr_kind:
1191 VISIT(st, expr, e->v.Repr.value);
1192 break;
1193 case Num_kind:
1194 case Str_kind:
1195 /* Nothing to do here. */
1196 break;
1197 /* The following exprs can be assignment targets. */
1198 case Attribute_kind:
1199 VISIT(st, expr, e->v.Attribute.value);
1200 break;
1201 case Subscript_kind:
1202 VISIT(st, expr, e->v.Subscript.value);
1203 VISIT(st, slice, e->v.Subscript.slice);
1204 break;
1205 case Name_kind:
1206 if (!symtable_add_def(st, e->v.Name.id,
1207 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1208 return 0;
1209 break;
1210 /* child nodes of List and Tuple will have expr_context set */
1211 case List_kind:
1212 VISIT_SEQ(st, expr, e->v.List.elts);
1213 break;
1214 case Tuple_kind:
1215 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1216 break;
1217 }
1218 return 1;
1219}
1220
1221static int
1222symtable_implicit_arg(struct symtable *st, int pos)
1223{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001224 PyObject *id = PyString_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 if (id == NULL)
1226 return 0;
1227 if (!symtable_add_def(st, id, DEF_PARAM)) {
1228 Py_DECREF(id);
1229 return 0;
1230 }
1231 Py_DECREF(id);
1232 return 1;
1233}
1234
1235static int
1236symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1237{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001238 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239
1240 /* go through all the toplevel arguments first */
1241 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001242 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 if (arg->kind == Name_kind) {
1244 assert(arg->v.Name.ctx == Param ||
1245 (arg->v.Name.ctx == Store && !toplevel));
1246 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1247 return 0;
1248 }
1249 else if (arg->kind == Tuple_kind) {
1250 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 if (toplevel) {
1252 if (!symtable_implicit_arg(st, i))
1253 return 0;
1254 }
1255 }
1256 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001257 PyErr_SetString(PyExc_SyntaxError,
1258 "invalid expression in parameter list");
1259 PyErr_SyntaxLocation(st->st_filename,
1260 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 return 0;
1262 }
1263 }
1264
1265 if (!toplevel) {
1266 if (!symtable_visit_params_nested(st, args))
1267 return 0;
1268 }
1269
1270 return 1;
1271}
1272
1273static int
1274symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1275{
1276 int i;
1277 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001278 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 if (arg->kind == Tuple_kind &&
1280 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1281 return 0;
1282 }
1283
1284 return 1;
1285}
1286
1287static int
1288symtable_visit_arguments(struct symtable *st, arguments_ty a)
1289{
1290 /* skip default arguments inside function block
1291 XXX should ast be different?
1292 */
1293 if (a->args && !symtable_visit_params(st, a->args, 1))
1294 return 0;
1295 if (a->vararg) {
1296 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1297 return 0;
1298 st->st_cur->ste_varargs = 1;
1299 }
1300 if (a->kwarg) {
1301 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1302 return 0;
1303 st->st_cur->ste_varkeywords = 1;
1304 }
1305 if (a->args && !symtable_visit_params_nested(st, a->args))
1306 return 0;
1307 return 1;
1308}
1309
1310
1311static int
1312symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1313{
Georg Brandla48f3ab2008-03-30 06:40:17 +00001314 if (eh->v.ExceptHandler.type)
1315 VISIT(st, expr, eh->v.ExceptHandler.type);
1316 if (eh->v.ExceptHandler.name)
1317 VISIT(st, expr, eh->v.ExceptHandler.name);
1318 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319 return 1;
1320}
1321
1322
1323static int
1324symtable_visit_alias(struct symtable *st, alias_ty a)
1325{
1326 /* Compute store_name, the name actually bound by the import
1327 operation. It is diferent than a->name when a->name is a
1328 dotted package name (e.g. spam.eggs)
1329 */
1330 PyObject *store_name;
1331 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001332 const char *base = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 char *dot = strchr(base, '.');
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001334 if (dot) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001335 store_name = PyString_FromStringAndSize(base, dot - base);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001336 if (!store_name)
1337 return 0;
1338 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 else {
1340 store_name = name;
1341 Py_INCREF(store_name);
1342 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001343 if (strcmp(PyString_AS_STRING(name), "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1345 Py_DECREF(store_name);
1346 return r;
1347 }
1348 else {
1349 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001350 int lineno = st->st_cur->ste_lineno;
1351 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001352 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001354 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 }
1356 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001357 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 return 1;
1359 }
1360}
1361
1362
1363static int
1364symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1365{
1366 VISIT(st, expr, lc->target);
1367 VISIT(st, expr, lc->iter);
1368 VISIT_SEQ(st, expr, lc->ifs);
1369 return 1;
1370}
1371
1372
1373static int
1374symtable_visit_keyword(struct symtable *st, keyword_ty k)
1375{
1376 VISIT(st, expr, k->value);
1377 return 1;
1378}
1379
1380
1381static int
1382symtable_visit_slice(struct symtable *st, slice_ty s)
1383{
1384 switch (s->kind) {
1385 case Slice_kind:
1386 if (s->v.Slice.lower)
1387 VISIT(st, expr, s->v.Slice.lower)
1388 if (s->v.Slice.upper)
1389 VISIT(st, expr, s->v.Slice.upper)
1390 if (s->v.Slice.step)
1391 VISIT(st, expr, s->v.Slice.step)
1392 break;
1393 case ExtSlice_kind:
1394 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1395 break;
1396 case Index_kind:
1397 VISIT(st, expr, s->v.Index.value)
1398 break;
1399 case Ellipsis_kind:
1400 break;
1401 }
1402 return 1;
1403}
1404
1405static int
1406symtable_visit_genexp(struct symtable *st, expr_ty e)
1407{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 comprehension_ty outermost = ((comprehension_ty)
1409 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1410 /* Outermost iterator is evaluated in current scope */
1411 VISIT(st, expr, outermost->iter);
1412 /* Create generator scope for the rest */
Neal Norwitz76059362006-08-19 04:52:03 +00001413 if (!GET_IDENTIFIER(genexpr) ||
1414 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 return 0;
1416 }
1417 st->st_cur->ste_generator = 1;
1418 /* Outermost iter is received as an argument */
1419 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001420 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421 return 0;
1422 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001423 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1424 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1425 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1426 e->v.GeneratorExp.generators, 1, (void*)e);
1427 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Neal Norwitz76059362006-08-19 04:52:03 +00001428 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429}