blob: c0201c368b5a190369adb8fcdfca993edbca79d1 [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
Neal Norwitz090b3dd2006-02-28 22:36:46 +000019/* XXX(nnorwitz): change name since static? */
20static PySTEntryObject *
Neal Norwitz62c2fac2005-10-24 00:30:44 +000021PySTEntry_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>",
Christian Heimes593daf52008-05-26 12:51:38 +000090 PyBytes_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Christian Heimes593daf52008-05-26 12:51:38 +000092 return PyBytes_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) \
Christian Heimes593daf52008-05-26 12:51:38 +0000183 ((VAR) ? (VAR) : ((VAR) = PyBytes_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",
Christian Heimes593daf52008-05-26 12:51:38 +0000375 PyBytes_AS_STRING(name));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376 return 0;
377 }
378 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
379 if (PyDict_SetItem(global, name, Py_None) < 0)
380 return 0;
381 if (bound && PyDict_GetItem(bound, name)) {
382 if (PyDict_DelItem(bound, name) < 0)
383 return 0;
384 }
385 return 1;
386 }
387 if (flags & DEF_BOUND) {
388 SET_SCOPE(dict, name, LOCAL);
389 if (PyDict_SetItem(local, name, Py_None) < 0)
390 return 0;
391 if (PyDict_GetItem(global, name)) {
392 if (PyDict_DelItem(global, name) < 0)
393 return 0;
394 }
395 return 1;
396 }
397 /* If an enclosing block has a binding for this name, it
398 is a free variable rather than a global variable.
399 Note that having a non-NULL bound implies that the block
400 is nested.
401 */
402 if (bound && PyDict_GetItem(bound, name)) {
403 SET_SCOPE(dict, name, FREE);
404 ste->ste_free = 1;
405 if (PyDict_SetItem(free, name, Py_None) < 0)
406 return 0;
407 return 1;
408 }
409 /* If a parent has a global statement, then call it global
410 explicit? It could also be global implicit.
411 */
412 else if (global && PyDict_GetItem(global, name)) {
413 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
414 return 1;
415 }
416 else {
417 if (ste->ste_nested)
418 ste->ste_free = 1;
419 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
420 return 1;
421 }
422 return 0; /* Can't get here */
423}
424
425#undef SET_SCOPE
426
427/* If a name is defined in free and also in locals, then this block
428 provides the binding for the free variable. The name should be
429 marked CELL in this block and removed from the free list.
430
431 Note that the current block's free variables are included in free.
432 That's safe because no name can be free and local in the same scope.
433*/
434
435static int
436analyze_cells(PyObject *scope, PyObject *free)
437{
438 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000439 int success = 0;
440 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441
442 w = PyInt_FromLong(CELL);
443 if (!w)
444 return 0;
445 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000446 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000447 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000448 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449 if (flags != LOCAL)
450 continue;
451 if (!PyDict_GetItem(free, name))
452 continue;
453 /* Replace LOCAL with CELL for this name, and remove
454 from free. It is safe to replace the value of name
455 in the dict, because it will not cause a resize.
456 */
457 if (PyDict_SetItem(scope, name, w) < 0)
458 goto error;
459 if (!PyDict_DelItem(free, name) < 0)
460 goto error;
461 }
462 success = 1;
463 error:
464 Py_DECREF(w);
465 return success;
466}
467
468/* Check for illegal statements in unoptimized namespaces */
469static int
470check_unoptimized(const PySTEntryObject* ste) {
471 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000472 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000474 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475 || !(ste->ste_free || ste->ste_child_free))
476 return 1;
477
Armin Rigo31441302005-10-21 12:57:31 +0000478 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479 "contains a nested function with free variables" :
480 "is a nested function");
481
482 switch (ste->ste_unoptimized) {
483 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
484 case OPT_EXEC: /* qualified exec is fine */
485 return 1;
486 case OPT_IMPORT_STAR:
487 PyOS_snprintf(buf, sizeof(buf),
488 "import * is not allowed in function '%.100s' "
489 "because it is %s",
Christian Heimes593daf52008-05-26 12:51:38 +0000490 PyBytes_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000491 break;
492 case OPT_BARE_EXEC:
493 PyOS_snprintf(buf, sizeof(buf),
494 "unqualified exec is not allowed in function "
495 "'%.100s' it %s",
Christian Heimes593daf52008-05-26 12:51:38 +0000496 PyBytes_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497 break;
498 default:
499 PyOS_snprintf(buf, sizeof(buf),
500 "function '%.100s' uses import * and bare exec, "
501 "which are illegal because it %s",
Christian Heimes593daf52008-05-26 12:51:38 +0000502 PyBytes_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000503 break;
504 }
505
506 PyErr_SetString(PyExc_SyntaxError, buf);
507 PyErr_SyntaxLocation(ste->ste_table->st_filename,
508 ste->ste_opt_lineno);
509 return 0;
510}
511
512/* Enter the final scope information into the st_symbols dict.
513 *
514 * All arguments are dicts. Modifies symbols, others are read-only.
515*/
516static int
517update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000518 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519{
520 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000521 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522
523 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000524 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525 assert(PyInt_Check(v));
526 flags = PyInt_AS_LONG(v);
527 w = PyDict_GetItem(scope, name);
528 assert(w && PyInt_Check(w));
529 i = PyInt_AS_LONG(w);
530 flags |= (i << SCOPE_OFF);
531 u = PyInt_FromLong(flags);
Neal Norwitz18b6adf2006-07-23 07:50:36 +0000532 if (!u)
533 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534 if (PyDict_SetItem(symbols, name, u) < 0) {
535 Py_DECREF(u);
536 return 0;
537 }
538 Py_DECREF(u);
539 }
540
541 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
542 if (!free_value)
543 return 0;
544
545 /* add a free variable when it's only use is for creating a closure */
546 pos = 0;
547 while (PyDict_Next(free, &pos, &name, &v)) {
548 PyObject *o = PyDict_GetItem(symbols, name);
549
550 if (o) {
551 /* It could be a free variable in a method of
552 the class that has the same name as a local
553 or global in the class scope.
554 */
Anthony Baxter019aec62006-04-12 04:00:50 +0000555 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000557 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000558 o = PyInt_FromLong(i);
559 if (!o) {
560 Py_DECREF(free_value);
561 return 0;
562 }
563 if (PyDict_SetItem(symbols, name, o) < 0) {
564 Py_DECREF(o);
565 Py_DECREF(free_value);
566 return 0;
567 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000568 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569 }
570 /* else it's not free, probably a cell */
571 continue;
572 }
573 if (!PyDict_GetItem(bound, name))
574 continue; /* it's a global */
575
576 if (PyDict_SetItem(symbols, name, free_value) < 0) {
577 Py_DECREF(free_value);
578 return 0;
579 }
580 }
581 Py_DECREF(free_value);
582 return 1;
583}
584
585/* Make final symbol table decisions for block of ste.
586 Arguments:
587 ste -- current symtable entry (input/output)
588 bound -- set of variables bound in enclosing scopes (input)
589 free -- set of free variables in enclosed scopes (output)
590 globals -- set of declared global variables in enclosing scopes (input)
591*/
592
593static int
594analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
595 PyObject *global)
596{
597 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
598 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000599 int i, success = 0;
600 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601
602 local = PyDict_New();
603 if (!local)
604 goto error;
605 scope = PyDict_New();
606 if (!scope)
607 goto error;
608 newglobal = PyDict_New();
609 if (!newglobal)
610 goto error;
611 newfree = PyDict_New();
612 if (!newfree)
613 goto error;
614 newbound = PyDict_New();
615 if (!newbound)
616 goto error;
617
618 if (ste->ste_type == ClassBlock) {
619 /* make a copy of globals before calling analyze_name(),
620 because global statements in the class have no effect
621 on nested functions.
622 */
623 if (PyDict_Update(newglobal, global) < 0)
624 goto error;
625 if (bound)
626 if (PyDict_Update(newbound, bound) < 0)
627 goto error;
628 }
629
630 assert(PySTEntry_Check(ste));
631 assert(PyDict_Check(ste->ste_symbols));
632 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000633 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 if (!analyze_name(ste, scope, name, flags, bound, local, free,
635 global))
636 goto error;
637 }
638
639 if (ste->ste_type != ClassBlock) {
640 if (ste->ste_type == FunctionBlock) {
641 if (PyDict_Update(newbound, local) < 0)
642 goto error;
643 }
644 if (bound) {
645 if (PyDict_Update(newbound, bound) < 0)
646 goto error;
647 }
648 if (PyDict_Update(newglobal, global) < 0)
649 goto error;
650 }
651
652 /* Recursively call analyze_block() on each child block */
653 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
654 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000655 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000657 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658 if (!analyze_block(entry, newbound, newfree, newglobal))
659 goto error;
660 if (entry->ste_free || entry->ste_child_free)
661 ste->ste_child_free = 1;
662 }
663
664 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
665 goto error;
666 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
667 ste->ste_type == ClassBlock))
668 goto error;
669 if (!check_unoptimized(ste))
670 goto error;
671
672 if (PyDict_Update(free, newfree) < 0)
673 goto error;
674 success = 1;
675 error:
676 Py_XDECREF(local);
677 Py_XDECREF(scope);
678 Py_XDECREF(newbound);
679 Py_XDECREF(newglobal);
680 Py_XDECREF(newfree);
681 if (!success)
682 assert(PyErr_Occurred());
683 return success;
684}
685
686static int
687symtable_analyze(struct symtable *st)
688{
689 PyObject *free, *global;
690 int r;
691
692 free = PyDict_New();
693 if (!free)
694 return 0;
695 global = PyDict_New();
696 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000697 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698 return 0;
699 }
700 r = analyze_block(st->st_top, NULL, free, global);
701 Py_DECREF(free);
702 Py_DECREF(global);
703 return r;
704}
705
706
707static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000708symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709{
710 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000711 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
713 PyErr_SetString(PyExc_SyntaxError, msg);
714 PyErr_SyntaxLocation(st->st_filename,
715 st->st_cur->ste_lineno);
716 }
717 return 0;
718 }
719 return 1;
720}
721
722/* symtable_enter_block() gets a reference via PySTEntry_New().
723 This reference is released when the block is exited, via the DECREF
724 in symtable_exit_block().
725*/
726
727static int
728symtable_exit_block(struct symtable *st, void *ast)
729{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000730 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000732 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000733 end = PyList_GET_SIZE(st->st_stack) - 1;
734 if (end >= 0) {
735 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
736 end);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000737 if (st->st_cur == NULL)
738 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 Py_INCREF(st->st_cur);
740 if (PySequence_DelItem(st->st_stack, end) < 0)
741 return 0;
742 }
743 return 1;
744}
745
746static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000747symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 void *ast, int lineno)
749{
750 PySTEntryObject *prev = NULL;
751
752 if (st->st_cur) {
753 prev = st->st_cur;
754 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 return 0;
756 }
757 Py_DECREF(st->st_cur);
758 }
759 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000760 if (st->st_cur == NULL)
761 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762 if (name == GET_IDENTIFIER(top))
763 st->st_global = st->st_cur->ste_symbols;
764 if (prev) {
765 if (PyList_Append(prev->ste_children,
766 (PyObject *)st->st_cur) < 0) {
767 return 0;
768 }
769 }
770 return 1;
771}
772
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000773static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774symtable_lookup(struct symtable *st, PyObject *name)
775{
776 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000777 PyObject *mangled = _Py_Mangle(st->st_private, name);
778 if (!mangled)
779 return 0;
780 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
781 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 if (!o)
783 return 0;
784 return PyInt_AsLong(o);
785}
786
787static int
788symtable_add_def(struct symtable *st, PyObject *name, int flag)
789{
790 PyObject *o;
791 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000792 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000793 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000795 if (!mangled)
796 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000798 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 val = PyInt_AS_LONG(o);
800 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000801 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Christian Heimes593daf52008-05-26 12:51:38 +0000803 PyBytes_AsString(name));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 PyErr_SyntaxLocation(st->st_filename,
805 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000806 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 }
808 val |= flag;
809 } else
810 val = flag;
811 o = PyInt_FromLong(val);
812 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000813 goto error;
814 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000816 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 }
818 Py_DECREF(o);
819
820 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000821 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
822 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 } else if (flag & DEF_GLOBAL) {
824 /* XXX need to update DEF_GLOBAL for other flags too;
825 perhaps only DEF_FREE_GLOBAL */
826 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000827 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 val |= PyInt_AS_LONG(o);
829 }
830 o = PyInt_FromLong(val);
831 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000832 goto error;
833 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000835 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 }
837 Py_DECREF(o);
838 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000839 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000841
842error:
843 Py_DECREF(mangled);
844 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845}
846
847/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
848 They use the ASDL name to synthesize the name of the C type and the visit
849 function.
850
851 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
852 useful if the first node in the sequence requires special treatment.
853*/
854
855#define VISIT(ST, TYPE, V) \
856 if (!symtable_visit_ ## TYPE((ST), (V))) \
857 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000858
859#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
860 if (!symtable_visit_ ## TYPE((ST), (V))) { \
861 symtable_exit_block((ST), (S)); \
862 return 0; \
863 }
864
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865#define VISIT_SEQ(ST, TYPE, SEQ) { \
866 int i; \
867 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
868 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000869 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 if (!symtable_visit_ ## TYPE((ST), elt)) \
871 return 0; \
872 } \
873}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000874
875#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
876 int i; \
877 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
878 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000879 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000880 if (!symtable_visit_ ## TYPE((ST), elt)) { \
881 symtable_exit_block((ST), (S)); \
882 return 0; \
883 } \
884 } \
885}
886
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
888 int i; \
889 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
890 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000891 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 if (!symtable_visit_ ## TYPE((ST), elt)) \
893 return 0; \
894 } \
895}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000896
897#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
898 int i; \
899 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
900 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000901 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000902 if (!symtable_visit_ ## TYPE((ST), elt)) { \
903 symtable_exit_block((ST), (S)); \
904 return 0; \
905 } \
906 } \
907}
908
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000910symtable_new_tmpname(struct symtable *st)
911{
912 char tmpname[256];
913 identifier tmp;
914
915 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
916 ++st->st_cur->ste_tmpname);
Christian Heimes593daf52008-05-26 12:51:38 +0000917 tmp = PyBytes_InternFromString(tmpname);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +0000918 if (!tmp)
919 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000920 if (!symtable_add_def(st, tmp, DEF_LOCAL))
921 return 0;
922 Py_DECREF(tmp);
923 return 1;
924}
925
926static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927symtable_visit_stmt(struct symtable *st, stmt_ty s)
928{
929 switch (s->kind) {
930 case FunctionDef_kind:
931 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
932 return 0;
933 if (s->v.FunctionDef.args->defaults)
934 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Christian Heimes5224d282008-02-23 15:01:05 +0000935 if (s->v.FunctionDef.decorator_list)
936 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937 if (!symtable_enter_block(st, s->v.FunctionDef.name,
938 FunctionBlock, (void *)s, s->lineno))
939 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000940 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
941 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 if (!symtable_exit_block(st, s))
943 return 0;
944 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000945 case ClassDef_kind: {
946 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
948 return 0;
949 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Christian Heimes5224d282008-02-23 15:01:05 +0000950 if (s->v.ClassDef.decorator_list)
951 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
953 (void *)s, s->lineno))
954 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000955 tmp = st->st_private;
956 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000957 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000958 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959 if (!symtable_exit_block(st, s))
960 return 0;
961 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000962 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 case Return_kind:
Georg Brandlddbaa662006-06-04 21:56:52 +0000964 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 VISIT(st, expr, s->v.Return.value);
Georg Brandlddbaa662006-06-04 21:56:52 +0000966 st->st_cur->ste_returns_value = 1;
967 if (st->st_cur->ste_generator) {
968 PyErr_SetString(PyExc_SyntaxError,
969 RETURN_VAL_IN_GENERATOR);
970 PyErr_SyntaxLocation(st->st_filename,
971 s->lineno);
972 return 0;
973 }
974 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 break;
976 case Delete_kind:
977 VISIT_SEQ(st, expr, s->v.Delete.targets);
978 break;
979 case Assign_kind:
980 VISIT_SEQ(st, expr, s->v.Assign.targets);
981 VISIT(st, expr, s->v.Assign.value);
982 break;
983 case AugAssign_kind:
984 VISIT(st, expr, s->v.AugAssign.target);
985 VISIT(st, expr, s->v.AugAssign.value);
986 break;
987 case Print_kind:
988 if (s->v.Print.dest)
989 VISIT(st, expr, s->v.Print.dest);
990 VISIT_SEQ(st, expr, s->v.Print.values);
991 break;
992 case For_kind:
993 VISIT(st, expr, s->v.For.target);
994 VISIT(st, expr, s->v.For.iter);
995 VISIT_SEQ(st, stmt, s->v.For.body);
996 if (s->v.For.orelse)
997 VISIT_SEQ(st, stmt, s->v.For.orelse);
998 break;
999 case While_kind:
1000 VISIT(st, expr, s->v.While.test);
1001 VISIT_SEQ(st, stmt, s->v.While.body);
1002 if (s->v.While.orelse)
1003 VISIT_SEQ(st, stmt, s->v.While.orelse);
1004 break;
1005 case If_kind:
1006 /* XXX if 0: and lookup_yield() hacks */
1007 VISIT(st, expr, s->v.If.test);
1008 VISIT_SEQ(st, stmt, s->v.If.body);
1009 if (s->v.If.orelse)
1010 VISIT_SEQ(st, stmt, s->v.If.orelse);
1011 break;
1012 case Raise_kind:
1013 if (s->v.Raise.type) {
1014 VISIT(st, expr, s->v.Raise.type);
1015 if (s->v.Raise.inst) {
1016 VISIT(st, expr, s->v.Raise.inst);
1017 if (s->v.Raise.tback)
1018 VISIT(st, expr, s->v.Raise.tback);
1019 }
1020 }
1021 break;
1022 case TryExcept_kind:
1023 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1024 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1025 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1026 break;
1027 case TryFinally_kind:
1028 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1029 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1030 break;
1031 case Assert_kind:
1032 VISIT(st, expr, s->v.Assert.test);
1033 if (s->v.Assert.msg)
1034 VISIT(st, expr, s->v.Assert.msg);
1035 break;
1036 case Import_kind:
1037 VISIT_SEQ(st, alias, s->v.Import.names);
1038 /* XXX Don't have the lineno available inside
1039 visit_alias */
1040 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1041 st->st_cur->ste_opt_lineno = s->lineno;
1042 break;
1043 case ImportFrom_kind:
1044 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1045 /* XXX Don't have the lineno available inside
1046 visit_alias */
1047 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1048 st->st_cur->ste_opt_lineno = s->lineno;
1049 break;
1050 case Exec_kind:
1051 VISIT(st, expr, s->v.Exec.body);
1052 if (!st->st_cur->ste_opt_lineno)
1053 st->st_cur->ste_opt_lineno = s->lineno;
1054 if (s->v.Exec.globals) {
1055 st->st_cur->ste_unoptimized |= OPT_EXEC;
1056 VISIT(st, expr, s->v.Exec.globals);
1057 if (s->v.Exec.locals)
1058 VISIT(st, expr, s->v.Exec.locals);
1059 } else {
1060 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1061 }
1062 break;
1063 case Global_kind: {
1064 int i;
1065 asdl_seq *seq = s->v.Global.names;
1066 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001067 identifier name = (identifier)asdl_seq_GET(seq, i);
Christian Heimes593daf52008-05-26 12:51:38 +00001068 char *c_name = PyBytes_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001069 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 if (cur < 0)
1071 return 0;
1072 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001073 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 if (cur & DEF_LOCAL)
1075 PyOS_snprintf(buf, sizeof(buf),
1076 GLOBAL_AFTER_ASSIGN,
1077 c_name);
1078 else
1079 PyOS_snprintf(buf, sizeof(buf),
1080 GLOBAL_AFTER_USE,
1081 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001082 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 return 0;
1084 }
1085 if (!symtable_add_def(st, name, DEF_GLOBAL))
1086 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 break;
1089 }
1090 case Expr_kind:
1091 VISIT(st, expr, s->v.Expr.value);
1092 break;
1093 case Pass_kind:
1094 case Break_kind:
1095 case Continue_kind:
1096 /* nothing to do here */
1097 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001098 case With_kind:
1099 if (!symtable_new_tmpname(st))
1100 return 0;
1101 VISIT(st, expr, s->v.With.context_expr);
1102 if (s->v.With.optional_vars) {
1103 if (!symtable_new_tmpname(st))
1104 return 0;
1105 VISIT(st, expr, s->v.With.optional_vars);
1106 }
1107 VISIT_SEQ(st, stmt, s->v.With.body);
1108 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109 }
1110 return 1;
1111}
1112
1113static int
1114symtable_visit_expr(struct symtable *st, expr_ty e)
1115{
1116 switch (e->kind) {
1117 case BoolOp_kind:
1118 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1119 break;
1120 case BinOp_kind:
1121 VISIT(st, expr, e->v.BinOp.left);
1122 VISIT(st, expr, e->v.BinOp.right);
1123 break;
1124 case UnaryOp_kind:
1125 VISIT(st, expr, e->v.UnaryOp.operand);
1126 break;
1127 case Lambda_kind: {
Neal Norwitz76059362006-08-19 04:52:03 +00001128 if (!GET_IDENTIFIER(lambda) ||
1129 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 return 0;
1131 if (e->v.Lambda.args->defaults)
1132 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1133 /* XXX how to get line numbers for expressions */
Neal Norwitz76059362006-08-19 04:52:03 +00001134 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 FunctionBlock, (void *)e, 0))
1136 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001137 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1138 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 if (!symtable_exit_block(st, (void *)e))
1140 return 0;
1141 break;
1142 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001143 case IfExp_kind:
1144 VISIT(st, expr, e->v.IfExp.test);
1145 VISIT(st, expr, e->v.IfExp.body);
1146 VISIT(st, expr, e->v.IfExp.orelse);
1147 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 case Dict_kind:
1149 VISIT_SEQ(st, expr, e->v.Dict.keys);
1150 VISIT_SEQ(st, expr, e->v.Dict.values);
1151 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001152 case ListComp_kind:
1153 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return 0;
1155 VISIT(st, expr, e->v.ListComp.elt);
1156 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1157 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001158 case GeneratorExp_kind:
1159 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 case Yield_kind:
1163 if (e->v.Yield.value)
1164 VISIT(st, expr, e->v.Yield.value);
1165 st->st_cur->ste_generator = 1;
Georg Brandlddbaa662006-06-04 21:56:52 +00001166 if (st->st_cur->ste_returns_value) {
1167 PyErr_SetString(PyExc_SyntaxError,
1168 RETURN_VAL_IN_GENERATOR);
1169 PyErr_SyntaxLocation(st->st_filename,
1170 e->lineno);
1171 return 0;
1172 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 break;
1174 case Compare_kind:
1175 VISIT(st, expr, e->v.Compare.left);
1176 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1177 break;
1178 case Call_kind:
1179 VISIT(st, expr, e->v.Call.func);
1180 VISIT_SEQ(st, expr, e->v.Call.args);
1181 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1182 if (e->v.Call.starargs)
1183 VISIT(st, expr, e->v.Call.starargs);
1184 if (e->v.Call.kwargs)
1185 VISIT(st, expr, e->v.Call.kwargs);
1186 break;
1187 case Repr_kind:
1188 VISIT(st, expr, e->v.Repr.value);
1189 break;
1190 case Num_kind:
1191 case Str_kind:
1192 /* Nothing to do here. */
1193 break;
1194 /* The following exprs can be assignment targets. */
1195 case Attribute_kind:
1196 VISIT(st, expr, e->v.Attribute.value);
1197 break;
1198 case Subscript_kind:
1199 VISIT(st, expr, e->v.Subscript.value);
1200 VISIT(st, slice, e->v.Subscript.slice);
1201 break;
1202 case Name_kind:
1203 if (!symtable_add_def(st, e->v.Name.id,
1204 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1205 return 0;
1206 break;
1207 /* child nodes of List and Tuple will have expr_context set */
1208 case List_kind:
1209 VISIT_SEQ(st, expr, e->v.List.elts);
1210 break;
1211 case Tuple_kind:
1212 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1213 break;
1214 }
1215 return 1;
1216}
1217
1218static int
1219symtable_implicit_arg(struct symtable *st, int pos)
1220{
Christian Heimes593daf52008-05-26 12:51:38 +00001221 PyObject *id = PyBytes_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 if (id == NULL)
1223 return 0;
1224 if (!symtable_add_def(st, id, DEF_PARAM)) {
1225 Py_DECREF(id);
1226 return 0;
1227 }
1228 Py_DECREF(id);
1229 return 1;
1230}
1231
1232static int
1233symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1234{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001235 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236
1237 /* go through all the toplevel arguments first */
1238 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001239 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 if (arg->kind == Name_kind) {
1241 assert(arg->v.Name.ctx == Param ||
1242 (arg->v.Name.ctx == Store && !toplevel));
1243 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1244 return 0;
1245 }
1246 else if (arg->kind == Tuple_kind) {
1247 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 if (toplevel) {
1249 if (!symtable_implicit_arg(st, i))
1250 return 0;
1251 }
1252 }
1253 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001254 PyErr_SetString(PyExc_SyntaxError,
1255 "invalid expression in parameter list");
1256 PyErr_SyntaxLocation(st->st_filename,
1257 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 return 0;
1259 }
1260 }
1261
1262 if (!toplevel) {
1263 if (!symtable_visit_params_nested(st, args))
1264 return 0;
1265 }
1266
1267 return 1;
1268}
1269
1270static int
1271symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1272{
1273 int i;
1274 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001275 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 if (arg->kind == Tuple_kind &&
1277 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1278 return 0;
1279 }
1280
1281 return 1;
1282}
1283
1284static int
1285symtable_visit_arguments(struct symtable *st, arguments_ty a)
1286{
1287 /* skip default arguments inside function block
1288 XXX should ast be different?
1289 */
1290 if (a->args && !symtable_visit_params(st, a->args, 1))
1291 return 0;
1292 if (a->vararg) {
1293 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1294 return 0;
1295 st->st_cur->ste_varargs = 1;
1296 }
1297 if (a->kwarg) {
1298 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1299 return 0;
1300 st->st_cur->ste_varkeywords = 1;
1301 }
1302 if (a->args && !symtable_visit_params_nested(st, a->args))
1303 return 0;
1304 return 1;
1305}
1306
1307
1308static int
1309symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1310{
Georg Brandla48f3ab2008-03-30 06:40:17 +00001311 if (eh->v.ExceptHandler.type)
1312 VISIT(st, expr, eh->v.ExceptHandler.type);
1313 if (eh->v.ExceptHandler.name)
1314 VISIT(st, expr, eh->v.ExceptHandler.name);
1315 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 return 1;
1317}
1318
1319
1320static int
1321symtable_visit_alias(struct symtable *st, alias_ty a)
1322{
1323 /* Compute store_name, the name actually bound by the import
1324 operation. It is diferent than a->name when a->name is a
1325 dotted package name (e.g. spam.eggs)
1326 */
1327 PyObject *store_name;
1328 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Christian Heimes593daf52008-05-26 12:51:38 +00001329 const char *base = PyBytes_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330 char *dot = strchr(base, '.');
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001331 if (dot) {
Christian Heimes593daf52008-05-26 12:51:38 +00001332 store_name = PyBytes_FromStringAndSize(base, dot - base);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001333 if (!store_name)
1334 return 0;
1335 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336 else {
1337 store_name = name;
1338 Py_INCREF(store_name);
1339 }
Christian Heimes593daf52008-05-26 12:51:38 +00001340 if (strcmp(PyBytes_AS_STRING(name), "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1342 Py_DECREF(store_name);
1343 return r;
1344 }
1345 else {
1346 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001347 int lineno = st->st_cur->ste_lineno;
1348 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001349 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 }
1353 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001354 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 return 1;
1356 }
1357}
1358
1359
1360static int
1361symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1362{
1363 VISIT(st, expr, lc->target);
1364 VISIT(st, expr, lc->iter);
1365 VISIT_SEQ(st, expr, lc->ifs);
1366 return 1;
1367}
1368
1369
1370static int
1371symtable_visit_keyword(struct symtable *st, keyword_ty k)
1372{
1373 VISIT(st, expr, k->value);
1374 return 1;
1375}
1376
1377
1378static int
1379symtable_visit_slice(struct symtable *st, slice_ty s)
1380{
1381 switch (s->kind) {
1382 case Slice_kind:
1383 if (s->v.Slice.lower)
1384 VISIT(st, expr, s->v.Slice.lower)
1385 if (s->v.Slice.upper)
1386 VISIT(st, expr, s->v.Slice.upper)
1387 if (s->v.Slice.step)
1388 VISIT(st, expr, s->v.Slice.step)
1389 break;
1390 case ExtSlice_kind:
1391 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1392 break;
1393 case Index_kind:
1394 VISIT(st, expr, s->v.Index.value)
1395 break;
1396 case Ellipsis_kind:
1397 break;
1398 }
1399 return 1;
1400}
1401
1402static int
1403symtable_visit_genexp(struct symtable *st, expr_ty e)
1404{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405 comprehension_ty outermost = ((comprehension_ty)
1406 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1407 /* Outermost iterator is evaluated in current scope */
1408 VISIT(st, expr, outermost->iter);
1409 /* Create generator scope for the rest */
Neal Norwitz76059362006-08-19 04:52:03 +00001410 if (!GET_IDENTIFIER(genexpr) ||
1411 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 return 0;
1413 }
1414 st->st_cur->ste_generator = 1;
1415 /* Outermost iter is received as an argument */
1416 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001417 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 return 0;
1419 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001420 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1421 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1422 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1423 e->v.GeneratorExp.generators, 1, (void*)e);
1424 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Neal Norwitz76059362006-08-19 04:52:03 +00001425 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426}