blob: 1871a420d9e6d68082111a4f1f9f6c916c71a70b [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
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000364 The namespace dictionaries may be modified to record information
365 about the new name. For example, a new global will add an entry to
366 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367*/
368
369static int
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000370analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371 PyObject *bound, PyObject *local, PyObject *free,
372 PyObject *global)
373{
374 if (flags & DEF_GLOBAL) {
375 if (flags & DEF_PARAM) {
376 PyErr_Format(PyExc_SyntaxError,
377 "name '%s' is local and global",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000378 PyString_AS_STRING(name));
Benjamin Peterson08473322008-08-16 22:11:33 +0000379 PyErr_SyntaxLocation(ste->ste_table->st_filename,
380 ste->ste_lineno);
381
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 return 0;
383 }
384 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
385 if (PyDict_SetItem(global, name, Py_None) < 0)
386 return 0;
387 if (bound && PyDict_GetItem(bound, name)) {
388 if (PyDict_DelItem(bound, name) < 0)
389 return 0;
390 }
391 return 1;
392 }
393 if (flags & DEF_BOUND) {
394 SET_SCOPE(dict, name, LOCAL);
395 if (PyDict_SetItem(local, name, Py_None) < 0)
396 return 0;
397 if (PyDict_GetItem(global, name)) {
398 if (PyDict_DelItem(global, name) < 0)
399 return 0;
400 }
401 return 1;
402 }
403 /* If an enclosing block has a binding for this name, it
404 is a free variable rather than a global variable.
405 Note that having a non-NULL bound implies that the block
406 is nested.
407 */
408 if (bound && PyDict_GetItem(bound, name)) {
409 SET_SCOPE(dict, name, FREE);
410 ste->ste_free = 1;
411 if (PyDict_SetItem(free, name, Py_None) < 0)
412 return 0;
413 return 1;
414 }
415 /* If a parent has a global statement, then call it global
416 explicit? It could also be global implicit.
417 */
418 else if (global && PyDict_GetItem(global, name)) {
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000419 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420 return 1;
421 }
422 else {
423 if (ste->ste_nested)
424 ste->ste_free = 1;
425 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
426 return 1;
427 }
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000428 /* Should never get here. */
429 PyErr_Format(PyExc_SystemError, "failed to set scope for %s",
430 PyString_AS_STRING(name));
431 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432}
433
434#undef SET_SCOPE
435
436/* If a name is defined in free and also in locals, then this block
437 provides the binding for the free variable. The name should be
438 marked CELL in this block and removed from the free list.
439
440 Note that the current block's free variables are included in free.
441 That's safe because no name can be free and local in the same scope.
442*/
443
444static int
445analyze_cells(PyObject *scope, PyObject *free)
446{
447 PyObject *name, *v, *w;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000448 int success = 0;
449 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450
451 w = PyInt_FromLong(CELL);
452 if (!w)
453 return 0;
454 while (PyDict_Next(scope, &pos, &name, &v)) {
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000455 long flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000456 assert(PyInt_Check(v));
Tim Petersd8fe7ab2006-01-08 02:19:07 +0000457 flags = PyInt_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458 if (flags != LOCAL)
459 continue;
460 if (!PyDict_GetItem(free, name))
461 continue;
462 /* Replace LOCAL with CELL for this name, and remove
463 from free. It is safe to replace the value of name
464 in the dict, because it will not cause a resize.
465 */
466 if (PyDict_SetItem(scope, name, w) < 0)
467 goto error;
468 if (!PyDict_DelItem(free, name) < 0)
469 goto error;
470 }
471 success = 1;
472 error:
473 Py_DECREF(w);
474 return success;
475}
476
477/* Check for illegal statements in unoptimized namespaces */
478static int
479check_unoptimized(const PySTEntryObject* ste) {
480 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000481 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000483 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484 || !(ste->ste_free || ste->ste_child_free))
485 return 1;
486
Armin Rigo31441302005-10-21 12:57:31 +0000487 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488 "contains a nested function with free variables" :
489 "is a nested function");
490
491 switch (ste->ste_unoptimized) {
492 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
493 case OPT_EXEC: /* qualified exec is fine */
494 return 1;
495 case OPT_IMPORT_STAR:
496 PyOS_snprintf(buf, sizeof(buf),
497 "import * is not allowed in function '%.100s' "
498 "because it is %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000499 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 break;
501 case OPT_BARE_EXEC:
502 PyOS_snprintf(buf, sizeof(buf),
503 "unqualified exec is not allowed in function "
504 "'%.100s' it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000505 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 break;
507 default:
508 PyOS_snprintf(buf, sizeof(buf),
509 "function '%.100s' uses import * and bare exec, "
510 "which are illegal because it %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000511 PyString_AS_STRING(ste->ste_name), trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 break;
513 }
514
515 PyErr_SetString(PyExc_SyntaxError, buf);
516 PyErr_SyntaxLocation(ste->ste_table->st_filename,
517 ste->ste_opt_lineno);
518 return 0;
519}
520
521/* Enter the final scope information into the st_symbols dict.
522 *
523 * All arguments are dicts. Modifies symbols, others are read-only.
524*/
525static int
526update_symbols(PyObject *symbols, PyObject *scope,
Anthony Baxter019aec62006-04-12 04:00:50 +0000527 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528{
529 PyObject *name, *v, *u, *w, *free_value = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000530 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531
532 while (PyDict_Next(symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000533 long i, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534 assert(PyInt_Check(v));
535 flags = PyInt_AS_LONG(v);
536 w = PyDict_GetItem(scope, name);
537 assert(w && PyInt_Check(w));
538 i = PyInt_AS_LONG(w);
539 flags |= (i << SCOPE_OFF);
540 u = PyInt_FromLong(flags);
Neal Norwitz18b6adf2006-07-23 07:50:36 +0000541 if (!u)
542 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543 if (PyDict_SetItem(symbols, name, u) < 0) {
544 Py_DECREF(u);
545 return 0;
546 }
547 Py_DECREF(u);
548 }
549
550 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
551 if (!free_value)
552 return 0;
553
554 /* add a free variable when it's only use is for creating a closure */
555 pos = 0;
556 while (PyDict_Next(free, &pos, &name, &v)) {
557 PyObject *o = PyDict_GetItem(symbols, name);
558
559 if (o) {
560 /* It could be a free variable in a method of
561 the class that has the same name as a local
562 or global in the class scope.
563 */
Anthony Baxter019aec62006-04-12 04:00:50 +0000564 if (classflag &&
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000566 long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 o = PyInt_FromLong(i);
568 if (!o) {
569 Py_DECREF(free_value);
570 return 0;
571 }
572 if (PyDict_SetItem(symbols, name, o) < 0) {
573 Py_DECREF(o);
574 Py_DECREF(free_value);
575 return 0;
576 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000577 Py_DECREF(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 }
579 /* else it's not free, probably a cell */
580 continue;
581 }
582 if (!PyDict_GetItem(bound, name))
583 continue; /* it's a global */
584
585 if (PyDict_SetItem(symbols, name, free_value) < 0) {
586 Py_DECREF(free_value);
587 return 0;
588 }
589 }
590 Py_DECREF(free_value);
591 return 1;
592}
593
594/* Make final symbol table decisions for block of ste.
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000595
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596 Arguments:
597 ste -- current symtable entry (input/output)
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000598 bound -- set of variables bound in enclosing scopes (input). bound
599 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 free -- set of free variables in enclosed scopes (output)
601 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000602
603 The implementation uses two mutually recursive functions,
604 analyze_block() and analyze_child_block(). analyze_block() is
605 responsible for analyzing the individual names defined in a block.
606 analyze_child_block() prepares temporary namespace dictionaries
607 used to evaluated nested blocks.
608
609 The two functions exist because a child block should see the name
610 bindings of its enclosing blocks, but those bindings should not
611 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612*/
613
614static int
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000615analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
616 PyObject *global, PyObject* child_free);
617
618static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
620 PyObject *global)
621{
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000622 PyObject *name, *v, *local = NULL, *scope = NULL;
623 PyObject *newbound = NULL, *newglobal = NULL;
624 PyObject *newfree = NULL, *allfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000625 int i, success = 0;
626 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000628 local = PyDict_New(); /* collect new names bound in block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629 if (!local)
630 goto error;
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000631 scope = PyDict_New(); /* collect scopes defined for each name */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632 if (!scope)
633 goto error;
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000634
635 /* Allocate new global and bound variable dictionaries. These
636 dictionaries hold the names visible in nested blocks. For
637 ClassBlocks, the bound and global names are initialized
638 before analyzing names, because class bindings aren't
639 visible in methods. For other blocks, they are initialized
640 after names are analyzed.
641 */
642
643 /* TODO(jhylton): Package these dicts in a struct so that we
644 can write reasonable helper functions?
645 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 newglobal = PyDict_New();
647 if (!newglobal)
648 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649 newbound = PyDict_New();
650 if (!newbound)
651 goto error;
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000652 newfree = PyDict_New();
653 if (!newfree)
654 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655
656 if (ste->ste_type == ClassBlock) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657 if (PyDict_Update(newglobal, global) < 0)
658 goto error;
659 if (bound)
660 if (PyDict_Update(newbound, bound) < 0)
661 goto error;
662 }
663
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000665 long flags = PyInt_AS_LONG(v);
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000666 if (!analyze_name(ste, scope, name, flags,
667 bound, local, free, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 goto error;
669 }
670
671 if (ste->ste_type != ClassBlock) {
672 if (ste->ste_type == FunctionBlock) {
673 if (PyDict_Update(newbound, local) < 0)
674 goto error;
675 }
676 if (bound) {
677 if (PyDict_Update(newbound, bound) < 0)
678 goto error;
679 }
680 if (PyDict_Update(newglobal, global) < 0)
681 goto error;
682 }
683
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000684 /* Recursively call analyze_block() on each child block.
685
686 newbound, newglobal now contain the names visible in
687 nested blocks. The free variables in the children will
688 be collected in allfree.
689 */
690 allfree = PyDict_New();
691 if (!allfree)
692 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
694 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000695 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000697 entry = (PySTEntryObject*)c;
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000698 if (!analyze_child_block(entry, newbound, newfree, newglobal,
699 allfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 goto error;
701 if (entry->ste_free || entry->ste_child_free)
702 ste->ste_child_free = 1;
703 }
704
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000705 PyDict_Update(newfree, allfree);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
707 goto error;
708 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
709 ste->ste_type == ClassBlock))
710 goto error;
711 if (!check_unoptimized(ste))
712 goto error;
713
714 if (PyDict_Update(free, newfree) < 0)
715 goto error;
716 success = 1;
717 error:
718 Py_XDECREF(local);
719 Py_XDECREF(scope);
720 Py_XDECREF(newbound);
721 Py_XDECREF(newglobal);
722 Py_XDECREF(newfree);
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000723 Py_XDECREF(allfree);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 if (!success)
725 assert(PyErr_Occurred());
726 return success;
727}
728
729static int
Jeremy Hyltoncfb3d332009-03-31 14:30:05 +0000730analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
731 PyObject *global, PyObject* child_free)
732{
733 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
734 int success = 0;
735
736 /* Copy the bound and global dictionaries.
737
738 These dictionary are used by all blocks enclosed by the
739 current block. The analyze_block() call modifies these
740 dictionaries.
741
742 */
743 temp_bound = PyDict_New();
744 if (!temp_bound)
745 goto error;
746 if (PyDict_Update(temp_bound, bound) < 0)
747 goto error;
748 temp_free = PyDict_New();
749 if (!temp_free)
750 goto error;
751 if (PyDict_Update(temp_free, free) < 0)
752 goto error;
753 temp_global = PyDict_New();
754 if (!temp_global)
755 goto error;
756 if (PyDict_Update(temp_global, global) < 0)
757 goto error;
758
759 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
760 goto error;
761 success = PyDict_Update(child_free, temp_free) >= 0;
762 success = 1;
763 error:
764 Py_XDECREF(temp_bound);
765 Py_XDECREF(temp_free);
766 Py_XDECREF(temp_global);
767 return success;
768}
769
770static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771symtable_analyze(struct symtable *st)
772{
773 PyObject *free, *global;
774 int r;
775
776 free = PyDict_New();
777 if (!free)
778 return 0;
779 global = PyDict_New();
780 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000781 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782 return 0;
783 }
784 r = analyze_block(st->st_top, NULL, free, global);
785 Py_DECREF(free);
786 Py_DECREF(global);
787 return r;
788}
789
790
791static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000792symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793{
794 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000795 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
797 PyErr_SetString(PyExc_SyntaxError, msg);
798 PyErr_SyntaxLocation(st->st_filename,
799 st->st_cur->ste_lineno);
800 }
801 return 0;
802 }
803 return 1;
804}
805
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000806/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 This reference is released when the block is exited, via the DECREF
808 in symtable_exit_block().
809*/
810
811static int
812symtable_exit_block(struct symtable *st, void *ast)
813{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000814 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000816 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 end = PyList_GET_SIZE(st->st_stack) - 1;
818 if (end >= 0) {
819 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
820 end);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000821 if (st->st_cur == NULL)
822 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 Py_INCREF(st->st_cur);
824 if (PySequence_DelItem(st->st_stack, end) < 0)
825 return 0;
826 }
827 return 1;
828}
829
830static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000831symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 void *ast, int lineno)
833{
834 PySTEntryObject *prev = NULL;
835
836 if (st->st_cur) {
837 prev = st->st_cur;
838 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 return 0;
840 }
841 Py_DECREF(st->st_cur);
842 }
Benjamin Petersone0d12eb2008-08-16 23:29:40 +0000843 st->st_cur = ste_new(st, name, block, ast, lineno);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000844 if (st->st_cur == NULL)
845 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 if (name == GET_IDENTIFIER(top))
847 st->st_global = st->st_cur->ste_symbols;
848 if (prev) {
849 if (PyList_Append(prev->ste_children,
850 (PyObject *)st->st_cur) < 0) {
851 return 0;
852 }
853 }
854 return 1;
855}
856
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000857static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858symtable_lookup(struct symtable *st, PyObject *name)
859{
860 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000861 PyObject *mangled = _Py_Mangle(st->st_private, name);
862 if (!mangled)
863 return 0;
864 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
865 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 if (!o)
867 return 0;
868 return PyInt_AsLong(o);
869}
870
871static int
872symtable_add_def(struct symtable *st, PyObject *name, int flag)
873{
874 PyObject *o;
875 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000876 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000877 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000879 if (!mangled)
880 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000882 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883 val = PyInt_AS_LONG(o);
884 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000885 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000887 PyString_AsString(name));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 PyErr_SyntaxLocation(st->st_filename,
889 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000890 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891 }
892 val |= flag;
893 } else
894 val = flag;
895 o = PyInt_FromLong(val);
896 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000897 goto error;
898 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000900 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901 }
902 Py_DECREF(o);
903
904 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000905 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
906 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907 } else if (flag & DEF_GLOBAL) {
908 /* XXX need to update DEF_GLOBAL for other flags too;
909 perhaps only DEF_FREE_GLOBAL */
910 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000911 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 val |= PyInt_AS_LONG(o);
913 }
914 o = PyInt_FromLong(val);
915 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000916 goto error;
917 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000919 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920 }
921 Py_DECREF(o);
922 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000923 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000925
926error:
927 Py_DECREF(mangled);
928 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929}
930
931/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
932 They use the ASDL name to synthesize the name of the C type and the visit
933 function.
934
935 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
936 useful if the first node in the sequence requires special treatment.
937*/
938
939#define VISIT(ST, TYPE, V) \
940 if (!symtable_visit_ ## TYPE((ST), (V))) \
941 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000942
943#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
944 if (!symtable_visit_ ## TYPE((ST), (V))) { \
945 symtable_exit_block((ST), (S)); \
946 return 0; \
947 }
948
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949#define VISIT_SEQ(ST, TYPE, SEQ) { \
950 int i; \
951 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
952 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000953 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954 if (!symtable_visit_ ## TYPE((ST), elt)) \
955 return 0; \
956 } \
957}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000958
959#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
960 int i; \
961 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
962 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000963 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000964 if (!symtable_visit_ ## TYPE((ST), elt)) { \
965 symtable_exit_block((ST), (S)); \
966 return 0; \
967 } \
968 } \
969}
970
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
972 int i; \
973 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
974 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000975 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 if (!symtable_visit_ ## TYPE((ST), elt)) \
977 return 0; \
978 } \
979}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000980
981#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
982 int i; \
983 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
984 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Anthony Baxter019aec62006-04-12 04:00:50 +0000985 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000986 if (!symtable_visit_ ## TYPE((ST), elt)) { \
987 symtable_exit_block((ST), (S)); \
988 return 0; \
989 } \
990 } \
991}
992
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000994symtable_new_tmpname(struct symtable *st)
995{
996 char tmpname[256];
997 identifier tmp;
998
999 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1000 ++st->st_cur->ste_tmpname);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001001 tmp = PyString_InternFromString(tmpname);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001002 if (!tmp)
1003 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001004 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1005 return 0;
1006 Py_DECREF(tmp);
1007 return 1;
1008}
1009
1010static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011symtable_visit_stmt(struct symtable *st, stmt_ty s)
1012{
1013 switch (s->kind) {
1014 case FunctionDef_kind:
1015 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1016 return 0;
1017 if (s->v.FunctionDef.args->defaults)
1018 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Christian Heimes5224d282008-02-23 15:01:05 +00001019 if (s->v.FunctionDef.decorator_list)
1020 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1022 FunctionBlock, (void *)s, s->lineno))
1023 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001024 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1025 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 if (!symtable_exit_block(st, s))
1027 return 0;
1028 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001029 case ClassDef_kind: {
1030 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1032 return 0;
1033 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Christian Heimes5224d282008-02-23 15:01:05 +00001034 if (s->v.ClassDef.decorator_list)
1035 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1037 (void *)s, s->lineno))
1038 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001039 tmp = st->st_private;
1040 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001041 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001042 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 if (!symtable_exit_block(st, s))
1044 return 0;
1045 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001046 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 case Return_kind:
Georg Brandlddbaa662006-06-04 21:56:52 +00001048 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049 VISIT(st, expr, s->v.Return.value);
Georg Brandlddbaa662006-06-04 21:56:52 +00001050 st->st_cur->ste_returns_value = 1;
1051 if (st->st_cur->ste_generator) {
1052 PyErr_SetString(PyExc_SyntaxError,
1053 RETURN_VAL_IN_GENERATOR);
1054 PyErr_SyntaxLocation(st->st_filename,
1055 s->lineno);
1056 return 0;
1057 }
1058 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 break;
1060 case Delete_kind:
1061 VISIT_SEQ(st, expr, s->v.Delete.targets);
1062 break;
1063 case Assign_kind:
1064 VISIT_SEQ(st, expr, s->v.Assign.targets);
1065 VISIT(st, expr, s->v.Assign.value);
1066 break;
1067 case AugAssign_kind:
1068 VISIT(st, expr, s->v.AugAssign.target);
1069 VISIT(st, expr, s->v.AugAssign.value);
1070 break;
1071 case Print_kind:
1072 if (s->v.Print.dest)
1073 VISIT(st, expr, s->v.Print.dest);
1074 VISIT_SEQ(st, expr, s->v.Print.values);
1075 break;
1076 case For_kind:
1077 VISIT(st, expr, s->v.For.target);
1078 VISIT(st, expr, s->v.For.iter);
1079 VISIT_SEQ(st, stmt, s->v.For.body);
1080 if (s->v.For.orelse)
1081 VISIT_SEQ(st, stmt, s->v.For.orelse);
1082 break;
1083 case While_kind:
1084 VISIT(st, expr, s->v.While.test);
1085 VISIT_SEQ(st, stmt, s->v.While.body);
1086 if (s->v.While.orelse)
1087 VISIT_SEQ(st, stmt, s->v.While.orelse);
1088 break;
1089 case If_kind:
1090 /* XXX if 0: and lookup_yield() hacks */
1091 VISIT(st, expr, s->v.If.test);
1092 VISIT_SEQ(st, stmt, s->v.If.body);
1093 if (s->v.If.orelse)
1094 VISIT_SEQ(st, stmt, s->v.If.orelse);
1095 break;
1096 case Raise_kind:
1097 if (s->v.Raise.type) {
1098 VISIT(st, expr, s->v.Raise.type);
1099 if (s->v.Raise.inst) {
1100 VISIT(st, expr, s->v.Raise.inst);
1101 if (s->v.Raise.tback)
1102 VISIT(st, expr, s->v.Raise.tback);
1103 }
1104 }
1105 break;
1106 case TryExcept_kind:
1107 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1108 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1109 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1110 break;
1111 case TryFinally_kind:
1112 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1113 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1114 break;
1115 case Assert_kind:
1116 VISIT(st, expr, s->v.Assert.test);
1117 if (s->v.Assert.msg)
1118 VISIT(st, expr, s->v.Assert.msg);
1119 break;
1120 case Import_kind:
1121 VISIT_SEQ(st, alias, s->v.Import.names);
1122 /* XXX Don't have the lineno available inside
1123 visit_alias */
1124 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1125 st->st_cur->ste_opt_lineno = s->lineno;
1126 break;
1127 case ImportFrom_kind:
1128 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1129 /* XXX Don't have the lineno available inside
1130 visit_alias */
1131 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1132 st->st_cur->ste_opt_lineno = s->lineno;
1133 break;
1134 case Exec_kind:
1135 VISIT(st, expr, s->v.Exec.body);
1136 if (!st->st_cur->ste_opt_lineno)
1137 st->st_cur->ste_opt_lineno = s->lineno;
1138 if (s->v.Exec.globals) {
1139 st->st_cur->ste_unoptimized |= OPT_EXEC;
1140 VISIT(st, expr, s->v.Exec.globals);
1141 if (s->v.Exec.locals)
1142 VISIT(st, expr, s->v.Exec.locals);
1143 } else {
1144 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
1145 }
1146 break;
1147 case Global_kind: {
1148 int i;
1149 asdl_seq *seq = s->v.Global.names;
1150 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001151 identifier name = (identifier)asdl_seq_GET(seq, i);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001152 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001153 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 if (cur < 0)
1155 return 0;
1156 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001157 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 if (cur & DEF_LOCAL)
1159 PyOS_snprintf(buf, sizeof(buf),
1160 GLOBAL_AFTER_ASSIGN,
1161 c_name);
1162 else
1163 PyOS_snprintf(buf, sizeof(buf),
1164 GLOBAL_AFTER_USE,
1165 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001166 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 return 0;
1168 }
1169 if (!symtable_add_def(st, name, DEF_GLOBAL))
1170 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 break;
1173 }
1174 case Expr_kind:
1175 VISIT(st, expr, s->v.Expr.value);
1176 break;
1177 case Pass_kind:
1178 case Break_kind:
1179 case Continue_kind:
1180 /* nothing to do here */
1181 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001182 case With_kind:
1183 if (!symtable_new_tmpname(st))
1184 return 0;
1185 VISIT(st, expr, s->v.With.context_expr);
1186 if (s->v.With.optional_vars) {
1187 if (!symtable_new_tmpname(st))
1188 return 0;
1189 VISIT(st, expr, s->v.With.optional_vars);
1190 }
1191 VISIT_SEQ(st, stmt, s->v.With.body);
1192 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 }
1194 return 1;
1195}
1196
1197static int
1198symtable_visit_expr(struct symtable *st, expr_ty e)
1199{
1200 switch (e->kind) {
1201 case BoolOp_kind:
1202 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1203 break;
1204 case BinOp_kind:
1205 VISIT(st, expr, e->v.BinOp.left);
1206 VISIT(st, expr, e->v.BinOp.right);
1207 break;
1208 case UnaryOp_kind:
1209 VISIT(st, expr, e->v.UnaryOp.operand);
1210 break;
1211 case Lambda_kind: {
Neal Norwitz76059362006-08-19 04:52:03 +00001212 if (!GET_IDENTIFIER(lambda) ||
1213 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 return 0;
1215 if (e->v.Lambda.args->defaults)
1216 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1217 /* XXX how to get line numbers for expressions */
Neal Norwitz76059362006-08-19 04:52:03 +00001218 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 FunctionBlock, (void *)e, 0))
1220 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001221 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1222 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 if (!symtable_exit_block(st, (void *)e))
1224 return 0;
1225 break;
1226 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001227 case IfExp_kind:
1228 VISIT(st, expr, e->v.IfExp.test);
1229 VISIT(st, expr, e->v.IfExp.body);
1230 VISIT(st, expr, e->v.IfExp.orelse);
1231 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 case Dict_kind:
1233 VISIT_SEQ(st, expr, e->v.Dict.keys);
1234 VISIT_SEQ(st, expr, e->v.Dict.values);
1235 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001236 case ListComp_kind:
1237 if (!symtable_new_tmpname(st))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 return 0;
1239 VISIT(st, expr, e->v.ListComp.elt);
1240 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1241 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001242 case GeneratorExp_kind:
1243 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 case Yield_kind:
1247 if (e->v.Yield.value)
1248 VISIT(st, expr, e->v.Yield.value);
1249 st->st_cur->ste_generator = 1;
Georg Brandlddbaa662006-06-04 21:56:52 +00001250 if (st->st_cur->ste_returns_value) {
1251 PyErr_SetString(PyExc_SyntaxError,
1252 RETURN_VAL_IN_GENERATOR);
1253 PyErr_SyntaxLocation(st->st_filename,
1254 e->lineno);
1255 return 0;
1256 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 break;
1258 case Compare_kind:
1259 VISIT(st, expr, e->v.Compare.left);
1260 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1261 break;
1262 case Call_kind:
1263 VISIT(st, expr, e->v.Call.func);
1264 VISIT_SEQ(st, expr, e->v.Call.args);
1265 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1266 if (e->v.Call.starargs)
1267 VISIT(st, expr, e->v.Call.starargs);
1268 if (e->v.Call.kwargs)
1269 VISIT(st, expr, e->v.Call.kwargs);
1270 break;
1271 case Repr_kind:
1272 VISIT(st, expr, e->v.Repr.value);
1273 break;
1274 case Num_kind:
1275 case Str_kind:
1276 /* Nothing to do here. */
1277 break;
1278 /* The following exprs can be assignment targets. */
1279 case Attribute_kind:
1280 VISIT(st, expr, e->v.Attribute.value);
1281 break;
1282 case Subscript_kind:
1283 VISIT(st, expr, e->v.Subscript.value);
1284 VISIT(st, slice, e->v.Subscript.slice);
1285 break;
1286 case Name_kind:
1287 if (!symtable_add_def(st, e->v.Name.id,
1288 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1289 return 0;
1290 break;
1291 /* child nodes of List and Tuple will have expr_context set */
1292 case List_kind:
1293 VISIT_SEQ(st, expr, e->v.List.elts);
1294 break;
1295 case Tuple_kind:
1296 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1297 break;
1298 }
1299 return 1;
1300}
1301
1302static int
1303symtable_implicit_arg(struct symtable *st, int pos)
1304{
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001305 PyObject *id = PyString_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 if (id == NULL)
1307 return 0;
1308 if (!symtable_add_def(st, id, DEF_PARAM)) {
1309 Py_DECREF(id);
1310 return 0;
1311 }
1312 Py_DECREF(id);
1313 return 1;
1314}
1315
1316static int
1317symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1318{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001319 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320
1321 /* go through all the toplevel arguments first */
1322 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001323 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 if (arg->kind == Name_kind) {
1325 assert(arg->v.Name.ctx == Param ||
1326 (arg->v.Name.ctx == Store && !toplevel));
1327 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1328 return 0;
1329 }
1330 else if (arg->kind == Tuple_kind) {
1331 assert(arg->v.Tuple.ctx == Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 if (toplevel) {
1333 if (!symtable_implicit_arg(st, i))
1334 return 0;
1335 }
1336 }
1337 else {
Neal Norwitz4737b232005-11-19 23:58:29 +00001338 PyErr_SetString(PyExc_SyntaxError,
1339 "invalid expression in parameter list");
1340 PyErr_SyntaxLocation(st->st_filename,
1341 st->st_cur->ste_lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 return 0;
1343 }
1344 }
1345
1346 if (!toplevel) {
1347 if (!symtable_visit_params_nested(st, args))
1348 return 0;
1349 }
1350
1351 return 1;
1352}
1353
1354static int
1355symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1356{
1357 int i;
1358 for (i = 0; i < asdl_seq_LEN(args); i++) {
Anthony Baxter019aec62006-04-12 04:00:50 +00001359 expr_ty arg = (expr_ty)asdl_seq_GET(args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 if (arg->kind == Tuple_kind &&
1361 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1362 return 0;
1363 }
1364
1365 return 1;
1366}
1367
1368static int
1369symtable_visit_arguments(struct symtable *st, arguments_ty a)
1370{
1371 /* skip default arguments inside function block
1372 XXX should ast be different?
1373 */
1374 if (a->args && !symtable_visit_params(st, a->args, 1))
1375 return 0;
1376 if (a->vararg) {
1377 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1378 return 0;
1379 st->st_cur->ste_varargs = 1;
1380 }
1381 if (a->kwarg) {
1382 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1383 return 0;
1384 st->st_cur->ste_varkeywords = 1;
1385 }
1386 if (a->args && !symtable_visit_params_nested(st, a->args))
1387 return 0;
1388 return 1;
1389}
1390
1391
1392static int
1393symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1394{
Georg Brandla48f3ab2008-03-30 06:40:17 +00001395 if (eh->v.ExceptHandler.type)
1396 VISIT(st, expr, eh->v.ExceptHandler.type);
1397 if (eh->v.ExceptHandler.name)
1398 VISIT(st, expr, eh->v.ExceptHandler.name);
1399 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 return 1;
1401}
1402
1403
1404static int
1405symtable_visit_alias(struct symtable *st, alias_ty a)
1406{
1407 /* Compute store_name, the name actually bound by the import
1408 operation. It is diferent than a->name when a->name is a
1409 dotted package name (e.g. spam.eggs)
1410 */
1411 PyObject *store_name;
1412 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001413 const char *base = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 char *dot = strchr(base, '.');
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001415 if (dot) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001416 store_name = PyString_FromStringAndSize(base, dot - base);
Neal Norwitz6f5ff3f2006-08-12 01:43:40 +00001417 if (!store_name)
1418 return 0;
1419 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 else {
1421 store_name = name;
1422 Py_INCREF(store_name);
1423 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001424 if (strcmp(PyString_AS_STRING(name), "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1426 Py_DECREF(store_name);
1427 return r;
1428 }
1429 else {
1430 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001431 int lineno = st->st_cur->ste_lineno;
1432 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001433 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001435 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 }
1437 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001438 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 return 1;
1440 }
1441}
1442
1443
1444static int
1445symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1446{
1447 VISIT(st, expr, lc->target);
1448 VISIT(st, expr, lc->iter);
1449 VISIT_SEQ(st, expr, lc->ifs);
1450 return 1;
1451}
1452
1453
1454static int
1455symtable_visit_keyword(struct symtable *st, keyword_ty k)
1456{
1457 VISIT(st, expr, k->value);
1458 return 1;
1459}
1460
1461
1462static int
1463symtable_visit_slice(struct symtable *st, slice_ty s)
1464{
1465 switch (s->kind) {
1466 case Slice_kind:
1467 if (s->v.Slice.lower)
1468 VISIT(st, expr, s->v.Slice.lower)
1469 if (s->v.Slice.upper)
1470 VISIT(st, expr, s->v.Slice.upper)
1471 if (s->v.Slice.step)
1472 VISIT(st, expr, s->v.Slice.step)
1473 break;
1474 case ExtSlice_kind:
1475 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1476 break;
1477 case Index_kind:
1478 VISIT(st, expr, s->v.Index.value)
1479 break;
1480 case Ellipsis_kind:
1481 break;
1482 }
1483 return 1;
1484}
1485
1486static int
1487symtable_visit_genexp(struct symtable *st, expr_ty e)
1488{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 comprehension_ty outermost = ((comprehension_ty)
1490 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1491 /* Outermost iterator is evaluated in current scope */
1492 VISIT(st, expr, outermost->iter);
1493 /* Create generator scope for the rest */
Neal Norwitz76059362006-08-19 04:52:03 +00001494 if (!GET_IDENTIFIER(genexpr) ||
1495 !symtable_enter_block(st, genexpr, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 return 0;
1497 }
1498 st->st_cur->ste_generator = 1;
1499 /* Outermost iter is received as an argument */
1500 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001501 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 return 0;
1503 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001504 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1505 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1506 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1507 e->v.GeneratorExp.generators, 1, (void*)e);
1508 VISIT_IN_BLOCK(st, expr, e->v.GeneratorExp.elt, (void*)e);
Neal Norwitz76059362006-08-19 04:52:03 +00001509 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510}