blob: 58f673eb809e232762fed279bdafe20cb7a23bdc [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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 if (s->v.FunctionDef.decorators)
1013 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
1014 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);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1033 (void *)s, s->lineno))
1034 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001035 tmp = st->st_private;
1036 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001037 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001038 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 if (!symtable_exit_block(st, s))
1040 return 0;
1041 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001042 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001044 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001046 st->st_cur->ste_returns_value = 1;
1047 if (st->st_cur->ste_generator) {
1048 PyErr_SetString(PyExc_SyntaxError,
1049 RETURN_VAL_IN_GENERATOR);
1050 PyErr_SyntaxLocation(st->st_filename,
1051 s->lineno);
1052 return 0;
1053 }
1054 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 break;
1056 case Delete_kind:
1057 VISIT_SEQ(st, expr, s->v.Delete.targets);
1058 break;
1059 case Assign_kind:
1060 VISIT_SEQ(st, expr, s->v.Assign.targets);
1061 VISIT(st, expr, s->v.Assign.value);
1062 break;
1063 case AugAssign_kind:
1064 VISIT(st, expr, s->v.AugAssign.target);
1065 VISIT(st, expr, s->v.AugAssign.value);
1066 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 case For_kind:
1068 VISIT(st, expr, s->v.For.target);
1069 VISIT(st, expr, s->v.For.iter);
1070 VISIT_SEQ(st, stmt, s->v.For.body);
1071 if (s->v.For.orelse)
1072 VISIT_SEQ(st, stmt, s->v.For.orelse);
1073 break;
1074 case While_kind:
1075 VISIT(st, expr, s->v.While.test);
1076 VISIT_SEQ(st, stmt, s->v.While.body);
1077 if (s->v.While.orelse)
1078 VISIT_SEQ(st, stmt, s->v.While.orelse);
1079 break;
1080 case If_kind:
1081 /* XXX if 0: and lookup_yield() hacks */
1082 VISIT(st, expr, s->v.If.test);
1083 VISIT_SEQ(st, stmt, s->v.If.body);
1084 if (s->v.If.orelse)
1085 VISIT_SEQ(st, stmt, s->v.If.orelse);
1086 break;
1087 case Raise_kind:
1088 if (s->v.Raise.type) {
1089 VISIT(st, expr, s->v.Raise.type);
1090 if (s->v.Raise.inst) {
1091 VISIT(st, expr, s->v.Raise.inst);
1092 if (s->v.Raise.tback)
1093 VISIT(st, expr, s->v.Raise.tback);
1094 }
1095 }
1096 break;
1097 case TryExcept_kind:
1098 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1099 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1100 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1101 break;
1102 case TryFinally_kind:
1103 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1104 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1105 break;
1106 case Assert_kind:
1107 VISIT(st, expr, s->v.Assert.test);
1108 if (s->v.Assert.msg)
1109 VISIT(st, expr, s->v.Assert.msg);
1110 break;
1111 case Import_kind:
1112 VISIT_SEQ(st, alias, s->v.Import.names);
1113 /* XXX Don't have the lineno available inside
1114 visit_alias */
1115 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1116 st->st_cur->ste_opt_lineno = s->lineno;
1117 break;
1118 case ImportFrom_kind:
1119 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1120 /* XXX Don't have the lineno available inside
1121 visit_alias */
1122 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1123 st->st_cur->ste_opt_lineno = s->lineno;
1124 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 case Global_kind: {
1126 int i;
1127 asdl_seq *seq = s->v.Global.names;
1128 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001129 identifier name = (identifier)asdl_seq_GET(seq, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 char *c_name = PyString_AS_STRING(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001131 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 if (cur < 0)
1133 return 0;
1134 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001135 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 if (cur & DEF_LOCAL)
1137 PyOS_snprintf(buf, sizeof(buf),
1138 GLOBAL_AFTER_ASSIGN,
1139 c_name);
1140 else
1141 PyOS_snprintf(buf, sizeof(buf),
1142 GLOBAL_AFTER_USE,
1143 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001144 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 return 0;
1146 }
1147 if (!symtable_add_def(st, name, DEF_GLOBAL))
1148 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 break;
1151 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001152 case Nonlocal_kind: {
1153 int i;
1154 asdl_seq *seq = s->v.Nonlocal.names;
1155 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1156 identifier name = (identifier)asdl_seq_GET(seq, i);
1157 char *c_name = PyString_AS_STRING(name);
1158 long cur = symtable_lookup(st, name);
1159 if (cur < 0)
1160 return 0;
1161 if (cur & (DEF_LOCAL | USE)) {
1162 char buf[256];
1163 if (cur & DEF_LOCAL)
1164 PyOS_snprintf(buf, sizeof(buf),
1165 NONLOCAL_AFTER_ASSIGN,
1166 c_name);
1167 else
1168 PyOS_snprintf(buf, sizeof(buf),
1169 NONLOCAL_AFTER_USE,
1170 c_name);
1171 if (!symtable_warn(st, buf, s->lineno))
1172 return 0;
1173 }
1174 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1175 return 0;
1176 }
1177 break;
1178 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 case Expr_kind:
1180 VISIT(st, expr, s->v.Expr.value);
1181 break;
1182 case Pass_kind:
1183 case Break_kind:
1184 case Continue_kind:
1185 /* nothing to do here */
1186 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001187 case With_kind:
1188 if (!symtable_new_tmpname(st))
1189 return 0;
1190 VISIT(st, expr, s->v.With.context_expr);
1191 if (s->v.With.optional_vars) {
1192 if (!symtable_new_tmpname(st))
1193 return 0;
1194 VISIT(st, expr, s->v.With.optional_vars);
1195 }
1196 VISIT_SEQ(st, stmt, s->v.With.body);
1197 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 }
1199 return 1;
1200}
1201
1202static int
1203symtable_visit_expr(struct symtable *st, expr_ty e)
1204{
1205 switch (e->kind) {
1206 case BoolOp_kind:
1207 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1208 break;
1209 case BinOp_kind:
1210 VISIT(st, expr, e->v.BinOp.left);
1211 VISIT(st, expr, e->v.BinOp.right);
1212 break;
1213 case UnaryOp_kind:
1214 VISIT(st, expr, e->v.UnaryOp.operand);
1215 break;
1216 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001217 if (!GET_IDENTIFIER(lambda) ||
1218 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 return 0;
1220 if (e->v.Lambda.args->defaults)
1221 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1222 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001223 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 FunctionBlock, (void *)e, 0))
1225 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001226 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1227 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228 if (!symtable_exit_block(st, (void *)e))
1229 return 0;
1230 break;
1231 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001232 case IfExp_kind:
1233 VISIT(st, expr, e->v.IfExp.test);
1234 VISIT(st, expr, e->v.IfExp.body);
1235 VISIT(st, expr, e->v.IfExp.orelse);
1236 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 case Dict_kind:
1238 VISIT_SEQ(st, expr, e->v.Dict.keys);
1239 VISIT_SEQ(st, expr, e->v.Dict.values);
1240 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001241 case Set_kind:
1242 VISIT_SEQ(st, expr, e->v.Set.elts);
1243 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001244 case GeneratorExp_kind:
1245 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001248 case ListComp_kind:
1249 if (!symtable_visit_listcomp(st, e))
1250 return 0;
1251 break;
1252 case SetComp_kind:
1253 if (!symtable_visit_setcomp(st, e))
1254 return 0;
1255 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 case Yield_kind:
1257 if (e->v.Yield.value)
1258 VISIT(st, expr, e->v.Yield.value);
1259 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001260 if (st->st_cur->ste_returns_value) {
1261 PyErr_SetString(PyExc_SyntaxError,
1262 RETURN_VAL_IN_GENERATOR);
1263 PyErr_SyntaxLocation(st->st_filename,
1264 e->lineno);
1265 return 0;
1266 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 break;
1268 case Compare_kind:
1269 VISIT(st, expr, e->v.Compare.left);
1270 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1271 break;
1272 case Call_kind:
1273 VISIT(st, expr, e->v.Call.func);
1274 VISIT_SEQ(st, expr, e->v.Call.args);
1275 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1276 if (e->v.Call.starargs)
1277 VISIT(st, expr, e->v.Call.starargs);
1278 if (e->v.Call.kwargs)
1279 VISIT(st, expr, e->v.Call.kwargs);
1280 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 case Num_kind:
1282 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001283 case Bytes_kind:
1284 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 /* Nothing to do here. */
1286 break;
1287 /* The following exprs can be assignment targets. */
1288 case Attribute_kind:
1289 VISIT(st, expr, e->v.Attribute.value);
1290 break;
1291 case Subscript_kind:
1292 VISIT(st, expr, e->v.Subscript.value);
1293 VISIT(st, slice, e->v.Subscript.slice);
1294 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001295 case Starred_kind:
1296 VISIT(st, expr, e->v.Starred.value);
1297 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 case Name_kind:
1299 if (!symtable_add_def(st, e->v.Name.id,
1300 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1301 return 0;
1302 break;
1303 /* child nodes of List and Tuple will have expr_context set */
1304 case List_kind:
1305 VISIT_SEQ(st, expr, e->v.List.elts);
1306 break;
1307 case Tuple_kind:
1308 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1309 break;
1310 }
1311 return 1;
1312}
1313
1314static int
1315symtable_implicit_arg(struct symtable *st, int pos)
1316{
1317 PyObject *id = PyString_FromFormat(".%d", pos);
1318 if (id == NULL)
1319 return 0;
1320 if (!symtable_add_def(st, id, DEF_PARAM)) {
1321 Py_DECREF(id);
1322 return 0;
1323 }
1324 Py_DECREF(id);
1325 return 1;
1326}
1327
1328static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001329symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001331 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001332
1333 if (!args)
1334 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001337 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001338 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 return 0;
1340 }
1341
1342 return 1;
1343}
1344
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001345static int
1346symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347{
1348 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001349
1350 if (!args)
1351 return -1;
1352
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001354 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001355 if (arg->annotation)
1356 VISIT(st, expr, arg->annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001358
1359 return 1;
1360}
1361
Neal Norwitzc1505362006-12-28 06:47:50 +00001362static int
1363symtable_visit_annotations(struct symtable *st, stmt_ty s)
1364{
1365 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001367 if (a->args && !symtable_visit_argannotations(st, a->args))
Neal Norwitzc1505362006-12-28 06:47:50 +00001368 return 0;
1369 if (a->varargannotation)
1370 VISIT(st, expr, a->varargannotation);
1371 if (a->kwargannotation)
1372 VISIT(st, expr, a->kwargannotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001373 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
Neal Norwitzc1505362006-12-28 06:47:50 +00001374 return 0;
1375 if (s->v.FunctionDef.returns)
1376 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 return 1;
1378}
1379
1380static int
1381symtable_visit_arguments(struct symtable *st, arguments_ty a)
1382{
1383 /* skip default arguments inside function block
1384 XXX should ast be different?
1385 */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001386 if (a->args && !symtable_visit_params(st, a->args))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001388 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001389 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390 if (a->vararg) {
1391 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1392 return 0;
1393 st->st_cur->ste_varargs = 1;
1394 }
1395 if (a->kwarg) {
1396 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1397 return 0;
1398 st->st_cur->ste_varkeywords = 1;
1399 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 return 1;
1401}
1402
1403
1404static int
1405symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1406{
1407 if (eh->type)
1408 VISIT(st, expr, eh->type);
1409 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001410 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1411 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 VISIT_SEQ(st, stmt, eh->body);
1413 return 1;
1414}
1415
1416
1417static int
1418symtable_visit_alias(struct symtable *st, alias_ty a)
1419{
1420 /* Compute store_name, the name actually bound by the import
1421 operation. It is diferent than a->name when a->name is a
1422 dotted package name (e.g. spam.eggs)
1423 */
1424 PyObject *store_name;
1425 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1426 const char *base = PyString_AS_STRING(name);
1427 char *dot = strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001428 if (dot) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 store_name = PyString_FromStringAndSize(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001430 if (!store_name)
1431 return 0;
1432 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 else {
1434 store_name = name;
1435 Py_INCREF(store_name);
1436 }
1437 if (strcmp(PyString_AS_STRING(name), "*")) {
1438 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1439 Py_DECREF(store_name);
1440 return r;
1441 }
1442 else {
1443 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001444 int lineno = st->st_cur->ste_lineno;
1445 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001446 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001448 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 }
1450 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001451 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452 return 1;
1453 }
1454}
1455
1456
1457static int
1458symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1459{
1460 VISIT(st, expr, lc->target);
1461 VISIT(st, expr, lc->iter);
1462 VISIT_SEQ(st, expr, lc->ifs);
1463 return 1;
1464}
1465
1466
1467static int
1468symtable_visit_keyword(struct symtable *st, keyword_ty k)
1469{
1470 VISIT(st, expr, k->value);
1471 return 1;
1472}
1473
1474
1475static int
1476symtable_visit_slice(struct symtable *st, slice_ty s)
1477{
1478 switch (s->kind) {
1479 case Slice_kind:
1480 if (s->v.Slice.lower)
1481 VISIT(st, expr, s->v.Slice.lower)
1482 if (s->v.Slice.upper)
1483 VISIT(st, expr, s->v.Slice.upper)
1484 if (s->v.Slice.step)
1485 VISIT(st, expr, s->v.Slice.step)
1486 break;
1487 case ExtSlice_kind:
1488 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1489 break;
1490 case Index_kind:
1491 VISIT(st, expr, s->v.Index.value)
1492 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 }
1494 return 1;
1495}
1496
1497static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001498symtable_handle_comprehension(struct symtable *st, expr_ty e,
1499 identifier scope_name,
1500 asdl_seq *generators, expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001502 int is_generator = (e->kind == GeneratorExp_kind);
1503 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001505 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 /* Outermost iterator is evaluated in current scope */
1507 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001508 /* Create comprehension scope for the rest */
1509 if (!scope_name ||
1510 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 return 0;
1512 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001513 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514 /* Outermost iter is received as an argument */
1515 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001516 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517 return 0;
1518 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001519 /* Allocate temporary name if needed */
1520 if (needs_tmp && !symtable_new_tmpname(st)) {
1521 symtable_exit_block(st, (void *)e);
1522 return 0;
1523 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001524 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1525 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1526 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001527 generators, 1, (void*)e);
1528 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001529 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001531
1532static int
1533symtable_visit_genexp(struct symtable *st, expr_ty e)
1534{
1535 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1536 e->v.GeneratorExp.generators,
1537 e->v.GeneratorExp.elt);
1538}
1539
1540static int
1541symtable_visit_listcomp(struct symtable *st, expr_ty e)
1542{
1543 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1544 e->v.ListComp.generators,
1545 e->v.ListComp.elt);
1546}
1547
1548static int
1549symtable_visit_setcomp(struct symtable *st, expr_ty e)
1550{
1551 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1552 e->v.SetComp.generators,
1553 e->v.SetComp.elt);
1554}