blob: 4b8876dbec70fcc24715fcb503cbdd87f1bdfc36 [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},
Benjamin Peterson25f2d892008-08-17 02:23:43 +0000115 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
Benjamin Petersone0d4c7b2008-08-17 01:09:17 +0000116 {"nested", T_INT, OFF(ste_nested), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000117 {"type", T_INT, OFF(ste_type), READONLY},
118 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119 {NULL}
120};
121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122PyTypeObject PySTEntry_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000123 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126 0,
127 (destructor)ste_dealloc, /* tp_dealloc */
128 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000129 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130 0, /* tp_setattr */
131 0, /* tp_compare */
132 (reprfunc)ste_repr, /* tp_repr */
133 0, /* tp_as_number */
134 0, /* tp_as_sequence */
135 0, /* tp_as_mapping */
136 0, /* tp_hash */
137 0, /* tp_call */
138 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000139 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000140 0, /* tp_setattro */
141 0, /* tp_as_buffer */
142 Py_TPFLAGS_DEFAULT, /* tp_flags */
143 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000144 0, /* tp_traverse */
145 0, /* tp_clear */
146 0, /* tp_richcompare */
147 0, /* tp_weaklistoffset */
148 0, /* tp_iter */
149 0, /* tp_iternext */
150 0, /* tp_methods */
151 ste_memberlist, /* tp_members */
152 0, /* tp_getset */
153 0, /* tp_base */
154 0, /* tp_dict */
155 0, /* tp_descr_get */
156 0, /* tp_descr_set */
157 0, /* tp_dictoffset */
158 0, /* tp_init */
159 0, /* tp_alloc */
160 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000161};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162
163static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000164static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000166 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_exit_block(struct symtable *st, void *ast);
168static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
169static int symtable_visit_expr(struct symtable *st, expr_ty s);
170static int symtable_visit_genexp(struct symtable *st, expr_ty s);
171static int symtable_visit_arguments(struct symtable *st, arguments_ty);
172static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
173static int symtable_visit_alias(struct symtable *st, alias_ty);
174static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
175static int symtable_visit_keyword(struct symtable *st, keyword_ty);
176static int symtable_visit_slice(struct symtable *st, slice_ty);
177static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
178static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
179static int symtable_implicit_arg(struct symtable *st, int pos);
180
181
Nick Coghlan99b25332005-11-16 12:45:24 +0000182static identifier top = NULL, lambda = NULL, genexpr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183
184#define GET_IDENTIFIER(VAR) \
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000185 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186
187#define DUPLICATE_ARGUMENT \
188"duplicate argument '%s' in function definition"
189
190static struct symtable *
191symtable_new(void)
192{
193 struct symtable *st;
194
195 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
196 if (st == NULL)
197 return NULL;
198
199 st->st_filename = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000200 st->st_symbols = NULL;
201
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202 if ((st->st_stack = PyList_New(0)) == NULL)
203 goto fail;
204 if ((st->st_symbols = PyDict_New()) == NULL)
205 goto fail;
206 st->st_cur = NULL;
207 st->st_tmpname = 0;
208 st->st_private = NULL;
209 return st;
210 fail:
211 PySymtable_Free(st);
212 return NULL;
213}
214
215struct symtable *
216PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
217{
218 struct symtable *st = symtable_new();
219 asdl_seq *seq;
220 int i;
221
222 if (st == NULL)
223 return st;
224 st->st_filename = filename;
225 st->st_future = future;
Neal Norwitz76059362006-08-19 04:52:03 +0000226 if (!GET_IDENTIFIER(top) ||
227 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000228 PySymtable_Free(st);
229 return NULL;
230 }
231
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 st->st_top = st->st_cur;
233 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
234 /* Any other top-level initialization? */
235 switch (mod->kind) {
236 case Module_kind:
237 seq = mod->v.Module.body;
238 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000239 if (!symtable_visit_stmt(st,
240 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241 goto error;
242 break;
243 case Expression_kind:
244 if (!symtable_visit_expr(st, mod->v.Expression.body))
245 goto error;
246 break;
247 case Interactive_kind:
248 seq = mod->v.Interactive.body;
249 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000250 if (!symtable_visit_stmt(st,
251 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 goto error;
253 break;
254 case Suite_kind:
255 PyErr_SetString(PyExc_RuntimeError,
256 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000257 goto error;
258 }
259 if (!symtable_exit_block(st, (void *)mod)) {
260 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 return NULL;
262 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263 if (symtable_analyze(st))
264 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000265 PySymtable_Free(st);
266 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000268 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269 PySymtable_Free(st);
270 return NULL;
271}
272
273void
274PySymtable_Free(struct symtable *st)
275{
276 Py_XDECREF(st->st_symbols);
277 Py_XDECREF(st->st_stack);
278 PyMem_Free((void *)st);
279}
280
281PySTEntryObject *
282PySymtable_Lookup(struct symtable *st, void *key)
283{
284 PyObject *k, *v;
285
286 k = PyLong_FromVoidPtr(key);
287 if (k == NULL)
288 return NULL;
289 v = PyDict_GetItem(st->st_symbols, k);
290 if (v) {
291 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293 }
294 else {
295 PyErr_SetString(PyExc_KeyError,
296 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000298
299 Py_DECREF(k);
300 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301}
302
303int
304PyST_GetScope(PySTEntryObject *ste, PyObject *name)
305{
306 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
307 if (!v)
308 return 0;
309 assert(PyInt_Check(v));
310 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
311}
312
313
314/* Analyze raw symbol information to determine scope of each name.
315
316 The next several functions are helpers for PySymtable_Analyze(),
317 which determines whether a name is local, global, or free. In addition,
318 it determines which local variables are cell variables; they provide
319 bindings that are used for free variables in enclosed blocks.
320
321 There are also two kinds of free variables, implicit and explicit. An
322 explicit global is declared with the global statement. An implicit
323 global is a free variable for which the compiler has found no binding
324 in an enclosing function scope. The implicit global is either a global
325 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
326 to handle these names to implement slightly odd semantics. In such a
327 block, the name is treated as global until it is assigned to; then it
328 is treated as a local.
329
330 The symbol table requires two passes to determine the scope of each name.
331 The first pass collects raw facts from the AST: the name is a parameter
332 here, the name is used by not defined here, etc. The second pass analyzes
333 these facts during a pass over the PySTEntryObjects created during pass 1.
334
335 When a function is entered during the second pass, the parent passes
336 the set of all name bindings visible to its children. These bindings
337 are used to determine if the variable is free or an implicit global.
338 After doing the local analysis, it analyzes each of its child blocks
339 using an updated set of name bindings.
340
341 The children update the free variable set. If a local variable is free
342 in a child, the variable is marked as a cell. The current function must
343 provide runtime storage for the variable that may outlive the function's
344 frame. Cell variables are removed from the free set before the analyze
345 function returns to its parent.
346
347 The sets of bound and free variables are implemented as dictionaries
348 mapping strings to None.
349*/
350
351#define SET_SCOPE(DICT, NAME, I) { \
352 PyObject *o = PyInt_FromLong(I); \
353 if (!o) \
354 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000355 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
356 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000358 } \
359 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360}
361
362/* Decide on scope of name, given flags.
363
364 The dicts passed in as arguments are modified as necessary.
365 ste is passed so that flags can be updated.
366*/
367
368static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000369analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370 PyObject *bound, PyObject *local, PyObject *free,
371 PyObject *global)
372{
373 if (flags & DEF_GLOBAL) {
374 if (flags & DEF_PARAM) {
375 PyErr_Format(PyExc_SyntaxError,
376 "name '%s' is local and global",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000377 PyString_AS_STRING(name));
Benjamin Peterson08473322008-08-16 22:11:33 +0000378 PyErr_SyntaxLocation(ste->ste_table->st_filename,
379 ste->ste_lineno);
380
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 return 0;
382 }
383 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
384 if (PyDict_SetItem(global, name, Py_None) < 0)
385 return 0;
386 if (bound && PyDict_GetItem(bound, name)) {
387 if (PyDict_DelItem(bound, name) < 0)
388 return 0;
389 }
390 return 1;
391 }
392 if (flags & DEF_BOUND) {
393 SET_SCOPE(dict, name, LOCAL);
394 if (PyDict_SetItem(local, name, Py_None) < 0)
395 return 0;
396 if (PyDict_GetItem(global, name)) {
397 if (PyDict_DelItem(global, name) < 0)
398 return 0;
399 }
400 return 1;
401 }
402 /* If an enclosing block has a binding for this name, it
403 is a free variable rather than a global variable.
404 Note that having a non-NULL bound implies that the block
405 is nested.
406 */
407 if (bound && PyDict_GetItem(bound, name)) {
408 SET_SCOPE(dict, name, FREE);
409 ste->ste_free = 1;
410 if (PyDict_SetItem(free, name, Py_None) < 0)
411 return 0;
412 return 1;
413 }
414 /* If a parent has a global statement, then call it global
415 explicit? It could also be global implicit.
416 */
417 else if (global && PyDict_GetItem(global, name)) {
418 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
419 return 1;
420 }
421 else {
422 if (ste->ste_nested)
423 ste->ste_free = 1;
424 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
425 return 1;
426 }
427 return 0; /* Can't get here */
428}
429
430#undef SET_SCOPE
431
432/* If a name is defined in free and also in locals, then this block
433 provides the binding for the free variable. The name should be
434 marked CELL in this block and removed from the free list.
435
436 Note that the current block's free variables are included in free.
437 That's safe because no name can be free and local in the same scope.
438*/
439
440static int
441analyze_cells(PyObject *scope, PyObject *free)
442{
443 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000444 int success = 0;
445 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446
447 w = PyInt_FromLong(CELL);
448 if (!w)
449 return 0;
450 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000451 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000453 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000454 if (flags != LOCAL)
455 continue;
456 if (!PyDict_GetItem(free, name))
457 continue;
458 /* Replace LOCAL with CELL for this name, and remove
459 from free. It is safe to replace the value of name
460 in the dict, because it will not cause a resize.
461 */
462 if (PyDict_SetItem(scope, name, w) < 0)
463 goto error;
464 if (!PyDict_DelItem(free, name) < 0)
465 goto error;
466 }
467 success = 1;
468 error:
469 Py_DECREF(w);
470 return success;
471}
472
473/* Check for illegal statements in unoptimized namespaces */
474static int
475check_unoptimized(const PySTEntryObject* ste) {
476 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000477 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000479 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480 || !(ste->ste_free || ste->ste_child_free))
481 return 1;
482
Armin Rigo31441302005-10-21 12:57:31 +0000483 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484 "contains a nested function with free variables" :
485 "is a nested function");
486
487 switch (ste->ste_unoptimized) {
488 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
489 case OPT_EXEC: /* qualified exec is fine */
490 return 1;
491 case OPT_IMPORT_STAR:
492 PyOS_snprintf(buf, sizeof(buf),
493 "import * is not allowed in function '%.100s' "
494 "because it is %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000495 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496 break;
497 case OPT_BARE_EXEC:
498 PyOS_snprintf(buf, sizeof(buf),
499 "unqualified exec is not allowed in function "
500 "'%.100s' it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000501 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502 break;
503 default:
504 PyOS_snprintf(buf, sizeof(buf),
505 "function '%.100s' uses import * and bare exec, "
506 "which are illegal because it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000507 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508 break;
509 }
510
511 PyErr_SetString(PyExc_SyntaxError, buf);
512 PyErr_SyntaxLocation(ste->ste_table->st_filename,
513 ste->ste_opt_lineno);
514 return 0;
515}
516
517/* Enter the final scope information into the st_symbols dict.
518 *
519 * All arguments are dicts. Modifies symbols, others are read-only.
520*/
521static int
522update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000523 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524{
525 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000526 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527
528 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000529 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530 assert(PyInt_Check(v));
531 flags = PyInt_AS_LONG(v);
532 w = PyDict_GetItem(scope, name);
533 assert(w && PyInt_Check(w));
534 i = PyInt_AS_LONG(w);
535 flags |= (i << SCOPE_OFF);
536 u = PyInt_FromLong(flags);
Neal Norwitz18b6adf2006-07-23 07:50:36 +0000537 if (!u)
538 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539 if (PyDict_SetItem(symbols, name, u) < 0) {
540 Py_DECREF(u);
541 return 0;
542 }
543 Py_DECREF(u);
544 }
545
546 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
547 if (!free_value)
548 return 0;
549
550 /* add a free variable when it's only use is for creating a closure */
551 pos = 0;
552 while (PyDict_Next(free, &pos, &name, &v)) {
553 PyObject *o = PyDict_GetItem(symbols, name);
554
555 if (o) {
556 /* It could be a free variable in a method of
557 the class that has the same name as a local
558 or global in the class scope.
559 */
Anthony Baxter019aec62006-04-12 04:00:50 +0000560 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000562 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 o = PyInt_FromLong(i);
564 if (!o) {
565 Py_DECREF(free_value);
566 return 0;
567 }
568 if (PyDict_SetItem(symbols, name, o) < 0) {
569 Py_DECREF(o);
570 Py_DECREF(free_value);
571 return 0;
572 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000573 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 }
575 /* else it's not free, probably a cell */
576 continue;
577 }
578 if (!PyDict_GetItem(bound, name))
579 continue; /* it's a global */
580
581 if (PyDict_SetItem(symbols, name, free_value) < 0) {
582 Py_DECREF(free_value);
583 return 0;
584 }
585 }
586 Py_DECREF(free_value);
587 return 1;
588}
589
590/* Make final symbol table decisions for block of ste.
591 Arguments:
592 ste -- current symtable entry (input/output)
593 bound -- set of variables bound in enclosing scopes (input)
594 free -- set of free variables in enclosed scopes (output)
595 globals -- set of declared global variables in enclosing scopes (input)
596*/
597
598static int
599analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
600 PyObject *global)
601{
602 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
603 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000604 int i, success = 0;
605 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606
607 local = PyDict_New();
608 if (!local)
609 goto error;
610 scope = PyDict_New();
611 if (!scope)
612 goto error;
613 newglobal = PyDict_New();
614 if (!newglobal)
615 goto error;
616 newfree = PyDict_New();
617 if (!newfree)
618 goto error;
619 newbound = PyDict_New();
620 if (!newbound)
621 goto error;
622
623 if (ste->ste_type == ClassBlock) {
624 /* make a copy of globals before calling analyze_name(),
625 because global statements in the class have no effect
626 on nested functions.
627 */
628 if (PyDict_Update(newglobal, global) < 0)
629 goto error;
630 if (bound)
631 if (PyDict_Update(newbound, bound) < 0)
632 goto error;
633 }
634
635 assert(PySTEntry_Check(ste));
636 assert(PyDict_Check(ste->ste_symbols));
637 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000638 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 if (!analyze_name(ste, scope, name, flags, bound, local, free,
640 global))
641 goto error;
642 }
643
644 if (ste->ste_type != ClassBlock) {
645 if (ste->ste_type == FunctionBlock) {
646 if (PyDict_Update(newbound, local) < 0)
647 goto error;
648 }
649 if (bound) {
650 if (PyDict_Update(newbound, bound) < 0)
651 goto error;
652 }
653 if (PyDict_Update(newglobal, global) < 0)
654 goto error;
655 }
656
657 /* Recursively call analyze_block() on each child block */
658 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
659 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000660 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000662 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663 if (!analyze_block(entry, newbound, newfree, newglobal))
664 goto error;
665 if (entry->ste_free || entry->ste_child_free)
666 ste->ste_child_free = 1;
667 }
668
669 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
670 goto error;
671 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
672 ste->ste_type == ClassBlock))
673 goto error;
674 if (!check_unoptimized(ste))
675 goto error;
676
677 if (PyDict_Update(free, newfree) < 0)
678 goto error;
679 success = 1;
680 error:
681 Py_XDECREF(local);
682 Py_XDECREF(scope);
683 Py_XDECREF(newbound);
684 Py_XDECREF(newglobal);
685 Py_XDECREF(newfree);
686 if (!success)
687 assert(PyErr_Occurred());
688 return success;
689}
690
691static int
692symtable_analyze(struct symtable *st)
693{
694 PyObject *free, *global;
695 int r;
696
697 free = PyDict_New();
698 if (!free)
699 return 0;
700 global = PyDict_New();
701 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000702 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 return 0;
704 }
705 r = analyze_block(st->st_top, NULL, free, global);
706 Py_DECREF(free);
707 Py_DECREF(global);
708 return r;
709}
710
711
712static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000713symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714{
715 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000716 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
718 PyErr_SetString(PyExc_SyntaxError, msg);
719 PyErr_SyntaxLocation(st->st_filename,
720 st->st_cur->ste_lineno);
721 }
722 return 0;
723 }
724 return 1;
725}
726
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000727/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728 This reference is released when the block is exited, via the DECREF
729 in symtable_exit_block().
730*/
731
732static int
733symtable_exit_block(struct symtable *st, void *ast)
734{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000735 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000737 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 end = PyList_GET_SIZE(st->st_stack) - 1;
739 if (end >= 0) {
740 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
741 end);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000742 if (st->st_cur == NULL)
743 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 Py_INCREF(st->st_cur);
745 if (PySequence_DelItem(st->st_stack, end) < 0)
746 return 0;
747 }
748 return 1;
749}
750
751static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000752symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 void *ast, int lineno)
754{
755 PySTEntryObject *prev = NULL;
756
757 if (st->st_cur) {
758 prev = st->st_cur;
759 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 return 0;
761 }
762 Py_DECREF(st->st_cur);
763 }
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000764 st->st_cur = ste_new(st, name, block, ast, lineno);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000765 if (st->st_cur == NULL)
766 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 if (name == GET_IDENTIFIER(top))
768 st->st_global = st->st_cur->ste_symbols;
769 if (prev) {
770 if (PyList_Append(prev->ste_children,
771 (PyObject *)st->st_cur) < 0) {
772 return 0;
773 }
774 }
775 return 1;
776}
777
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000778static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779symtable_lookup(struct symtable *st, PyObject *name)
780{
781 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000782 PyObject *mangled = _Py_Mangle(st->st_private, name);
783 if (!mangled)
784 return 0;
785 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
786 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 if (!o)
788 return 0;
789 return PyInt_AsLong(o);
790}
791
792static int
793symtable_add_def(struct symtable *st, PyObject *name, int flag)
794{
795 PyObject *o;
796 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000797 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000798 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000800 if (!mangled)
801 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000803 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 val = PyInt_AS_LONG(o);
805 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000806 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000808 PyString_AsString(name));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 PyErr_SyntaxLocation(st->st_filename,
810 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000811 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 }
813 val |= flag;
814 } else
815 val = flag;
816 o = PyInt_FromLong(val);
817 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000818 goto error;
819 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000821 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 }
823 Py_DECREF(o);
824
825 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000826 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
827 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 } else if (flag & DEF_GLOBAL) {
829 /* XXX need to update DEF_GLOBAL for other flags too;
830 perhaps only DEF_FREE_GLOBAL */
831 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000832 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 val |= PyInt_AS_LONG(o);
834 }
835 o = PyInt_FromLong(val);
836 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000837 goto error;
838 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000840 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 }
842 Py_DECREF(o);
843 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000844 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000846
847error:
848 Py_DECREF(mangled);
849 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850}
851
852/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
853 They use the ASDL name to synthesize the name of the C type and the visit
854 function.
855
856 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
857 useful if the first node in the sequence requires special treatment.
858*/
859
860#define VISIT(ST, TYPE, V) \
861 if (!symtable_visit_ ## TYPE((ST), (V))) \
862 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000863
864#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
865 if (!symtable_visit_ ## TYPE((ST), (V))) { \
866 symtable_exit_block((ST), (S)); \
867 return 0; \
868 }
869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870#define VISIT_SEQ(ST, TYPE, SEQ) { \
871 int i; \
872 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
873 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000874 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 if (!symtable_visit_ ## TYPE((ST), elt)) \
876 return 0; \
877 } \
878}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000879
880#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
881 int i; \
882 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
883 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000884 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000885 if (!symtable_visit_ ## TYPE((ST), elt)) { \
886 symtable_exit_block((ST), (S)); \
887 return 0; \
888 } \
889 } \
890}
891
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
893 int i; \
894 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
895 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000896 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897 if (!symtable_visit_ ## TYPE((ST), elt)) \
898 return 0; \
899 } \
900}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000901
902#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
903 int i; \
904 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
905 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000906 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000907 if (!symtable_visit_ ## TYPE((ST), elt)) { \
908 symtable_exit_block((ST), (S)); \
909 return 0; \
910 } \
911 } \
912}
913
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000915symtable_new_tmpname(struct symtable *st)
916{
917 char tmpname[256];
918 identifier tmp;
919
920 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
921 ++st->st_cur->ste_tmpname);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000922 tmp = PyString_InternFromString(tmpname);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +0000923 if (!tmp)
924 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000925 if (!symtable_add_def(st, tmp, DEF_LOCAL))
926 return 0;
927 Py_DECREF(tmp);
928 return 1;
929}
930
931static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932symtable_visit_stmt(struct symtable *st, stmt_ty s)
933{
934 switch (s->kind) {
935 case FunctionDef_kind:
936 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
937 return 0;
938 if (s->v.FunctionDef.args->defaults)
939 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Christian Heimes5224d282008-02-23 15:01:05 +0000940 if (s->v.FunctionDef.decorator_list)
941 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 if (!symtable_enter_block(st, s->v.FunctionDef.name,
943 FunctionBlock, (void *)s, s->lineno))
944 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000945 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
946 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 if (!symtable_exit_block(st, s))
948 return 0;
949 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000950 case ClassDef_kind: {
951 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
953 return 0;
954 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Christian Heimes5224d282008-02-23 15:01:05 +0000955 if (s->v.ClassDef.decorator_list)
956 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
958 (void *)s, s->lineno))
959 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000960 tmp = st->st_private;
961 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000962 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000963 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964 if (!symtable_exit_block(st, s))
965 return 0;
966 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000967 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968 case Return_kind:
Georg Brandlddbaa662006-06-04 21:56:52 +0000969 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970 VISIT(st, expr, s->v.Return.value);
Georg Brandlddbaa662006-06-04 21:56:52 +0000971 st->st_cur->ste_returns_value = 1;
972 if (st->st_cur->ste_generator) {
973 PyErr_SetString(PyExc_SyntaxError,
974 RETURN_VAL_IN_GENERATOR);
975 PyErr_SyntaxLocation(st->st_filename,
976 s->lineno);
977 return 0;
978 }
979 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 break;
981 case Delete_kind:
982 VISIT_SEQ(st, expr, s->v.Delete.targets);
983 break;
984 case Assign_kind:
985 VISIT_SEQ(st, expr, s->v.Assign.targets);
986 VISIT(st, expr, s->v.Assign.value);
987 break;
988 case AugAssign_kind:
989 VISIT(st, expr, s->v.AugAssign.target);
990 VISIT(st, expr, s->v.AugAssign.value);
991 break;
992 case Print_kind:
993 if (s->v.Print.dest)
994 VISIT(st, expr, s->v.Print.dest);
995 VISIT_SEQ(st, expr, s->v.Print.values);
996 break;
997 case For_kind:
998 VISIT(st, expr, s->v.For.target);
999 VISIT(st, expr, s->v.For.iter);
1000 VISIT_SEQ(st, stmt, s->v.For.body);
1001 if (s->v.For.orelse)
1002 VISIT_SEQ(st, stmt, s->v.For.orelse);
1003 break;
1004 case While_kind:
1005 VISIT(st, expr, s->v.While.test);
1006 VISIT_SEQ(st, stmt, s->v.While.body);
1007 if (s->v.While.orelse)
1008 VISIT_SEQ(st, stmt, s->v.While.orelse);
1009 break;
1010 case If_kind:
1011 /* XXX if 0: and lookup_yield() hacks */
1012 VISIT(st, expr, s->v.If.test);
1013 VISIT_SEQ(st, stmt, s->v.If.body);
1014 if (s->v.If.orelse)
1015 VISIT_SEQ(st, stmt, s->v.If.orelse);
1016 break;
1017 case Raise_kind:
1018 if (s->v.Raise.type) {
1019 VISIT(st, expr, s->v.Raise.type);
1020 if (s->v.Raise.inst) {
1021 VISIT(st, expr, s->v.Raise.inst);
1022 if (s->v.Raise.tback)
1023 VISIT(st, expr, s->v.Raise.tback);
1024 }
1025 }
1026 break;
1027 case TryExcept_kind:
1028 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1029 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1030 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1031 break;
1032 case TryFinally_kind:
1033 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1034 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1035 break;
1036 case Assert_kind:
1037 VISIT(st, expr, s->v.Assert.test);
1038 if (s->v.Assert.msg)
1039 VISIT(st, expr, s->v.Assert.msg);
1040 break;
1041 case Import_kind:
1042 VISIT_SEQ(st, alias, s->v.Import.names);
1043 /* XXX Don't have the lineno available inside
1044 visit_alias */
1045 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1046 st->st_cur->ste_opt_lineno = s->lineno;
1047 break;
1048 case ImportFrom_kind:
1049 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1050 /* XXX Don't have the lineno available inside
1051 visit_alias */
1052 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1053 st->st_cur->ste_opt_lineno = s->lineno;
1054 break;
1055 case Exec_kind:
1056 VISIT(st, expr, s->v.Exec.body);
1057 if (!st->st_cur->ste_opt_lineno)
1058 st->st_cur->ste_opt_lineno = s->lineno;
1059 if (s->v.Exec.globals) {
1060 st->st_cur->ste_unoptimized |= OPT_EXEC;
1061 VISIT(st, expr, s->v.Exec.globals);
1062 if (s->v.Exec.locals)
1063 VISIT(st, expr, s->v.Exec.locals);
1064 } else {
1065 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1066 }
1067 break;
1068 case Global_kind: {
1069 int i;
1070 asdl_seq *seq = s->v.Global.names;
1071 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001072 identifier name = (identifier)asdl_seq_GET(seq, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001073 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001074 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 if (cur < 0)
1076 return 0;
1077 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001078 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 if (cur & DEF_LOCAL)
1080 PyOS_snprintf(buf, sizeof(buf),
1081 GLOBAL_AFTER_ASSIGN,
1082 c_name);
1083 else
1084 PyOS_snprintf(buf, sizeof(buf),
1085 GLOBAL_AFTER_USE,
1086 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001087 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 return 0;
1089 }
1090 if (!symtable_add_def(st, name, DEF_GLOBAL))
1091 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 break;
1094 }
1095 case Expr_kind:
1096 VISIT(st, expr, s->v.Expr.value);
1097 break;
1098 case Pass_kind:
1099 case Break_kind:
1100 case Continue_kind:
1101 /* nothing to do here */
1102 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001103 case With_kind:
1104 if (!symtable_new_tmpname(st))
1105 return 0;
1106 VISIT(st, expr, s->v.With.context_expr);
1107 if (s->v.With.optional_vars) {
1108 if (!symtable_new_tmpname(st))
1109 return 0;
1110 VISIT(st, expr, s->v.With.optional_vars);
1111 }
1112 VISIT_SEQ(st, stmt, s->v.With.body);
1113 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 }
1115 return 1;
1116}
1117
1118static int
1119symtable_visit_expr(struct symtable *st, expr_ty e)
1120{
1121 switch (e->kind) {
1122 case BoolOp_kind:
1123 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1124 break;
1125 case BinOp_kind:
1126 VISIT(st, expr, e->v.BinOp.left);
1127 VISIT(st, expr, e->v.BinOp.right);
1128 break;
1129 case UnaryOp_kind:
1130 VISIT(st, expr, e->v.UnaryOp.operand);
1131 break;
1132 case Lambda_kind: {
Neal Norwitz76059362006-08-19 04:52:03 +00001133 if (!GET_IDENTIFIER(lambda) ||
1134 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 return 0;
1136 if (e->v.Lambda.args->defaults)
1137 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1138 /* XXX how to get line numbers for expressions */
Neal Norwitz76059362006-08-19 04:52:03 +00001139 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 FunctionBlock, (void *)e, 0))
1141 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001142 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1143 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 if (!symtable_exit_block(st, (void *)e))
1145 return 0;
1146 break;
1147 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001148 case IfExp_kind:
1149 VISIT(st, expr, e->v.IfExp.test);
1150 VISIT(st, expr, e->v.IfExp.body);
1151 VISIT(st, expr, e->v.IfExp.orelse);
1152 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 case Dict_kind:
1154 VISIT_SEQ(st, expr, e->v.Dict.keys);
1155 VISIT_SEQ(st, expr, e->v.Dict.values);
1156 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001157 case ListComp_kind:
1158 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 return 0;
1160 VISIT(st, expr, e->v.ListComp.elt);
1161 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1162 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001163 case GeneratorExp_kind:
1164 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 case Yield_kind:
1168 if (e->v.Yield.value)
1169 VISIT(st, expr, e->v.Yield.value);
1170 st->st_cur->ste_generator = 1;
Georg Brandlddbaa662006-06-04 21:56:52 +00001171 if (st->st_cur->ste_returns_value) {
1172 PyErr_SetString(PyExc_SyntaxError,
1173 RETURN_VAL_IN_GENERATOR);
1174 PyErr_SyntaxLocation(st->st_filename,
1175 e->lineno);
1176 return 0;
1177 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 break;
1179 case Compare_kind:
1180 VISIT(st, expr, e->v.Compare.left);
1181 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1182 break;
1183 case Call_kind:
1184 VISIT(st, expr, e->v.Call.func);
1185 VISIT_SEQ(st, expr, e->v.Call.args);
1186 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1187 if (e->v.Call.starargs)
1188 VISIT(st, expr, e->v.Call.starargs);
1189 if (e->v.Call.kwargs)
1190 VISIT(st, expr, e->v.Call.kwargs);
1191 break;
1192 case Repr_kind:
1193 VISIT(st, expr, e->v.Repr.value);
1194 break;
1195 case Num_kind:
1196 case Str_kind:
1197 /* Nothing to do here. */
1198 break;
1199 /* The following exprs can be assignment targets. */
1200 case Attribute_kind:
1201 VISIT(st, expr, e->v.Attribute.value);
1202 break;
1203 case Subscript_kind:
1204 VISIT(st, expr, e->v.Subscript.value);
1205 VISIT(st, slice, e->v.Subscript.slice);
1206 break;
1207 case Name_kind:
1208 if (!symtable_add_def(st, e->v.Name.id,
1209 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1210 return 0;
1211 break;
1212 /* child nodes of List and Tuple will have expr_context set */
1213 case List_kind:
1214 VISIT_SEQ(st, expr, e->v.List.elts);
1215 break;
1216 case Tuple_kind:
1217 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1218 break;
1219 }
1220 return 1;
1221}
1222
1223static int
1224symtable_implicit_arg(struct symtable *st, int pos)
1225{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001226 PyObject *id = PyString_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 if (id == NULL)
1228 return 0;
1229 if (!symtable_add_def(st, id, DEF_PARAM)) {
1230 Py_DECREF(id);
1231 return 0;
1232 }
1233 Py_DECREF(id);
1234 return 1;
1235}
1236
1237static int
1238symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1239{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001240 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241
1242 /* go through all the toplevel arguments first */
1243 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001244 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 if (arg->kind == Name_kind) {
1246 assert(arg->v.Name.ctx == Param ||
1247 (arg->v.Name.ctx == Store && !toplevel));
1248 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1249 return 0;
1250 }
1251 else if (arg->kind == Tuple_kind) {
1252 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 if (toplevel) {
1254 if (!symtable_implicit_arg(st, i))
1255 return 0;
1256 }
1257 }
1258 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001259 PyErr_SetString(PyExc_SyntaxError,
1260 "invalid expression in parameter list");
1261 PyErr_SyntaxLocation(st->st_filename,
1262 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 return 0;
1264 }
1265 }
1266
1267 if (!toplevel) {
1268 if (!symtable_visit_params_nested(st, args))
1269 return 0;
1270 }
1271
1272 return 1;
1273}
1274
1275static int
1276symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1277{
1278 int i;
1279 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001280 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 if (arg->kind == Tuple_kind &&
1282 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1283 return 0;
1284 }
1285
1286 return 1;
1287}
1288
1289static int
1290symtable_visit_arguments(struct symtable *st, arguments_ty a)
1291{
1292 /* skip default arguments inside function block
1293 XXX should ast be different?
1294 */
1295 if (a->args && !symtable_visit_params(st, a->args, 1))
1296 return 0;
1297 if (a->vararg) {
1298 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1299 return 0;
1300 st->st_cur->ste_varargs = 1;
1301 }
1302 if (a->kwarg) {
1303 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1304 return 0;
1305 st->st_cur->ste_varkeywords = 1;
1306 }
1307 if (a->args && !symtable_visit_params_nested(st, a->args))
1308 return 0;
1309 return 1;
1310}
1311
1312
1313static int
1314symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1315{
Georg Brandla48f3ab2008-03-30 06:40:17 +00001316 if (eh->v.ExceptHandler.type)
1317 VISIT(st, expr, eh->v.ExceptHandler.type);
1318 if (eh->v.ExceptHandler.name)
1319 VISIT(st, expr, eh->v.ExceptHandler.name);
1320 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 return 1;
1322}
1323
1324
1325static int
1326symtable_visit_alias(struct symtable *st, alias_ty a)
1327{
1328 /* Compute store_name, the name actually bound by the import
1329 operation. It is diferent than a->name when a->name is a
1330 dotted package name (e.g. spam.eggs)
1331 */
1332 PyObject *store_name;
1333 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001334 const char *base = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 char *dot = strchr(base, '.');
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001336 if (dot) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001337 store_name = PyString_FromStringAndSize(base, dot - base);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001338 if (!store_name)
1339 return 0;
1340 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 else {
1342 store_name = name;
1343 Py_INCREF(store_name);
1344 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001345 if (strcmp(PyString_AS_STRING(name), "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1347 Py_DECREF(store_name);
1348 return r;
1349 }
1350 else {
1351 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001352 int lineno = st->st_cur->ste_lineno;
1353 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001354 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001356 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 }
1358 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001359 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 return 1;
1361 }
1362}
1363
1364
1365static int
1366symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1367{
1368 VISIT(st, expr, lc->target);
1369 VISIT(st, expr, lc->iter);
1370 VISIT_SEQ(st, expr, lc->ifs);
1371 return 1;
1372}
1373
1374
1375static int
1376symtable_visit_keyword(struct symtable *st, keyword_ty k)
1377{
1378 VISIT(st, expr, k->value);
1379 return 1;
1380}
1381
1382
1383static int
1384symtable_visit_slice(struct symtable *st, slice_ty s)
1385{
1386 switch (s->kind) {
1387 case Slice_kind:
1388 if (s->v.Slice.lower)
1389 VISIT(st, expr, s->v.Slice.lower)
1390 if (s->v.Slice.upper)
1391 VISIT(st, expr, s->v.Slice.upper)
1392 if (s->v.Slice.step)
1393 VISIT(st, expr, s->v.Slice.step)
1394 break;
1395 case ExtSlice_kind:
1396 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1397 break;
1398 case Index_kind:
1399 VISIT(st, expr, s->v.Index.value)
1400 break;
1401 case Ellipsis_kind:
1402 break;
1403 }
1404 return 1;
1405}
1406
1407static int
1408symtable_visit_genexp(struct symtable *st, expr_ty e)
1409{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 comprehension_ty outermost = ((comprehension_ty)
1411 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1412 /* Outermost iterator is evaluated in current scope */
1413 VISIT(st, expr, outermost->iter);
1414 /* Create generator scope for the rest */
Neal Norwitz76059362006-08-19 04:52:03 +00001415 if (!GET_IDENTIFIER(genexpr) ||
1416 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 return 0;
1418 }
1419 st->st_cur->ste_generator = 1;
1420 /* Outermost iter is received as an argument */
1421 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001422 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 return 0;
1424 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001425 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1426 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1427 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1428 e->v.GeneratorExp.generators, 1, (void*)e);
1429 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Neal Norwitz76059362006-08-19 04:52:03 +00001430 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431}