blob: f3a2c78c16ba240f778e608e71cbfb8330714978 [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 Hylton81e95022007-02-27 06:50:52 +000011#define NONLOCAL_AFTER_ASSIGN \
12"name '%.400s' is assigned to before nonlocal declaration"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_USE \
15"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_USE \
18"name '%.400s' is used prior to nonlocal declaration"
19
Neal Norwitz5d0ad502005-12-19 04:27:42 +000020#define IMPORT_STAR_WARNING "import * only allowed at module level"
21
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000022#define RETURN_VAL_IN_GENERATOR \
23 "'return' with argument inside generator"
Neal Norwitz5d0ad502005-12-19 04:27:42 +000024
Neal Norwitz090b3dd2006-02-28 22:36:46 +000025/* XXX(nnorwitz): change name since static? */
26static PySTEntryObject *
Neal Norwitz62c2fac2005-10-24 00:30:44 +000027PySTEntry_New(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000029{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000030 PySTEntryObject *ste = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000031 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000033 k = PyLong_FromVoidPtr(key);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000034 if (k == NULL)
35 goto fail;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000036 ste = (PySTEntryObject *)PyObject_New(PySTEntryObject,
37 &PySTEntry_Type);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000038 ste->ste_table = st;
39 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000040 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042 ste->ste_name = name;
43 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000045 ste->ste_symbols = NULL;
46 ste->ste_varnames = NULL;
47 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000048
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000049 ste->ste_symbols = PyDict_New();
50 if (ste->ste_symbols == NULL)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000051 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000052
53 ste->ste_varnames = PyList_New(0);
54 if (ste->ste_varnames == NULL)
55 goto fail;
56
57 ste->ste_children = PyList_New(0);
58 if (ste->ste_children == NULL)
59 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000060
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000061 ste->ste_type = block;
62 ste->ste_unoptimized = 0;
63 ste->ste_nested = 0;
64 ste->ste_free = 0;
65 ste->ste_varargs = 0;
66 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000067 ste->ste_opt_lineno = 0;
Jeremy Hylton4d508ad2003-05-21 17:34:50 +000068 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000071 if (st->st_cur != NULL &&
72 (st->st_cur->ste_nested ||
73 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000074 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000075 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000076 ste->ste_generator = 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000077 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000078
Nick Coghlan650f0d02007-04-15 12:05:43 +000079 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000080 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083 fail:
84 Py_XDECREF(ste);
85 return NULL;
86}
87
88static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000090{
91 char buf[256];
92
Barry Warsaw4b4ab202001-11-28 21:36:28 +000093 PyOS_snprintf(buf, sizeof(buf),
94 "<symtable entry %.100s(%ld), line %d>",
95 PyString_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Walter Dörwald1ab83302007-05-18 17:15:44 +000097 return PyUnicode_FromString(buf);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000098}
99
100static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000101ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000102{
103 ste->ste_table = NULL;
104 Py_XDECREF(ste->ste_id);
105 Py_XDECREF(ste->ste_name);
106 Py_XDECREF(ste->ste_symbols);
107 Py_XDECREF(ste->ste_varnames);
108 Py_XDECREF(ste->ste_children);
109 PyObject_Del(ste);
110}
111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000113
Guido van Rossum6f799372001-09-20 20:46:19 +0000114static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000115 {"id", T_OBJECT, OFF(ste_id), READONLY},
116 {"name", T_OBJECT, OFF(ste_name), READONLY},
117 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
118 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
119 {"children", T_OBJECT, OFF(ste_children), READONLY},
120 {"type", T_INT, OFF(ste_type), READONLY},
121 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000122 {NULL}
123};
124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125PyTypeObject PySTEntry_Type = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126 PyObject_HEAD_INIT(&PyType_Type)
127 0,
128 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000129 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130 0,
131 (destructor)ste_dealloc, /* tp_dealloc */
132 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000133 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000134 0, /* tp_setattr */
135 0, /* tp_compare */
136 (reprfunc)ste_repr, /* tp_repr */
137 0, /* tp_as_number */
138 0, /* tp_as_sequence */
139 0, /* tp_as_mapping */
140 0, /* tp_hash */
141 0, /* tp_call */
142 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000143 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000144 0, /* tp_setattro */
145 0, /* tp_as_buffer */
146 Py_TPFLAGS_DEFAULT, /* tp_flags */
147 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000148 0, /* tp_traverse */
149 0, /* tp_clear */
150 0, /* tp_richcompare */
151 0, /* tp_weaklistoffset */
152 0, /* tp_iter */
153 0, /* tp_iternext */
154 0, /* tp_methods */
155 ste_memberlist, /* tp_members */
156 0, /* tp_getset */
157 0, /* tp_base */
158 0, /* tp_dict */
159 0, /* tp_descr_get */
160 0, /* tp_descr_set */
161 0, /* tp_dictoffset */
162 0, /* tp_init */
163 0, /* tp_alloc */
164 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000165};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166
167static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000168static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000170 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171static int symtable_exit_block(struct symtable *st, void *ast);
172static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
173static int symtable_visit_expr(struct symtable *st, expr_ty s);
174static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000175static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
176static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177static int symtable_visit_arguments(struct symtable *st, arguments_ty);
178static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
179static int symtable_visit_alias(struct symtable *st, alias_ty);
180static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
181static int symtable_visit_keyword(struct symtable *st, keyword_ty);
182static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000183static int symtable_visit_params(struct symtable *st, asdl_seq *args);
184static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000186static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
188
Nick Coghlan650f0d02007-04-15 12:05:43 +0000189static identifier top = NULL, lambda = NULL, genexpr = NULL,
190 listcomp = NULL, setcomp = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
192#define GET_IDENTIFIER(VAR) \
193 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
194
195#define DUPLICATE_ARGUMENT \
196"duplicate argument '%s' in function definition"
197
198static struct symtable *
199symtable_new(void)
200{
201 struct symtable *st;
202
203 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
204 if (st == NULL)
205 return NULL;
206
207 st->st_filename = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000208 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000209
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 if ((st->st_stack = PyList_New(0)) == NULL)
211 goto fail;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000212 if ((st->st_blocks = PyDict_New()) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213 goto fail;
214 st->st_cur = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215 st->st_private = NULL;
216 return st;
217 fail:
218 PySymtable_Free(st);
219 return NULL;
220}
221
222struct symtable *
223PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
224{
225 struct symtable *st = symtable_new();
226 asdl_seq *seq;
227 int i;
228
229 if (st == NULL)
230 return st;
231 st->st_filename = filename;
232 st->st_future = future;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000233 /* Make the initial symbol information gathering pass */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000234 if (!GET_IDENTIFIER(top) ||
235 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000236 PySymtable_Free(st);
237 return NULL;
238 }
239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 st->st_top = st->st_cur;
241 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242 switch (mod->kind) {
243 case Module_kind:
244 seq = mod->v.Module.body;
245 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246 if (!symtable_visit_stmt(st,
247 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 goto error;
249 break;
250 case Expression_kind:
251 if (!symtable_visit_expr(st, mod->v.Expression.body))
252 goto error;
253 break;
254 case Interactive_kind:
255 seq = mod->v.Interactive.body;
256 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257 if (!symtable_visit_stmt(st,
258 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259 goto error;
260 break;
261 case Suite_kind:
262 PyErr_SetString(PyExc_RuntimeError,
263 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000264 goto error;
265 }
266 if (!symtable_exit_block(st, (void *)mod)) {
267 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 return NULL;
269 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000270 /* Make the second symbol analysis pass */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271 if (symtable_analyze(st))
272 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000273 PySymtable_Free(st);
274 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000276 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277 PySymtable_Free(st);
278 return NULL;
279}
280
281void
282PySymtable_Free(struct symtable *st)
283{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000284 Py_XDECREF(st->st_blocks);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 Py_XDECREF(st->st_stack);
286 PyMem_Free((void *)st);
287}
288
289PySTEntryObject *
290PySymtable_Lookup(struct symtable *st, void *key)
291{
292 PyObject *k, *v;
293
294 k = PyLong_FromVoidPtr(key);
295 if (k == NULL)
296 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000297 v = PyDict_GetItem(st->st_blocks, k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298 if (v) {
299 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301 }
302 else {
303 PyErr_SetString(PyExc_KeyError,
304 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000306
307 Py_DECREF(k);
308 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309}
310
311int
312PyST_GetScope(PySTEntryObject *ste, PyObject *name)
313{
314 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
315 if (!v)
316 return 0;
317 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000318 return (PyInt_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319}
320
321
322/* Analyze raw symbol information to determine scope of each name.
323
324 The next several functions are helpers for PySymtable_Analyze(),
325 which determines whether a name is local, global, or free. In addition,
326 it determines which local variables are cell variables; they provide
327 bindings that are used for free variables in enclosed blocks.
328
Nick Coghlan650f0d02007-04-15 12:05:43 +0000329 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330 explicit global is declared with the global statement. An implicit
331 global is a free variable for which the compiler has found no binding
332 in an enclosing function scope. The implicit global is either a global
333 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
334 to handle these names to implement slightly odd semantics. In such a
335 block, the name is treated as global until it is assigned to; then it
336 is treated as a local.
337
338 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000339 The first pass collects raw facts from the AST via the symtable_visit_*
340 functions: the name is a parameter here, the name is used but not defined
341 here, etc. The second pass analyzes these facts during a pass over the
342 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343
344 When a function is entered during the second pass, the parent passes
345 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000346 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000347 Names which are explicitly declared nonlocal must exist in this set of
348 visible names - if they do not, a syntax error is raised. After doing
349 the local analysis, it analyzes each of its child blocks using an
350 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351
Nick Coghlan650f0d02007-04-15 12:05:43 +0000352 The children update the free variable set. If a local variable is added to
353 the free variable set by the child, the variable is marked as a cell. The
354 function object being defined must provide runtime storage for the variable
355 that may outlive the function's frame. Cell variables are removed from the
356 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000357
Nick Coghlan650f0d02007-04-15 12:05:43 +0000358 During analysis, the names are:
359 symbols: dict mapping from symbol names to flag values (including offset scope values)
360 scopes: dict mapping from symbol names to scope values (no offset)
361 local: set of all symbol names local to the current scope
362 bound: set of all symbol names local to a containing function scope
363 free: set of all symbol names referenced but not bound in child scopes
364 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365*/
366
367#define SET_SCOPE(DICT, NAME, I) { \
368 PyObject *o = PyInt_FromLong(I); \
369 if (!o) \
370 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000371 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
372 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000374 } \
375 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376}
377
378/* Decide on scope of name, given flags.
379
380 The dicts passed in as arguments are modified as necessary.
381 ste is passed so that flags can be updated.
382*/
383
384static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000385analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386 PyObject *bound, PyObject *local, PyObject *free,
387 PyObject *global)
388{
389 if (flags & DEF_GLOBAL) {
390 if (flags & DEF_PARAM) {
391 PyErr_Format(PyExc_SyntaxError,
Nick Coghlan650f0d02007-04-15 12:05:43 +0000392 "name '%s' is parameter and global",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393 PyString_AS_STRING(name));
394 return 0;
395 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000396 if (flags & DEF_NONLOCAL) {
397 PyErr_Format(PyExc_SyntaxError,
398 "name '%s' is nonlocal and global",
399 PyString_AS_STRING(name));
400 return 0;
401 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000402 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
403 if (PySet_Add(global, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000405 if (bound && (PySet_Discard(bound, name) < 0))
406 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407 return 1;
408 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000409 if (flags & DEF_NONLOCAL) {
410 if (flags & DEF_PARAM) {
411 PyErr_Format(PyExc_SyntaxError,
Nick Coghlan650f0d02007-04-15 12:05:43 +0000412 "name '%s' is parameter and nonlocal",
Jeremy Hylton81e95022007-02-27 06:50:52 +0000413 PyString_AS_STRING(name));
414 return 0;
415 }
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000416 if (!bound) {
417 PyErr_Format(PyExc_SyntaxError,
418 "nonlocal declaration not allowed at module level");
419 return 0;
420 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000421 if (!PySet_Contains(bound, name)) {
Jeremy Hylton81e95022007-02-27 06:50:52 +0000422 PyErr_Format(PyExc_SyntaxError,
423 "no binding for nonlocal '%s' found",
424 PyString_AS_STRING(name));
425
426 return 0;
427 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000428 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000429 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000430 return PySet_Add(free, name) >= 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000431 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432 if (flags & DEF_BOUND) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000433 SET_SCOPE(scopes, name, LOCAL);
434 if (PySet_Add(local, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000436 if (PySet_Discard(global, name) < 0)
437 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000438 return 1;
439 }
440 /* If an enclosing block has a binding for this name, it
441 is a free variable rather than a global variable.
442 Note that having a non-NULL bound implies that the block
443 is nested.
444 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000445 if (bound && PySet_Contains(bound, name)) {
446 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000447 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000448 return PySet_Add(free, name) >= 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449 }
450 /* If a parent has a global statement, then call it global
451 explicit? It could also be global implicit.
452 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000453 if (global && PySet_Contains(global, name)) {
454 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455 return 1;
456 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000457 if (ste->ste_nested)
458 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000459 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000460 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461}
462
463#undef SET_SCOPE
464
465/* If a name is defined in free and also in locals, then this block
466 provides the binding for the free variable. The name should be
467 marked CELL in this block and removed from the free list.
468
469 Note that the current block's free variables are included in free.
470 That's safe because no name can be free and local in the same scope.
471*/
472
473static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000474analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000476 PyObject *name, *v, *v_cell;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000477 int success = 0;
478 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479
Nick Coghlan650f0d02007-04-15 12:05:43 +0000480 v_cell = PyInt_FromLong(CELL);
481 if (!v_cell)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000483 while (PyDict_Next(scopes, &pos, &name, &v)) {
484 long scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000486 scope = PyInt_AS_LONG(v);
487 if (scope != LOCAL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488 continue;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000489 if (!PySet_Contains(free, name))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 continue;
491 /* Replace LOCAL with CELL for this name, and remove
492 from free. It is safe to replace the value of name
493 in the dict, because it will not cause a resize.
494 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000495 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000497 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498 goto error;
499 }
500 success = 1;
501 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000502 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000503 return success;
504}
505
506/* Check for illegal statements in unoptimized namespaces */
507static int
508check_unoptimized(const PySTEntryObject* ste) {
509 char buf[300];
Armin Rigo31441302005-10-21 12:57:31 +0000510 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000512 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513 || !(ste->ste_free || ste->ste_child_free))
514 return 1;
515
Armin Rigo31441302005-10-21 12:57:31 +0000516 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517 "contains a nested function with free variables" :
518 "is a nested function");
519
520 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000521 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522 return 1;
523 case OPT_IMPORT_STAR:
524 PyOS_snprintf(buf, sizeof(buf),
525 "import * is not allowed in function '%.100s' "
526 "because it is %s",
527 PyString_AS_STRING(ste->ste_name), trailer);
528 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 }
530
531 PyErr_SetString(PyExc_SyntaxError, buf);
532 PyErr_SyntaxLocation(ste->ste_table->st_filename,
533 ste->ste_opt_lineno);
534 return 0;
535}
536
Nick Coghlan650f0d02007-04-15 12:05:43 +0000537/* Enter the final scope information into the ste_symbols dict.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 *
539 * All arguments are dicts. Modifies symbols, others are read-only.
540*/
541static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000542update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000545 PyObject *name = NULL, *itr = NULL;
546 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000547 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Nick Coghlan650f0d02007-04-15 12:05:43 +0000549 /* Update scope information for all symbols in this scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550 while (PyDict_Next(symbols, &pos, &name, &v)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000551 long scope, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552 assert(PyInt_Check(v));
553 flags = PyInt_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000554 v_scope = PyDict_GetItem(scopes, name);
555 assert(v_scope && PyInt_Check(v_scope));
556 scope = PyInt_AS_LONG(v_scope);
557 flags |= (scope << SCOPE_OFFSET);
558 v_new = PyInt_FromLong(flags);
559 if (!v_new)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000560 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000561 if (PyDict_SetItem(symbols, name, v_new) < 0) {
562 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 return 0;
564 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000565 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566 }
567
Nick Coghlan650f0d02007-04-15 12:05:43 +0000568 /* Record not yet resolved free variables from children (if any) */
569 v_free = PyInt_FromLong(FREE << SCOPE_OFFSET);
570 if (!v_free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 return 0;
572
Nick Coghlan650f0d02007-04-15 12:05:43 +0000573 itr = PyObject_GetIter(free);
574 if (!itr)
575 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576
Nick Coghlan650f0d02007-04-15 12:05:43 +0000577 while ((name = PyIter_Next(itr))) {
578 v = PyDict_GetItem(symbols, name);
579
580 /* Handle symbol that already exists in this scope */
581 if (v) {
582 /* Handle a free variable in a method of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 the class that has the same name as a local
584 or global in the class scope.
585 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000586 if (classflag &&
Nick Coghlan650f0d02007-04-15 12:05:43 +0000587 PyInt_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
588 long flags = PyInt_AS_LONG(v) | DEF_FREE_CLASS;
589 v_new = PyInt_FromLong(flags);
590 if (!v_new) {
591 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000593 if (PyDict_SetItem(symbols, name, v_new) < 0) {
594 Py_DECREF(v_new);
595 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000597 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000599 /* It's a cell, or already a free variable in this scope */
600 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 continue;
602 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000603 /* Handle global symbol */
604 if (!PySet_Contains(bound, name)) {
605 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 continue; /* it's a global */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000608 /* Propagate new free symbol up the lexical stack */
609 if (PyDict_SetItem(symbols, name, v_free) < 0) {
610 goto error;
611 }
612 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000614 Py_DECREF(itr);
615 Py_DECREF(v_free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000617error:
618 Py_XDECREF(v_free);
619 Py_XDECREF(itr);
620 Py_XDECREF(name);
621 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622}
623
624/* Make final symbol table decisions for block of ste.
625 Arguments:
626 ste -- current symtable entry (input/output)
627 bound -- set of variables bound in enclosing scopes (input)
628 free -- set of free variables in enclosed scopes (output)
629 globals -- set of declared global variables in enclosing scopes (input)
630*/
631
632static int
633analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
634 PyObject *global)
635{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000636 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638 int i, success = 0;
639 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640
Nick Coghlan650f0d02007-04-15 12:05:43 +0000641 scopes = PyDict_New();
642 if (!scopes)
643 goto error;
644 local = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (!local)
646 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000647 newglobal = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 if (!newglobal)
649 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000650 newfree = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 if (!newfree)
652 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000653 newbound = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654 if (!newbound)
655 goto error;
656
Nick Coghlan650f0d02007-04-15 12:05:43 +0000657 /* Class namespace has no effect on names visible in
658 nested functions, so populate the global and bound
659 sets to be passed to child blocks before analyzing
660 this one.
661 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662 if (ste->ste_type == ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000663 /* Pass down previously bound symbols */
664 if (bound) {
665 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000667 Py_DECREF(newbound);
668 }
669 /* Pass down known globals */
670 if (!PyNumber_InPlaceOr(newglobal, global))
671 goto error;
672 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 }
674
Nick Coghlan650f0d02007-04-15 12:05:43 +0000675 /* Analyze symbols in current scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 assert(PySTEntry_Check(ste));
677 assert(PyDict_Check(ste->ste_symbols));
678 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000679 long flags = PyInt_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000680 if (!analyze_name(ste, scopes, name, flags, bound, local, free,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681 global))
682 goto error;
683 }
684
Nick Coghlan650f0d02007-04-15 12:05:43 +0000685 /* Populate global and bound sets to be passed to children.
686 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000688 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 if (ste->ste_type == FunctionBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000690 if (!PyNumber_InPlaceOr(newbound, local))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000692 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000694 /* Pass down previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695 if (bound) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000696 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000698 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000700 /* Pass down known globals */
701 if (!PyNumber_InPlaceOr(newglobal, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000703 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 }
705
706 /* Recursively call analyze_block() on each child block */
707 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
708 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000709 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000711 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 if (!analyze_block(entry, newbound, newfree, newglobal))
713 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000714 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715 if (entry->ste_free || entry->ste_child_free)
716 ste->ste_child_free = 1;
717 }
718
Nick Coghlan650f0d02007-04-15 12:05:43 +0000719 /* Check if any local variables need to be converted to cell variables */
720 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000722 /* Records the results of the analysis in the symbol table entry */
723 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 ste->ste_type == ClassBlock))
725 goto error;
726 if (!check_unoptimized(ste))
727 goto error;
728
Nick Coghlan650f0d02007-04-15 12:05:43 +0000729 if (!PyNumber_InPlaceOr(free, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000731 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 success = 1;
733 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000734 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736 Py_XDECREF(newbound);
737 Py_XDECREF(newglobal);
738 Py_XDECREF(newfree);
739 if (!success)
740 assert(PyErr_Occurred());
741 return success;
742}
743
744static int
745symtable_analyze(struct symtable *st)
746{
747 PyObject *free, *global;
748 int r;
749
Nick Coghlan650f0d02007-04-15 12:05:43 +0000750 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 if (!free)
752 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000753 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000755 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 return 0;
757 }
758 r = analyze_block(st->st_top, NULL, free, global);
759 Py_DECREF(free);
760 Py_DECREF(global);
761 return r;
762}
763
764
765static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000766symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767{
768 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000769 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
771 PyErr_SetString(PyExc_SyntaxError, msg);
772 PyErr_SyntaxLocation(st->st_filename,
773 st->st_cur->ste_lineno);
774 }
775 return 0;
776 }
777 return 1;
778}
779
780/* symtable_enter_block() gets a reference via PySTEntry_New().
781 This reference is released when the block is exited, via the DECREF
782 in symtable_exit_block().
783*/
784
785static int
786symtable_exit_block(struct symtable *st, void *ast)
787{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000788 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000790 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 end = PyList_GET_SIZE(st->st_stack) - 1;
792 if (end >= 0) {
793 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
794 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000795 if (st->st_cur == NULL)
796 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 Py_INCREF(st->st_cur);
798 if (PySequence_DelItem(st->st_stack, end) < 0)
799 return 0;
800 }
801 return 1;
802}
803
804static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000805symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 void *ast, int lineno)
807{
808 PySTEntryObject *prev = NULL;
809
810 if (st->st_cur) {
811 prev = st->st_cur;
812 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 return 0;
814 }
815 Py_DECREF(st->st_cur);
816 }
817 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000818 if (st->st_cur == NULL)
819 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 if (name == GET_IDENTIFIER(top))
821 st->st_global = st->st_cur->ste_symbols;
822 if (prev) {
823 if (PyList_Append(prev->ste_children,
824 (PyObject *)st->st_cur) < 0) {
825 return 0;
826 }
827 }
828 return 1;
829}
830
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000831static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832symtable_lookup(struct symtable *st, PyObject *name)
833{
834 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000835 PyObject *mangled = _Py_Mangle(st->st_private, name);
836 if (!mangled)
837 return 0;
838 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
839 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 if (!o)
841 return 0;
842 return PyInt_AsLong(o);
843}
844
845static int
846symtable_add_def(struct symtable *st, PyObject *name, int flag)
847{
848 PyObject *o;
849 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000850 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000851 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852
Jeremy Hylton81e95022007-02-27 06:50:52 +0000853
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000854 if (!mangled)
855 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000857 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 val = PyInt_AS_LONG(o);
859 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000860 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
862 PyString_AsString(name));
863 PyErr_SyntaxLocation(st->st_filename,
864 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000865 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 }
867 val |= flag;
868 } else
869 val = flag;
870 o = PyInt_FromLong(val);
871 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000872 goto error;
873 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000875 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 }
877 Py_DECREF(o);
878
879 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000880 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
881 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 } else if (flag & DEF_GLOBAL) {
883 /* XXX need to update DEF_GLOBAL for other flags too;
884 perhaps only DEF_FREE_GLOBAL */
885 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000886 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 val |= PyInt_AS_LONG(o);
888 }
889 o = PyInt_FromLong(val);
890 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000891 goto error;
892 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000894 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895 }
896 Py_DECREF(o);
897 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000898 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000900
901error:
902 Py_DECREF(mangled);
903 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904}
905
906/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
907 They use the ASDL name to synthesize the name of the C type and the visit
908 function.
909
910 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
911 useful if the first node in the sequence requires special treatment.
912*/
913
914#define VISIT(ST, TYPE, V) \
915 if (!symtable_visit_ ## TYPE((ST), (V))) \
916 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000917
918#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
919 if (!symtable_visit_ ## TYPE((ST), (V))) { \
920 symtable_exit_block((ST), (S)); \
921 return 0; \
922 }
923
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924#define VISIT_SEQ(ST, TYPE, SEQ) { \
925 int i; \
926 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
927 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000928 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 if (!symtable_visit_ ## TYPE((ST), elt)) \
930 return 0; \
931 } \
932}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000933
934#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
935 int i; \
936 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
937 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000938 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000939 if (!symtable_visit_ ## TYPE((ST), elt)) { \
940 symtable_exit_block((ST), (S)); \
941 return 0; \
942 } \
943 } \
944}
945
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
947 int i; \
948 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
949 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000950 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951 if (!symtable_visit_ ## TYPE((ST), elt)) \
952 return 0; \
953 } \
954}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000955
956#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
957 int i; \
958 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
959 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000960 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000961 if (!symtable_visit_ ## TYPE((ST), elt)) { \
962 symtable_exit_block((ST), (S)); \
963 return 0; \
964 } \
965 } \
966}
967
Guido van Rossum4f72a782006-10-27 23:31:49 +0000968#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
969 int i = 0; \
970 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
971 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
972 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
973 if (!elt) continue; /* can be NULL */ \
974 if (!symtable_visit_expr((ST), elt)) \
975 return 0; \
976 } \
977}
978
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000980symtable_new_tmpname(struct symtable *st)
981{
982 char tmpname[256];
983 identifier tmp;
984
985 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
986 ++st->st_cur->ste_tmpname);
987 tmp = PyString_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000988 if (!tmp)
989 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000990 if (!symtable_add_def(st, tmp, DEF_LOCAL))
991 return 0;
992 Py_DECREF(tmp);
993 return 1;
994}
995
Guido van Rossum4f72a782006-10-27 23:31:49 +0000996
997
Guido van Rossumc2e20742006-02-27 22:32:47 +0000998static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999symtable_visit_stmt(struct symtable *st, stmt_ty s)
1000{
1001 switch (s->kind) {
1002 case FunctionDef_kind:
1003 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1004 return 0;
1005 if (s->v.FunctionDef.args->defaults)
1006 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001007 if (s->v.FunctionDef.args->kw_defaults)
1008 VISIT_KWONLYDEFAULTS(st,
1009 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001010 if (!symtable_visit_annotations(st, s))
1011 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001012 if (s->v.FunctionDef.decorator_list)
1013 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1015 FunctionBlock, (void *)s, s->lineno))
1016 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001017 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1018 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 if (!symtable_exit_block(st, s))
1020 return 0;
1021 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001022 case ClassDef_kind: {
1023 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1025 return 0;
1026 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001027 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1028 if (s->v.ClassDef.starargs)
1029 VISIT(st, expr, s->v.ClassDef.starargs);
1030 if (s->v.ClassDef.kwargs)
1031 VISIT(st, expr, s->v.ClassDef.kwargs);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001032 if (s->v.ClassDef.decorator_list)
1033 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1035 (void *)s, s->lineno))
1036 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001037 tmp = st->st_private;
1038 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001039 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001040 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 if (!symtable_exit_block(st, s))
1042 return 0;
1043 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001044 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001046 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001048 st->st_cur->ste_returns_value = 1;
1049 if (st->st_cur->ste_generator) {
1050 PyErr_SetString(PyExc_SyntaxError,
1051 RETURN_VAL_IN_GENERATOR);
1052 PyErr_SyntaxLocation(st->st_filename,
1053 s->lineno);
1054 return 0;
1055 }
1056 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 break;
1058 case Delete_kind:
1059 VISIT_SEQ(st, expr, s->v.Delete.targets);
1060 break;
1061 case Assign_kind:
1062 VISIT_SEQ(st, expr, s->v.Assign.targets);
1063 VISIT(st, expr, s->v.Assign.value);
1064 break;
1065 case AugAssign_kind:
1066 VISIT(st, expr, s->v.AugAssign.target);
1067 VISIT(st, expr, s->v.AugAssign.value);
1068 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 case For_kind:
1070 VISIT(st, expr, s->v.For.target);
1071 VISIT(st, expr, s->v.For.iter);
1072 VISIT_SEQ(st, stmt, s->v.For.body);
1073 if (s->v.For.orelse)
1074 VISIT_SEQ(st, stmt, s->v.For.orelse);
1075 break;
1076 case While_kind:
1077 VISIT(st, expr, s->v.While.test);
1078 VISIT_SEQ(st, stmt, s->v.While.body);
1079 if (s->v.While.orelse)
1080 VISIT_SEQ(st, stmt, s->v.While.orelse);
1081 break;
1082 case If_kind:
1083 /* XXX if 0: and lookup_yield() hacks */
1084 VISIT(st, expr, s->v.If.test);
1085 VISIT_SEQ(st, stmt, s->v.If.body);
1086 if (s->v.If.orelse)
1087 VISIT_SEQ(st, stmt, s->v.If.orelse);
1088 break;
1089 case Raise_kind:
1090 if (s->v.Raise.type) {
1091 VISIT(st, expr, s->v.Raise.type);
1092 if (s->v.Raise.inst) {
1093 VISIT(st, expr, s->v.Raise.inst);
1094 if (s->v.Raise.tback)
1095 VISIT(st, expr, s->v.Raise.tback);
1096 }
1097 }
1098 break;
1099 case TryExcept_kind:
1100 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1101 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1102 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1103 break;
1104 case TryFinally_kind:
1105 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1106 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1107 break;
1108 case Assert_kind:
1109 VISIT(st, expr, s->v.Assert.test);
1110 if (s->v.Assert.msg)
1111 VISIT(st, expr, s->v.Assert.msg);
1112 break;
1113 case Import_kind:
1114 VISIT_SEQ(st, alias, s->v.Import.names);
1115 /* XXX Don't have the lineno available inside
1116 visit_alias */
1117 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1118 st->st_cur->ste_opt_lineno = s->lineno;
1119 break;
1120 case ImportFrom_kind:
1121 VISIT_SEQ(st, alias, s->v.ImportFrom.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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 case Global_kind: {
1128 int i;
1129 asdl_seq *seq = s->v.Global.names;
1130 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001131 identifier name = (identifier)asdl_seq_GET(seq, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001133 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 if (cur < 0)
1135 return 0;
1136 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001137 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 if (cur & DEF_LOCAL)
1139 PyOS_snprintf(buf, sizeof(buf),
1140 GLOBAL_AFTER_ASSIGN,
1141 c_name);
1142 else
1143 PyOS_snprintf(buf, sizeof(buf),
1144 GLOBAL_AFTER_USE,
1145 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001146 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 return 0;
1148 }
1149 if (!symtable_add_def(st, name, DEF_GLOBAL))
1150 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 break;
1153 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001154 case Nonlocal_kind: {
1155 int i;
1156 asdl_seq *seq = s->v.Nonlocal.names;
1157 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1158 identifier name = (identifier)asdl_seq_GET(seq, i);
1159 char *c_name = PyString_AS_STRING(name);
1160 long cur = symtable_lookup(st, name);
1161 if (cur < 0)
1162 return 0;
1163 if (cur & (DEF_LOCAL | USE)) {
1164 char buf[256];
1165 if (cur & DEF_LOCAL)
1166 PyOS_snprintf(buf, sizeof(buf),
1167 NONLOCAL_AFTER_ASSIGN,
1168 c_name);
1169 else
1170 PyOS_snprintf(buf, sizeof(buf),
1171 NONLOCAL_AFTER_USE,
1172 c_name);
1173 if (!symtable_warn(st, buf, s->lineno))
1174 return 0;
1175 }
1176 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1177 return 0;
1178 }
1179 break;
1180 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 case Expr_kind:
1182 VISIT(st, expr, s->v.Expr.value);
1183 break;
1184 case Pass_kind:
1185 case Break_kind:
1186 case Continue_kind:
1187 /* nothing to do here */
1188 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001189 case With_kind:
1190 if (!symtable_new_tmpname(st))
1191 return 0;
1192 VISIT(st, expr, s->v.With.context_expr);
1193 if (s->v.With.optional_vars) {
1194 if (!symtable_new_tmpname(st))
1195 return 0;
1196 VISIT(st, expr, s->v.With.optional_vars);
1197 }
1198 VISIT_SEQ(st, stmt, s->v.With.body);
1199 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 }
1201 return 1;
1202}
1203
1204static int
1205symtable_visit_expr(struct symtable *st, expr_ty e)
1206{
1207 switch (e->kind) {
1208 case BoolOp_kind:
1209 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1210 break;
1211 case BinOp_kind:
1212 VISIT(st, expr, e->v.BinOp.left);
1213 VISIT(st, expr, e->v.BinOp.right);
1214 break;
1215 case UnaryOp_kind:
1216 VISIT(st, expr, e->v.UnaryOp.operand);
1217 break;
1218 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001219 if (!GET_IDENTIFIER(lambda) ||
1220 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 return 0;
1222 if (e->v.Lambda.args->defaults)
1223 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1224 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001225 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 FunctionBlock, (void *)e, 0))
1227 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001228 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1229 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 if (!symtable_exit_block(st, (void *)e))
1231 return 0;
1232 break;
1233 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001234 case IfExp_kind:
1235 VISIT(st, expr, e->v.IfExp.test);
1236 VISIT(st, expr, e->v.IfExp.body);
1237 VISIT(st, expr, e->v.IfExp.orelse);
1238 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239 case Dict_kind:
1240 VISIT_SEQ(st, expr, e->v.Dict.keys);
1241 VISIT_SEQ(st, expr, e->v.Dict.values);
1242 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001243 case Set_kind:
1244 VISIT_SEQ(st, expr, e->v.Set.elts);
1245 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001246 case GeneratorExp_kind:
1247 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001250 case ListComp_kind:
1251 if (!symtable_visit_listcomp(st, e))
1252 return 0;
1253 break;
1254 case SetComp_kind:
1255 if (!symtable_visit_setcomp(st, e))
1256 return 0;
1257 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 case Yield_kind:
1259 if (e->v.Yield.value)
1260 VISIT(st, expr, e->v.Yield.value);
1261 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001262 if (st->st_cur->ste_returns_value) {
1263 PyErr_SetString(PyExc_SyntaxError,
1264 RETURN_VAL_IN_GENERATOR);
1265 PyErr_SyntaxLocation(st->st_filename,
1266 e->lineno);
1267 return 0;
1268 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 break;
1270 case Compare_kind:
1271 VISIT(st, expr, e->v.Compare.left);
1272 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1273 break;
1274 case Call_kind:
1275 VISIT(st, expr, e->v.Call.func);
1276 VISIT_SEQ(st, expr, e->v.Call.args);
1277 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1278 if (e->v.Call.starargs)
1279 VISIT(st, expr, e->v.Call.starargs);
1280 if (e->v.Call.kwargs)
1281 VISIT(st, expr, e->v.Call.kwargs);
1282 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 case Num_kind:
1284 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001285 case Bytes_kind:
1286 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 /* Nothing to do here. */
1288 break;
1289 /* The following exprs can be assignment targets. */
1290 case Attribute_kind:
1291 VISIT(st, expr, e->v.Attribute.value);
1292 break;
1293 case Subscript_kind:
1294 VISIT(st, expr, e->v.Subscript.value);
1295 VISIT(st, slice, e->v.Subscript.slice);
1296 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001297 case Starred_kind:
1298 VISIT(st, expr, e->v.Starred.value);
1299 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 case Name_kind:
1301 if (!symtable_add_def(st, e->v.Name.id,
1302 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1303 return 0;
1304 break;
1305 /* child nodes of List and Tuple will have expr_context set */
1306 case List_kind:
1307 VISIT_SEQ(st, expr, e->v.List.elts);
1308 break;
1309 case Tuple_kind:
1310 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1311 break;
1312 }
1313 return 1;
1314}
1315
1316static int
1317symtable_implicit_arg(struct symtable *st, int pos)
1318{
1319 PyObject *id = PyString_FromFormat(".%d", pos);
1320 if (id == NULL)
1321 return 0;
1322 if (!symtable_add_def(st, id, DEF_PARAM)) {
1323 Py_DECREF(id);
1324 return 0;
1325 }
1326 Py_DECREF(id);
1327 return 1;
1328}
1329
1330static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001331symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001333 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001334
1335 if (!args)
1336 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001339 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001340 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 return 0;
1342 }
1343
1344 return 1;
1345}
1346
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001347static int
1348symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349{
1350 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001351
1352 if (!args)
1353 return -1;
1354
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001356 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001357 if (arg->annotation)
1358 VISIT(st, expr, arg->annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001360
1361 return 1;
1362}
1363
Neal Norwitzc1505362006-12-28 06:47:50 +00001364static int
1365symtable_visit_annotations(struct symtable *st, stmt_ty s)
1366{
1367 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001369 if (a->args && !symtable_visit_argannotations(st, a->args))
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 return 0;
1371 if (a->varargannotation)
1372 VISIT(st, expr, a->varargannotation);
1373 if (a->kwargannotation)
1374 VISIT(st, expr, a->kwargannotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001375 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
Neal Norwitzc1505362006-12-28 06:47:50 +00001376 return 0;
1377 if (s->v.FunctionDef.returns)
1378 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 return 1;
1380}
1381
1382static int
1383symtable_visit_arguments(struct symtable *st, arguments_ty a)
1384{
1385 /* skip default arguments inside function block
1386 XXX should ast be different?
1387 */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001388 if (a->args && !symtable_visit_params(st, a->args))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389 return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001390 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001391 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 if (a->vararg) {
1393 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1394 return 0;
1395 st->st_cur->ste_varargs = 1;
1396 }
1397 if (a->kwarg) {
1398 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1399 return 0;
1400 st->st_cur->ste_varkeywords = 1;
1401 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 return 1;
1403}
1404
1405
1406static int
1407symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1408{
1409 if (eh->type)
1410 VISIT(st, expr, eh->type);
1411 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001412 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1413 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 VISIT_SEQ(st, stmt, eh->body);
1415 return 1;
1416}
1417
1418
1419static int
1420symtable_visit_alias(struct symtable *st, alias_ty a)
1421{
1422 /* Compute store_name, the name actually bound by the import
1423 operation. It is diferent than a->name when a->name is a
1424 dotted package name (e.g. spam.eggs)
1425 */
1426 PyObject *store_name;
1427 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1428 const char *base = PyString_AS_STRING(name);
1429 char *dot = strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001430 if (dot) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 store_name = PyString_FromStringAndSize(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001432 if (!store_name)
1433 return 0;
1434 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 else {
1436 store_name = name;
1437 Py_INCREF(store_name);
1438 }
1439 if (strcmp(PyString_AS_STRING(name), "*")) {
1440 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1441 Py_DECREF(store_name);
1442 return r;
1443 }
1444 else {
1445 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001446 int lineno = st->st_cur->ste_lineno;
1447 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001448 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001450 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 }
1452 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001453 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 return 1;
1455 }
1456}
1457
1458
1459static int
1460symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1461{
1462 VISIT(st, expr, lc->target);
1463 VISIT(st, expr, lc->iter);
1464 VISIT_SEQ(st, expr, lc->ifs);
1465 return 1;
1466}
1467
1468
1469static int
1470symtable_visit_keyword(struct symtable *st, keyword_ty k)
1471{
1472 VISIT(st, expr, k->value);
1473 return 1;
1474}
1475
1476
1477static int
1478symtable_visit_slice(struct symtable *st, slice_ty s)
1479{
1480 switch (s->kind) {
1481 case Slice_kind:
1482 if (s->v.Slice.lower)
1483 VISIT(st, expr, s->v.Slice.lower)
1484 if (s->v.Slice.upper)
1485 VISIT(st, expr, s->v.Slice.upper)
1486 if (s->v.Slice.step)
1487 VISIT(st, expr, s->v.Slice.step)
1488 break;
1489 case ExtSlice_kind:
1490 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1491 break;
1492 case Index_kind:
1493 VISIT(st, expr, s->v.Index.value)
1494 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 }
1496 return 1;
1497}
1498
1499static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001500symtable_handle_comprehension(struct symtable *st, expr_ty e,
1501 identifier scope_name,
1502 asdl_seq *generators, expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001504 int is_generator = (e->kind == GeneratorExp_kind);
1505 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001507 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 /* Outermost iterator is evaluated in current scope */
1509 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001510 /* Create comprehension scope for the rest */
1511 if (!scope_name ||
1512 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 return 0;
1514 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001515 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 /* Outermost iter is received as an argument */
1517 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001518 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 return 0;
1520 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001521 /* Allocate temporary name if needed */
1522 if (needs_tmp && !symtable_new_tmpname(st)) {
1523 symtable_exit_block(st, (void *)e);
1524 return 0;
1525 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001526 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1527 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1528 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001529 generators, 1, (void*)e);
1530 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001531 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001533
1534static int
1535symtable_visit_genexp(struct symtable *st, expr_ty e)
1536{
1537 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1538 e->v.GeneratorExp.generators,
1539 e->v.GeneratorExp.elt);
1540}
1541
1542static int
1543symtable_visit_listcomp(struct symtable *st, expr_ty e)
1544{
1545 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1546 e->v.ListComp.generators,
1547 e->v.ListComp.elt);
1548}
1549
1550static int
1551symtable_visit_setcomp(struct symtable *st, expr_ty e)
1552{
1553 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1554 e->v.SetComp.generators,
1555 e->v.SetComp.elt);
1556}