blob: 46909fcef92a7c3c0c57bbeb1f43edfb8669a54a [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 Petersone0d4c7b2008-08-17 01:09:17 +0000115 {"nested", T_INT, OFF(ste_nested), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000116 {"type", T_INT, OFF(ste_type), READONLY},
117 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000118 {NULL}
119};
120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121PyTypeObject PySTEntry_Type = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000122 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000123 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000125 0,
126 (destructor)ste_dealloc, /* tp_dealloc */
127 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000128 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000129 0, /* tp_setattr */
130 0, /* tp_compare */
131 (reprfunc)ste_repr, /* tp_repr */
132 0, /* tp_as_number */
133 0, /* tp_as_sequence */
134 0, /* tp_as_mapping */
135 0, /* tp_hash */
136 0, /* tp_call */
137 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000138 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000139 0, /* tp_setattro */
140 0, /* tp_as_buffer */
141 Py_TPFLAGS_DEFAULT, /* tp_flags */
142 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000143 0, /* tp_traverse */
144 0, /* tp_clear */
145 0, /* tp_richcompare */
146 0, /* tp_weaklistoffset */
147 0, /* tp_iter */
148 0, /* tp_iternext */
149 0, /* tp_methods */
150 ste_memberlist, /* tp_members */
151 0, /* tp_getset */
152 0, /* tp_base */
153 0, /* tp_dict */
154 0, /* tp_descr_get */
155 0, /* tp_descr_set */
156 0, /* tp_dictoffset */
157 0, /* tp_init */
158 0, /* tp_alloc */
159 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000160};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161
162static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000163static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000165 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166static int symtable_exit_block(struct symtable *st, void *ast);
167static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
168static int symtable_visit_expr(struct symtable *st, expr_ty s);
169static int symtable_visit_genexp(struct symtable *st, expr_ty s);
170static int symtable_visit_arguments(struct symtable *st, arguments_ty);
171static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
172static int symtable_visit_alias(struct symtable *st, alias_ty);
173static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
174static int symtable_visit_keyword(struct symtable *st, keyword_ty);
175static int symtable_visit_slice(struct symtable *st, slice_ty);
176static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
177static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
178static int symtable_implicit_arg(struct symtable *st, int pos);
179
180
Nick Coghlan99b25332005-11-16 12:45:24 +0000181static identifier top = NULL, lambda = NULL, genexpr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182
183#define GET_IDENTIFIER(VAR) \
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000184 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185
186#define DUPLICATE_ARGUMENT \
187"duplicate argument '%s' in function definition"
188
189static struct symtable *
190symtable_new(void)
191{
192 struct symtable *st;
193
194 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
195 if (st == NULL)
196 return NULL;
197
198 st->st_filename = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000199 st->st_symbols = NULL;
200
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201 if ((st->st_stack = PyList_New(0)) == NULL)
202 goto fail;
203 if ((st->st_symbols = PyDict_New()) == NULL)
204 goto fail;
205 st->st_cur = NULL;
206 st->st_tmpname = 0;
207 st->st_private = NULL;
208 return st;
209 fail:
210 PySymtable_Free(st);
211 return NULL;
212}
213
214struct symtable *
215PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
216{
217 struct symtable *st = symtable_new();
218 asdl_seq *seq;
219 int i;
220
221 if (st == NULL)
222 return st;
223 st->st_filename = filename;
224 st->st_future = future;
Neal Norwitz76059362006-08-19 04:52:03 +0000225 if (!GET_IDENTIFIER(top) ||
226 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Neal Norwitzd12bd012006-07-21 07:59:47 +0000227 PySymtable_Free(st);
228 return NULL;
229 }
230
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 st->st_top = st->st_cur;
232 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
233 /* Any other top-level initialization? */
234 switch (mod->kind) {
235 case Module_kind:
236 seq = mod->v.Module.body;
237 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000238 if (!symtable_visit_stmt(st,
239 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 goto error;
241 break;
242 case Expression_kind:
243 if (!symtable_visit_expr(st, mod->v.Expression.body))
244 goto error;
245 break;
246 case Interactive_kind:
247 seq = mod->v.Interactive.body;
248 for (i = 0; i < asdl_seq_LEN(seq); i++)
Anthony Baxter019aec62006-04-12 04:00:50 +0000249 if (!symtable_visit_stmt(st,
250 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000251 goto error;
252 break;
253 case Suite_kind:
254 PyErr_SetString(PyExc_RuntimeError,
255 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000256 goto error;
257 }
258 if (!symtable_exit_block(st, (void *)mod)) {
259 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260 return NULL;
261 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262 if (symtable_analyze(st))
263 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000264 PySymtable_Free(st);
265 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000267 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 PySymtable_Free(st);
269 return NULL;
270}
271
272void
273PySymtable_Free(struct symtable *st)
274{
275 Py_XDECREF(st->st_symbols);
276 Py_XDECREF(st->st_stack);
277 PyMem_Free((void *)st);
278}
279
280PySTEntryObject *
281PySymtable_Lookup(struct symtable *st, void *key)
282{
283 PyObject *k, *v;
284
285 k = PyLong_FromVoidPtr(key);
286 if (k == NULL)
287 return NULL;
288 v = PyDict_GetItem(st->st_symbols, k);
289 if (v) {
290 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292 }
293 else {
294 PyErr_SetString(PyExc_KeyError,
295 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000296 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000297
298 Py_DECREF(k);
299 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300}
301
302int
303PyST_GetScope(PySTEntryObject *ste, PyObject *name)
304{
305 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
306 if (!v)
307 return 0;
308 assert(PyInt_Check(v));
309 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
310}
311
312
313/* Analyze raw symbol information to determine scope of each name.
314
315 The next several functions are helpers for PySymtable_Analyze(),
316 which determines whether a name is local, global, or free. In addition,
317 it determines which local variables are cell variables; they provide
318 bindings that are used for free variables in enclosed blocks.
319
320 There are also two kinds of free variables, implicit and explicit. An
321 explicit global is declared with the global statement. An implicit
322 global is a free variable for which the compiler has found no binding
323 in an enclosing function scope. The implicit global is either a global
324 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
325 to handle these names to implement slightly odd semantics. In such a
326 block, the name is treated as global until it is assigned to; then it
327 is treated as a local.
328
329 The symbol table requires two passes to determine the scope of each name.
330 The first pass collects raw facts from the AST: the name is a parameter
331 here, the name is used by not defined here, etc. The second pass analyzes
332 these facts during a pass over the PySTEntryObjects created during pass 1.
333
334 When a function is entered during the second pass, the parent passes
335 the set of all name bindings visible to its children. These bindings
336 are used to determine if the variable is free or an implicit global.
337 After doing the local analysis, it analyzes each of its child blocks
338 using an updated set of name bindings.
339
340 The children update the free variable set. If a local variable is free
341 in a child, the variable is marked as a cell. The current function must
342 provide runtime storage for the variable that may outlive the function's
343 frame. Cell variables are removed from the free set before the analyze
344 function returns to its parent.
345
346 The sets of bound and free variables are implemented as dictionaries
347 mapping strings to None.
348*/
349
350#define SET_SCOPE(DICT, NAME, I) { \
351 PyObject *o = PyInt_FromLong(I); \
352 if (!o) \
353 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000354 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
355 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000357 } \
358 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359}
360
361/* Decide on scope of name, given flags.
362
363 The dicts passed in as arguments are modified as necessary.
364 ste is passed so that flags can be updated.
365*/
366
367static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000368analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369 PyObject *bound, PyObject *local, PyObject *free,
370 PyObject *global)
371{
372 if (flags & DEF_GLOBAL) {
373 if (flags & DEF_PARAM) {
374 PyErr_Format(PyExc_SyntaxError,
375 "name '%s' is local and global",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000376 PyString_AS_STRING(name));
Benjamin Peterson08473322008-08-16 22:11:33 +0000377 PyErr_SyntaxLocation(ste->ste_table->st_filename,
378 ste->ste_lineno);
379
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380 return 0;
381 }
382 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
383 if (PyDict_SetItem(global, name, Py_None) < 0)
384 return 0;
385 if (bound && PyDict_GetItem(bound, name)) {
386 if (PyDict_DelItem(bound, name) < 0)
387 return 0;
388 }
389 return 1;
390 }
391 if (flags & DEF_BOUND) {
392 SET_SCOPE(dict, name, LOCAL);
393 if (PyDict_SetItem(local, name, Py_None) < 0)
394 return 0;
395 if (PyDict_GetItem(global, name)) {
396 if (PyDict_DelItem(global, name) < 0)
397 return 0;
398 }
399 return 1;
400 }
401 /* If an enclosing block has a binding for this name, it
402 is a free variable rather than a global variable.
403 Note that having a non-NULL bound implies that the block
404 is nested.
405 */
406 if (bound && PyDict_GetItem(bound, name)) {
407 SET_SCOPE(dict, name, FREE);
408 ste->ste_free = 1;
409 if (PyDict_SetItem(free, name, Py_None) < 0)
410 return 0;
411 return 1;
412 }
413 /* If a parent has a global statement, then call it global
414 explicit? It could also be global implicit.
415 */
416 else if (global && PyDict_GetItem(global, name)) {
417 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
418 return 1;
419 }
420 else {
421 if (ste->ste_nested)
422 ste->ste_free = 1;
423 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
424 return 1;
425 }
426 return 0; /* Can't get here */
427}
428
429#undef SET_SCOPE
430
431/* If a name is defined in free and also in locals, then this block
432 provides the binding for the free variable. The name should be
433 marked CELL in this block and removed from the free list.
434
435 Note that the current block's free variables are included in free.
436 That's safe because no name can be free and local in the same scope.
437*/
438
439static int
440analyze_cells(PyObject *scope, PyObject *free)
441{
442 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000443 int success = 0;
444 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445
446 w = PyInt_FromLong(CELL);
447 if (!w)
448 return 0;
449 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000450 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000452 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453 if (flags != LOCAL)
454 continue;
455 if (!PyDict_GetItem(free, name))
456 continue;
457 /* Replace LOCAL with CELL for this name, and remove
458 from free. It is safe to replace the value of name
459 in the dict, because it will not cause a resize.
460 */
461 if (PyDict_SetItem(scope, name, w) < 0)
462 goto error;
463 if (!PyDict_DelItem(free, name) < 0)
464 goto error;
465 }
466 success = 1;
467 error:
468 Py_DECREF(w);
469 return success;
470}
471
472/* Check for illegal statements in unoptimized namespaces */
473static int
474check_unoptimized(const PySTEntryObject* ste) {
475 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000476 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000478 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479 || !(ste->ste_free || ste->ste_child_free))
480 return 1;
481
Armin Rigo31441302005-10-21 12:57:31 +0000482 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483 "contains a nested function with free variables" :
484 "is a nested function");
485
486 switch (ste->ste_unoptimized) {
487 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
488 case OPT_EXEC: /* qualified exec is fine */
489 return 1;
490 case OPT_IMPORT_STAR:
491 PyOS_snprintf(buf, sizeof(buf),
492 "import * is not allowed in function '%.100s' "
493 "because it is %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000494 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495 break;
496 case OPT_BARE_EXEC:
497 PyOS_snprintf(buf, sizeof(buf),
498 "unqualified exec is not allowed in function "
499 "'%.100s' it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000500 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501 break;
502 default:
503 PyOS_snprintf(buf, sizeof(buf),
504 "function '%.100s' uses import * and bare exec, "
505 "which are illegal because it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000506 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507 break;
508 }
509
510 PyErr_SetString(PyExc_SyntaxError, buf);
511 PyErr_SyntaxLocation(ste->ste_table->st_filename,
512 ste->ste_opt_lineno);
513 return 0;
514}
515
516/* Enter the final scope information into the st_symbols dict.
517 *
518 * All arguments are dicts. Modifies symbols, others are read-only.
519*/
520static int
521update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000522 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523{
524 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000525 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526
527 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000528 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 assert(PyInt_Check(v));
530 flags = PyInt_AS_LONG(v);
531 w = PyDict_GetItem(scope, name);
532 assert(w && PyInt_Check(w));
533 i = PyInt_AS_LONG(w);
534 flags |= (i << SCOPE_OFF);
535 u = PyInt_FromLong(flags);
Neal Norwitz18b6adf2006-07-23 07:50:36 +0000536 if (!u)
537 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 if (PyDict_SetItem(symbols, name, u) < 0) {
539 Py_DECREF(u);
540 return 0;
541 }
542 Py_DECREF(u);
543 }
544
545 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
546 if (!free_value)
547 return 0;
548
549 /* add a free variable when it's only use is for creating a closure */
550 pos = 0;
551 while (PyDict_Next(free, &pos, &name, &v)) {
552 PyObject *o = PyDict_GetItem(symbols, name);
553
554 if (o) {
555 /* It could be a free variable in a method of
556 the class that has the same name as a local
557 or global in the class scope.
558 */
Anthony Baxter019aec62006-04-12 04:00:50 +0000559 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000561 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562 o = PyInt_FromLong(i);
563 if (!o) {
564 Py_DECREF(free_value);
565 return 0;
566 }
567 if (PyDict_SetItem(symbols, name, o) < 0) {
568 Py_DECREF(o);
569 Py_DECREF(free_value);
570 return 0;
571 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000572 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 }
574 /* else it's not free, probably a cell */
575 continue;
576 }
577 if (!PyDict_GetItem(bound, name))
578 continue; /* it's a global */
579
580 if (PyDict_SetItem(symbols, name, free_value) < 0) {
581 Py_DECREF(free_value);
582 return 0;
583 }
584 }
585 Py_DECREF(free_value);
586 return 1;
587}
588
589/* Make final symbol table decisions for block of ste.
590 Arguments:
591 ste -- current symtable entry (input/output)
592 bound -- set of variables bound in enclosing scopes (input)
593 free -- set of free variables in enclosed scopes (output)
594 globals -- set of declared global variables in enclosing scopes (input)
595*/
596
597static int
598analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
599 PyObject *global)
600{
601 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
602 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000603 int i, success = 0;
604 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605
606 local = PyDict_New();
607 if (!local)
608 goto error;
609 scope = PyDict_New();
610 if (!scope)
611 goto error;
612 newglobal = PyDict_New();
613 if (!newglobal)
614 goto error;
615 newfree = PyDict_New();
616 if (!newfree)
617 goto error;
618 newbound = PyDict_New();
619 if (!newbound)
620 goto error;
621
622 if (ste->ste_type == ClassBlock) {
623 /* make a copy of globals before calling analyze_name(),
624 because global statements in the class have no effect
625 on nested functions.
626 */
627 if (PyDict_Update(newglobal, global) < 0)
628 goto error;
629 if (bound)
630 if (PyDict_Update(newbound, bound) < 0)
631 goto error;
632 }
633
634 assert(PySTEntry_Check(ste));
635 assert(PyDict_Check(ste->ste_symbols));
636 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000637 long flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 if (!analyze_name(ste, scope, name, flags, bound, local, free,
639 global))
640 goto error;
641 }
642
643 if (ste->ste_type != ClassBlock) {
644 if (ste->ste_type == FunctionBlock) {
645 if (PyDict_Update(newbound, local) < 0)
646 goto error;
647 }
648 if (bound) {
649 if (PyDict_Update(newbound, bound) < 0)
650 goto error;
651 }
652 if (PyDict_Update(newglobal, global) < 0)
653 goto error;
654 }
655
656 /* Recursively call analyze_block() on each child block */
657 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
658 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000659 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000661 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662 if (!analyze_block(entry, newbound, newfree, newglobal))
663 goto error;
664 if (entry->ste_free || entry->ste_child_free)
665 ste->ste_child_free = 1;
666 }
667
668 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
669 goto error;
670 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
671 ste->ste_type == ClassBlock))
672 goto error;
673 if (!check_unoptimized(ste))
674 goto error;
675
676 if (PyDict_Update(free, newfree) < 0)
677 goto error;
678 success = 1;
679 error:
680 Py_XDECREF(local);
681 Py_XDECREF(scope);
682 Py_XDECREF(newbound);
683 Py_XDECREF(newglobal);
684 Py_XDECREF(newfree);
685 if (!success)
686 assert(PyErr_Occurred());
687 return success;
688}
689
690static int
691symtable_analyze(struct symtable *st)
692{
693 PyObject *free, *global;
694 int r;
695
696 free = PyDict_New();
697 if (!free)
698 return 0;
699 global = PyDict_New();
700 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000701 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 return 0;
703 }
704 r = analyze_block(st->st_top, NULL, free, global);
705 Py_DECREF(free);
706 Py_DECREF(global);
707 return r;
708}
709
710
711static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000712symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713{
714 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000715 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
717 PyErr_SetString(PyExc_SyntaxError, msg);
718 PyErr_SyntaxLocation(st->st_filename,
719 st->st_cur->ste_lineno);
720 }
721 return 0;
722 }
723 return 1;
724}
725
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000726/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 This reference is released when the block is exited, via the DECREF
728 in symtable_exit_block().
729*/
730
731static int
732symtable_exit_block(struct symtable *st, void *ast)
733{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000734 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000736 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 end = PyList_GET_SIZE(st->st_stack) - 1;
738 if (end >= 0) {
739 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
740 end);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000741 if (st->st_cur == NULL)
742 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743 Py_INCREF(st->st_cur);
744 if (PySequence_DelItem(st->st_stack, end) < 0)
745 return 0;
746 }
747 return 1;
748}
749
750static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000751symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 void *ast, int lineno)
753{
754 PySTEntryObject *prev = NULL;
755
756 if (st->st_cur) {
757 prev = st->st_cur;
758 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759 return 0;
760 }
761 Py_DECREF(st->st_cur);
762 }
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000763 st->st_cur = ste_new(st, name, block, ast, lineno);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000764 if (st->st_cur == NULL)
765 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 if (name == GET_IDENTIFIER(top))
767 st->st_global = st->st_cur->ste_symbols;
768 if (prev) {
769 if (PyList_Append(prev->ste_children,
770 (PyObject *)st->st_cur) < 0) {
771 return 0;
772 }
773 }
774 return 1;
775}
776
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000777static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778symtable_lookup(struct symtable *st, PyObject *name)
779{
780 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000781 PyObject *mangled = _Py_Mangle(st->st_private, name);
782 if (!mangled)
783 return 0;
784 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
785 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 if (!o)
787 return 0;
788 return PyInt_AsLong(o);
789}
790
791static int
792symtable_add_def(struct symtable *st, PyObject *name, int flag)
793{
794 PyObject *o;
795 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000796 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000797 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000799 if (!mangled)
800 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000802 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 val = PyInt_AS_LONG(o);
804 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000805 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000807 PyString_AsString(name));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 PyErr_SyntaxLocation(st->st_filename,
809 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000810 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 }
812 val |= flag;
813 } else
814 val = flag;
815 o = PyInt_FromLong(val);
816 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000817 goto error;
818 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000820 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 }
822 Py_DECREF(o);
823
824 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000825 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
826 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 } else if (flag & DEF_GLOBAL) {
828 /* XXX need to update DEF_GLOBAL for other flags too;
829 perhaps only DEF_FREE_GLOBAL */
830 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000831 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 val |= PyInt_AS_LONG(o);
833 }
834 o = PyInt_FromLong(val);
835 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000836 goto error;
837 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000839 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 }
841 Py_DECREF(o);
842 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000843 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000845
846error:
847 Py_DECREF(mangled);
848 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849}
850
851/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
852 They use the ASDL name to synthesize the name of the C type and the visit
853 function.
854
855 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
856 useful if the first node in the sequence requires special treatment.
857*/
858
859#define VISIT(ST, TYPE, V) \
860 if (!symtable_visit_ ## TYPE((ST), (V))) \
861 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000862
863#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
864 if (!symtable_visit_ ## TYPE((ST), (V))) { \
865 symtable_exit_block((ST), (S)); \
866 return 0; \
867 }
868
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869#define VISIT_SEQ(ST, TYPE, SEQ) { \
870 int i; \
871 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
872 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000873 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874 if (!symtable_visit_ ## TYPE((ST), elt)) \
875 return 0; \
876 } \
877}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000878
879#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
880 int i; \
881 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
882 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000883 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000884 if (!symtable_visit_ ## TYPE((ST), elt)) { \
885 symtable_exit_block((ST), (S)); \
886 return 0; \
887 } \
888 } \
889}
890
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
892 int i; \
893 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
894 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000895 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896 if (!symtable_visit_ ## TYPE((ST), elt)) \
897 return 0; \
898 } \
899}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000900
901#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
902 int i; \
903 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
904 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000905 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000906 if (!symtable_visit_ ## TYPE((ST), elt)) { \
907 symtable_exit_block((ST), (S)); \
908 return 0; \
909 } \
910 } \
911}
912
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000914symtable_new_tmpname(struct symtable *st)
915{
916 char tmpname[256];
917 identifier tmp;
918
919 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
920 ++st->st_cur->ste_tmpname);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000921 tmp = PyString_InternFromString(tmpname);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +0000922 if (!tmp)
923 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000924 if (!symtable_add_def(st, tmp, DEF_LOCAL))
925 return 0;
926 Py_DECREF(tmp);
927 return 1;
928}
929
930static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931symtable_visit_stmt(struct symtable *st, stmt_ty s)
932{
933 switch (s->kind) {
934 case FunctionDef_kind:
935 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
936 return 0;
937 if (s->v.FunctionDef.args->defaults)
938 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Christian Heimes5224d282008-02-23 15:01:05 +0000939 if (s->v.FunctionDef.decorator_list)
940 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 if (!symtable_enter_block(st, s->v.FunctionDef.name,
942 FunctionBlock, (void *)s, s->lineno))
943 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000944 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
945 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946 if (!symtable_exit_block(st, s))
947 return 0;
948 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000949 case ClassDef_kind: {
950 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
952 return 0;
953 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Christian Heimes5224d282008-02-23 15:01:05 +0000954 if (s->v.ClassDef.decorator_list)
955 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
957 (void *)s, s->lineno))
958 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000959 tmp = st->st_private;
960 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000961 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000962 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 if (!symtable_exit_block(st, s))
964 return 0;
965 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000966 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967 case Return_kind:
Georg Brandlddbaa662006-06-04 21:56:52 +0000968 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969 VISIT(st, expr, s->v.Return.value);
Georg Brandlddbaa662006-06-04 21:56:52 +0000970 st->st_cur->ste_returns_value = 1;
971 if (st->st_cur->ste_generator) {
972 PyErr_SetString(PyExc_SyntaxError,
973 RETURN_VAL_IN_GENERATOR);
974 PyErr_SyntaxLocation(st->st_filename,
975 s->lineno);
976 return 0;
977 }
978 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 break;
980 case Delete_kind:
981 VISIT_SEQ(st, expr, s->v.Delete.targets);
982 break;
983 case Assign_kind:
984 VISIT_SEQ(st, expr, s->v.Assign.targets);
985 VISIT(st, expr, s->v.Assign.value);
986 break;
987 case AugAssign_kind:
988 VISIT(st, expr, s->v.AugAssign.target);
989 VISIT(st, expr, s->v.AugAssign.value);
990 break;
991 case Print_kind:
992 if (s->v.Print.dest)
993 VISIT(st, expr, s->v.Print.dest);
994 VISIT_SEQ(st, expr, s->v.Print.values);
995 break;
996 case For_kind:
997 VISIT(st, expr, s->v.For.target);
998 VISIT(st, expr, s->v.For.iter);
999 VISIT_SEQ(st, stmt, s->v.For.body);
1000 if (s->v.For.orelse)
1001 VISIT_SEQ(st, stmt, s->v.For.orelse);
1002 break;
1003 case While_kind:
1004 VISIT(st, expr, s->v.While.test);
1005 VISIT_SEQ(st, stmt, s->v.While.body);
1006 if (s->v.While.orelse)
1007 VISIT_SEQ(st, stmt, s->v.While.orelse);
1008 break;
1009 case If_kind:
1010 /* XXX if 0: and lookup_yield() hacks */
1011 VISIT(st, expr, s->v.If.test);
1012 VISIT_SEQ(st, stmt, s->v.If.body);
1013 if (s->v.If.orelse)
1014 VISIT_SEQ(st, stmt, s->v.If.orelse);
1015 break;
1016 case Raise_kind:
1017 if (s->v.Raise.type) {
1018 VISIT(st, expr, s->v.Raise.type);
1019 if (s->v.Raise.inst) {
1020 VISIT(st, expr, s->v.Raise.inst);
1021 if (s->v.Raise.tback)
1022 VISIT(st, expr, s->v.Raise.tback);
1023 }
1024 }
1025 break;
1026 case TryExcept_kind:
1027 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1028 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1029 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1030 break;
1031 case TryFinally_kind:
1032 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1033 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1034 break;
1035 case Assert_kind:
1036 VISIT(st, expr, s->v.Assert.test);
1037 if (s->v.Assert.msg)
1038 VISIT(st, expr, s->v.Assert.msg);
1039 break;
1040 case Import_kind:
1041 VISIT_SEQ(st, alias, s->v.Import.names);
1042 /* XXX Don't have the lineno available inside
1043 visit_alias */
1044 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1045 st->st_cur->ste_opt_lineno = s->lineno;
1046 break;
1047 case ImportFrom_kind:
1048 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1049 /* XXX Don't have the lineno available inside
1050 visit_alias */
1051 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1052 st->st_cur->ste_opt_lineno = s->lineno;
1053 break;
1054 case Exec_kind:
1055 VISIT(st, expr, s->v.Exec.body);
1056 if (!st->st_cur->ste_opt_lineno)
1057 st->st_cur->ste_opt_lineno = s->lineno;
1058 if (s->v.Exec.globals) {
1059 st->st_cur->ste_unoptimized |= OPT_EXEC;
1060 VISIT(st, expr, s->v.Exec.globals);
1061 if (s->v.Exec.locals)
1062 VISIT(st, expr, s->v.Exec.locals);
1063 } else {
1064 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1065 }
1066 break;
1067 case Global_kind: {
1068 int i;
1069 asdl_seq *seq = s->v.Global.names;
1070 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001071 identifier name = (identifier)asdl_seq_GET(seq, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001072 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001073 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 if (cur < 0)
1075 return 0;
1076 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001077 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 if (cur & DEF_LOCAL)
1079 PyOS_snprintf(buf, sizeof(buf),
1080 GLOBAL_AFTER_ASSIGN,
1081 c_name);
1082 else
1083 PyOS_snprintf(buf, sizeof(buf),
1084 GLOBAL_AFTER_USE,
1085 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001086 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 return 0;
1088 }
1089 if (!symtable_add_def(st, name, DEF_GLOBAL))
1090 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092 break;
1093 }
1094 case Expr_kind:
1095 VISIT(st, expr, s->v.Expr.value);
1096 break;
1097 case Pass_kind:
1098 case Break_kind:
1099 case Continue_kind:
1100 /* nothing to do here */
1101 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001102 case With_kind:
1103 if (!symtable_new_tmpname(st))
1104 return 0;
1105 VISIT(st, expr, s->v.With.context_expr);
1106 if (s->v.With.optional_vars) {
1107 if (!symtable_new_tmpname(st))
1108 return 0;
1109 VISIT(st, expr, s->v.With.optional_vars);
1110 }
1111 VISIT_SEQ(st, stmt, s->v.With.body);
1112 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 }
1114 return 1;
1115}
1116
1117static int
1118symtable_visit_expr(struct symtable *st, expr_ty e)
1119{
1120 switch (e->kind) {
1121 case BoolOp_kind:
1122 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1123 break;
1124 case BinOp_kind:
1125 VISIT(st, expr, e->v.BinOp.left);
1126 VISIT(st, expr, e->v.BinOp.right);
1127 break;
1128 case UnaryOp_kind:
1129 VISIT(st, expr, e->v.UnaryOp.operand);
1130 break;
1131 case Lambda_kind: {
Neal Norwitz76059362006-08-19 04:52:03 +00001132 if (!GET_IDENTIFIER(lambda) ||
1133 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 return 0;
1135 if (e->v.Lambda.args->defaults)
1136 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1137 /* XXX how to get line numbers for expressions */
Neal Norwitz76059362006-08-19 04:52:03 +00001138 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 FunctionBlock, (void *)e, 0))
1140 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001141 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1142 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 if (!symtable_exit_block(st, (void *)e))
1144 return 0;
1145 break;
1146 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001147 case IfExp_kind:
1148 VISIT(st, expr, e->v.IfExp.test);
1149 VISIT(st, expr, e->v.IfExp.body);
1150 VISIT(st, expr, e->v.IfExp.orelse);
1151 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 case Dict_kind:
1153 VISIT_SEQ(st, expr, e->v.Dict.keys);
1154 VISIT_SEQ(st, expr, e->v.Dict.values);
1155 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001156 case ListComp_kind:
1157 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 return 0;
1159 VISIT(st, expr, e->v.ListComp.elt);
1160 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1161 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001162 case GeneratorExp_kind:
1163 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 case Yield_kind:
1167 if (e->v.Yield.value)
1168 VISIT(st, expr, e->v.Yield.value);
1169 st->st_cur->ste_generator = 1;
Georg Brandlddbaa662006-06-04 21:56:52 +00001170 if (st->st_cur->ste_returns_value) {
1171 PyErr_SetString(PyExc_SyntaxError,
1172 RETURN_VAL_IN_GENERATOR);
1173 PyErr_SyntaxLocation(st->st_filename,
1174 e->lineno);
1175 return 0;
1176 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 break;
1178 case Compare_kind:
1179 VISIT(st, expr, e->v.Compare.left);
1180 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1181 break;
1182 case Call_kind:
1183 VISIT(st, expr, e->v.Call.func);
1184 VISIT_SEQ(st, expr, e->v.Call.args);
1185 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1186 if (e->v.Call.starargs)
1187 VISIT(st, expr, e->v.Call.starargs);
1188 if (e->v.Call.kwargs)
1189 VISIT(st, expr, e->v.Call.kwargs);
1190 break;
1191 case Repr_kind:
1192 VISIT(st, expr, e->v.Repr.value);
1193 break;
1194 case Num_kind:
1195 case Str_kind:
1196 /* Nothing to do here. */
1197 break;
1198 /* The following exprs can be assignment targets. */
1199 case Attribute_kind:
1200 VISIT(st, expr, e->v.Attribute.value);
1201 break;
1202 case Subscript_kind:
1203 VISIT(st, expr, e->v.Subscript.value);
1204 VISIT(st, slice, e->v.Subscript.slice);
1205 break;
1206 case Name_kind:
1207 if (!symtable_add_def(st, e->v.Name.id,
1208 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1209 return 0;
1210 break;
1211 /* child nodes of List and Tuple will have expr_context set */
1212 case List_kind:
1213 VISIT_SEQ(st, expr, e->v.List.elts);
1214 break;
1215 case Tuple_kind:
1216 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1217 break;
1218 }
1219 return 1;
1220}
1221
1222static int
1223symtable_implicit_arg(struct symtable *st, int pos)
1224{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001225 PyObject *id = PyString_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 if (id == NULL)
1227 return 0;
1228 if (!symtable_add_def(st, id, DEF_PARAM)) {
1229 Py_DECREF(id);
1230 return 0;
1231 }
1232 Py_DECREF(id);
1233 return 1;
1234}
1235
1236static int
1237symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1238{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001239 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240
1241 /* go through all the toplevel arguments first */
1242 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001243 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 if (arg->kind == Name_kind) {
1245 assert(arg->v.Name.ctx == Param ||
1246 (arg->v.Name.ctx == Store && !toplevel));
1247 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1248 return 0;
1249 }
1250 else if (arg->kind == Tuple_kind) {
1251 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 if (toplevel) {
1253 if (!symtable_implicit_arg(st, i))
1254 return 0;
1255 }
1256 }
1257 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001258 PyErr_SetString(PyExc_SyntaxError,
1259 "invalid expression in parameter list");
1260 PyErr_SyntaxLocation(st->st_filename,
1261 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 return 0;
1263 }
1264 }
1265
1266 if (!toplevel) {
1267 if (!symtable_visit_params_nested(st, args))
1268 return 0;
1269 }
1270
1271 return 1;
1272}
1273
1274static int
1275symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1276{
1277 int i;
1278 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001279 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 if (arg->kind == Tuple_kind &&
1281 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1282 return 0;
1283 }
1284
1285 return 1;
1286}
1287
1288static int
1289symtable_visit_arguments(struct symtable *st, arguments_ty a)
1290{
1291 /* skip default arguments inside function block
1292 XXX should ast be different?
1293 */
1294 if (a->args && !symtable_visit_params(st, a->args, 1))
1295 return 0;
1296 if (a->vararg) {
1297 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1298 return 0;
1299 st->st_cur->ste_varargs = 1;
1300 }
1301 if (a->kwarg) {
1302 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1303 return 0;
1304 st->st_cur->ste_varkeywords = 1;
1305 }
1306 if (a->args && !symtable_visit_params_nested(st, a->args))
1307 return 0;
1308 return 1;
1309}
1310
1311
1312static int
1313symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1314{
Georg Brandla48f3ab2008-03-30 06:40:17 +00001315 if (eh->v.ExceptHandler.type)
1316 VISIT(st, expr, eh->v.ExceptHandler.type);
1317 if (eh->v.ExceptHandler.name)
1318 VISIT(st, expr, eh->v.ExceptHandler.name);
1319 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 return 1;
1321}
1322
1323
1324static int
1325symtable_visit_alias(struct symtable *st, alias_ty a)
1326{
1327 /* Compute store_name, the name actually bound by the import
1328 operation. It is diferent than a->name when a->name is a
1329 dotted package name (e.g. spam.eggs)
1330 */
1331 PyObject *store_name;
1332 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001333 const char *base = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 char *dot = strchr(base, '.');
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001335 if (dot) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001336 store_name = PyString_FromStringAndSize(base, dot - base);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001337 if (!store_name)
1338 return 0;
1339 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 else {
1341 store_name = name;
1342 Py_INCREF(store_name);
1343 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001344 if (strcmp(PyString_AS_STRING(name), "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1346 Py_DECREF(store_name);
1347 return r;
1348 }
1349 else {
1350 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001351 int lineno = st->st_cur->ste_lineno;
1352 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001353 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001355 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 }
1357 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001358 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 return 1;
1360 }
1361}
1362
1363
1364static int
1365symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1366{
1367 VISIT(st, expr, lc->target);
1368 VISIT(st, expr, lc->iter);
1369 VISIT_SEQ(st, expr, lc->ifs);
1370 return 1;
1371}
1372
1373
1374static int
1375symtable_visit_keyword(struct symtable *st, keyword_ty k)
1376{
1377 VISIT(st, expr, k->value);
1378 return 1;
1379}
1380
1381
1382static int
1383symtable_visit_slice(struct symtable *st, slice_ty s)
1384{
1385 switch (s->kind) {
1386 case Slice_kind:
1387 if (s->v.Slice.lower)
1388 VISIT(st, expr, s->v.Slice.lower)
1389 if (s->v.Slice.upper)
1390 VISIT(st, expr, s->v.Slice.upper)
1391 if (s->v.Slice.step)
1392 VISIT(st, expr, s->v.Slice.step)
1393 break;
1394 case ExtSlice_kind:
1395 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1396 break;
1397 case Index_kind:
1398 VISIT(st, expr, s->v.Index.value)
1399 break;
1400 case Ellipsis_kind:
1401 break;
1402 }
1403 return 1;
1404}
1405
1406static int
1407symtable_visit_genexp(struct symtable *st, expr_ty e)
1408{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 comprehension_ty outermost = ((comprehension_ty)
1410 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1411 /* Outermost iterator is evaluated in current scope */
1412 VISIT(st, expr, outermost->iter);
1413 /* Create generator scope for the rest */
Neal Norwitz76059362006-08-19 04:52:03 +00001414 if (!GET_IDENTIFIER(genexpr) ||
1415 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 return 0;
1417 }
1418 st->st_cur->ste_generator = 1;
1419 /* Outermost iter is received as an argument */
1420 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001421 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 return 0;
1423 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001424 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1425 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1426 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1427 e->v.GeneratorExp.generators, 1, (void*)e);
1428 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Neal Norwitz76059362006-08-19 04:52:03 +00001429 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430}