blob: 121f3eeafe3841b5cd0870bbe0d27e4146cf1d9c [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{
Walter Dörwald5d4ede12007-06-11 16:03:16 +000091 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
92 ste->ste_name,
93 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000094}
95
96static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000098{
99 ste->ste_table = NULL;
100 Py_XDECREF(ste->ste_id);
101 Py_XDECREF(ste->ste_name);
102 Py_XDECREF(ste->ste_symbols);
103 Py_XDECREF(ste->ste_varnames);
104 Py_XDECREF(ste->ste_children);
105 PyObject_Del(ste);
106}
107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109
Guido van Rossum6f799372001-09-20 20:46:19 +0000110static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000111 {"id", T_OBJECT, OFF(ste_id), READONLY},
112 {"name", T_OBJECT, OFF(ste_name), READONLY},
113 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
114 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
115 {"children", T_OBJECT, OFF(ste_children), READONLY},
116 {"type", T_INT, OFF(ste_type), READONLY},
117 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000118 {NULL}
119};
120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121PyTypeObject PySTEntry_Type = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000122 PyObject_HEAD_INIT(&PyType_Type)
123 0,
124 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126 0,
127 (destructor)ste_dealloc, /* tp_dealloc */
128 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000129 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130 0, /* tp_setattr */
131 0, /* tp_compare */
132 (reprfunc)ste_repr, /* tp_repr */
133 0, /* tp_as_number */
134 0, /* tp_as_sequence */
135 0, /* tp_as_mapping */
136 0, /* tp_hash */
137 0, /* tp_call */
138 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000139 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000140 0, /* tp_setattro */
141 0, /* tp_as_buffer */
142 Py_TPFLAGS_DEFAULT, /* tp_flags */
143 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000144 0, /* tp_traverse */
145 0, /* tp_clear */
146 0, /* tp_richcompare */
147 0, /* tp_weaklistoffset */
148 0, /* tp_iter */
149 0, /* tp_iternext */
150 0, /* tp_methods */
151 ste_memberlist, /* tp_members */
152 0, /* tp_getset */
153 0, /* tp_base */
154 0, /* tp_dict */
155 0, /* tp_descr_get */
156 0, /* tp_descr_set */
157 0, /* tp_dictoffset */
158 0, /* tp_init */
159 0, /* tp_alloc */
160 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000161};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162
163static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000164static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000166 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_exit_block(struct symtable *st, void *ast);
168static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
169static int symtable_visit_expr(struct symtable *st, expr_ty s);
170static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000171static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
172static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000173static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static int symtable_visit_arguments(struct symtable *st, arguments_ty);
175static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
176static int symtable_visit_alias(struct symtable *st, alias_ty);
177static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
178static int symtable_visit_keyword(struct symtable *st, keyword_ty);
179static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000180static int symtable_visit_params(struct symtable *st, asdl_seq *args);
181static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000183static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184
185
Nick Coghlan650f0d02007-04-15 12:05:43 +0000186static identifier top = NULL, lambda = NULL, genexpr = NULL,
Guido van Rossum992d4a32007-07-11 13:09:30 +0000187 listcomp = NULL, setcomp = NULL, dictcomp = NULL, __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188
189#define GET_IDENTIFIER(VAR) \
Martin v. Löwis5b222132007-06-10 09:51:05 +0000190 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
192#define DUPLICATE_ARGUMENT \
193"duplicate argument '%s' in function definition"
194
195static struct symtable *
196symtable_new(void)
197{
198 struct symtable *st;
199
200 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
201 if (st == NULL)
202 return NULL;
203
204 st->st_filename = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000205 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000206
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 if ((st->st_stack = PyList_New(0)) == NULL)
208 goto fail;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000209 if ((st->st_blocks = PyDict_New()) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 goto fail;
211 st->st_cur = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212 st->st_private = NULL;
213 return st;
214 fail:
215 PySymtable_Free(st);
216 return NULL;
217}
218
219struct symtable *
220PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
221{
222 struct symtable *st = symtable_new();
223 asdl_seq *seq;
224 int i;
225
226 if (st == NULL)
227 return st;
228 st->st_filename = filename;
229 st->st_future = future;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000230 /* Make the initial symbol information gathering pass */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000231 if (!GET_IDENTIFIER(top) ||
232 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000233 PySymtable_Free(st);
234 return NULL;
235 }
236
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 st->st_top = st->st_cur;
238 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 switch (mod->kind) {
240 case Module_kind:
241 seq = mod->v.Module.body;
242 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243 if (!symtable_visit_stmt(st,
244 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245 goto error;
246 break;
247 case Expression_kind:
248 if (!symtable_visit_expr(st, mod->v.Expression.body))
249 goto error;
250 break;
251 case Interactive_kind:
252 seq = mod->v.Interactive.body;
253 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000254 if (!symtable_visit_stmt(st,
255 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256 goto error;
257 break;
258 case Suite_kind:
259 PyErr_SetString(PyExc_RuntimeError,
260 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000261 goto error;
262 }
263 if (!symtable_exit_block(st, (void *)mod)) {
264 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 return NULL;
266 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000267 /* Make the second symbol analysis pass */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 if (symtable_analyze(st))
269 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000270 PySymtable_Free(st);
271 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000273 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 PySymtable_Free(st);
275 return NULL;
276}
277
278void
279PySymtable_Free(struct symtable *st)
280{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000281 Py_XDECREF(st->st_blocks);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282 Py_XDECREF(st->st_stack);
283 PyMem_Free((void *)st);
284}
285
286PySTEntryObject *
287PySymtable_Lookup(struct symtable *st, void *key)
288{
289 PyObject *k, *v;
290
291 k = PyLong_FromVoidPtr(key);
292 if (k == NULL)
293 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000294 v = PyDict_GetItem(st->st_blocks, k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295 if (v) {
296 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298 }
299 else {
300 PyErr_SetString(PyExc_KeyError,
301 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000303
304 Py_DECREF(k);
305 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306}
307
308int
309PyST_GetScope(PySTEntryObject *ste, PyObject *name)
310{
311 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
312 if (!v)
313 return 0;
314 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000315 return (PyInt_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000316}
317
318
319/* Analyze raw symbol information to determine scope of each name.
320
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000321 The next several functions are helpers for symtable_analyze(),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322 which determines whether a name is local, global, or free. In addition,
323 it determines which local variables are cell variables; they provide
324 bindings that are used for free variables in enclosed blocks.
325
Nick Coghlan650f0d02007-04-15 12:05:43 +0000326 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327 explicit global is declared with the global statement. An implicit
328 global is a free variable for which the compiler has found no binding
329 in an enclosing function scope. The implicit global is either a global
330 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
331 to handle these names to implement slightly odd semantics. In such a
332 block, the name is treated as global until it is assigned to; then it
333 is treated as a local.
334
335 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000336 The first pass collects raw facts from the AST via the symtable_visit_*
337 functions: the name is a parameter here, the name is used but not defined
338 here, etc. The second pass analyzes these facts during a pass over the
339 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340
341 When a function is entered during the second pass, the parent passes
342 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000343 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000344 Names which are explicitly declared nonlocal must exist in this set of
345 visible names - if they do not, a syntax error is raised. After doing
346 the local analysis, it analyzes each of its child blocks using an
347 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348
Nick Coghlan650f0d02007-04-15 12:05:43 +0000349 The children update the free variable set. If a local variable is added to
350 the free variable set by the child, the variable is marked as a cell. The
351 function object being defined must provide runtime storage for the variable
352 that may outlive the function's frame. Cell variables are removed from the
353 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000354
Nick Coghlan650f0d02007-04-15 12:05:43 +0000355 During analysis, the names are:
356 symbols: dict mapping from symbol names to flag values (including offset scope values)
357 scopes: dict mapping from symbol names to scope values (no offset)
358 local: set of all symbol names local to the current scope
359 bound: set of all symbol names local to a containing function scope
360 free: set of all symbol names referenced but not bound in child scopes
361 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362*/
363
364#define SET_SCOPE(DICT, NAME, I) { \
365 PyObject *o = PyInt_FromLong(I); \
366 if (!o) \
367 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000368 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
369 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000371 } \
372 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373}
374
375/* Decide on scope of name, given flags.
376
377 The dicts passed in as arguments are modified as necessary.
378 ste is passed so that flags can be updated.
379*/
380
381static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000382analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 PyObject *bound, PyObject *local, PyObject *free,
384 PyObject *global)
385{
386 if (flags & DEF_GLOBAL) {
387 if (flags & DEF_PARAM) {
388 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000389 "name '%U' is parameter and global",
390 name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000391 return 0;
392 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000393 if (flags & DEF_NONLOCAL) {
394 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000395 "name '%U' is nonlocal and global",
396 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000397 return 0;
398 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000399 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
400 if (PySet_Add(global, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000401 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000402 if (bound && (PySet_Discard(bound, name) < 0))
403 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404 return 1;
405 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000406 if (flags & DEF_NONLOCAL) {
407 if (flags & DEF_PARAM) {
408 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000409 "name '%U' is parameter and nonlocal",
410 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000411 return 0;
412 }
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000413 if (!bound) {
414 PyErr_Format(PyExc_SyntaxError,
415 "nonlocal declaration not allowed at module level");
416 return 0;
417 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000418 if (!PySet_Contains(bound, name)) {
Jeremy Hylton81e95022007-02-27 06:50:52 +0000419 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000420 "no binding for nonlocal '%U' found",
421 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000422
423 return 0;
424 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000425 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000426 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000427 return PySet_Add(free, name) >= 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000428 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429 if (flags & DEF_BOUND) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000430 SET_SCOPE(scopes, name, LOCAL);
431 if (PySet_Add(local, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000433 if (PySet_Discard(global, name) < 0)
434 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435 return 1;
436 }
437 /* If an enclosing block has a binding for this name, it
438 is a free variable rather than a global variable.
439 Note that having a non-NULL bound implies that the block
440 is nested.
441 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000442 if (bound && PySet_Contains(bound, name)) {
443 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000445 return PySet_Add(free, name) >= 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446 }
447 /* If a parent has a global statement, then call it global
448 explicit? It could also be global implicit.
449 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000450 if (global && PySet_Contains(global, name)) {
451 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 return 1;
453 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000454 if (ste->ste_nested)
455 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000456 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000457 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458}
459
460#undef SET_SCOPE
461
462/* If a name is defined in free and also in locals, then this block
463 provides the binding for the free variable. The name should be
464 marked CELL in this block and removed from the free list.
465
466 Note that the current block's free variables are included in free.
467 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000468
469 The 'restrict' argument may be set to a string to restrict the analysis
470 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471*/
472
473static int
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000474analyze_cells(PyObject *scopes, PyObject *free, const char *restrict)
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;
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000491 if (restrict != NULL &&
492 PyUnicode_CompareWithASCIIString(name, restrict))
493 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494 /* Replace LOCAL with CELL for this name, and remove
495 from free. It is safe to replace the value of name
496 in the dict, because it will not cause a resize.
497 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000498 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000500 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501 goto error;
502 }
503 success = 1;
504 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000505 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 return success;
507}
508
509/* Check for illegal statements in unoptimized namespaces */
510static int
511check_unoptimized(const PySTEntryObject* ste) {
Armin Rigo31441302005-10-21 12:57:31 +0000512 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000514 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 || !(ste->ste_free || ste->ste_child_free))
516 return 1;
517
Armin Rigo31441302005-10-21 12:57:31 +0000518 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 "contains a nested function with free variables" :
520 "is a nested function");
521
522 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000523 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 return 1;
525 case OPT_IMPORT_STAR:
Walter Dörwald3ef72bb2007-06-11 16:12:10 +0000526 PyErr_Format(PyExc_SyntaxError,
527 "import * is not allowed in function '%U' because it %s",
Walter Dörwalde42109d2007-06-11 16:08:41 +0000528 ste->ste_name, trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530 }
531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532 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 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000599 /* It's a cell, or already free in this scope */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000600 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
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000685 /* Populate global and bound sets to be passed to children. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000687 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 if (ste->ste_type == FunctionBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000689 if (!PyNumber_InPlaceOr(newbound, local))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000691 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000693 /* Pass down previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694 if (bound) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000695 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000697 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000699 /* Pass down known globals */
700 if (!PyNumber_InPlaceOr(newglobal, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000702 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000704 else {
705 /* Special-case __class__ */
706 if (!GET_IDENTIFIER(__class__))
707 goto error;
708 assert(PySet_Contains(local, __class__) == 1);
709 if (PySet_Add(newbound, __class__) < 0)
710 goto error;
711 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712
713 /* Recursively call analyze_block() on each child block */
714 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
715 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000716 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000718 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719 if (!analyze_block(entry, newbound, newfree, newglobal))
720 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000721 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 if (entry->ste_free || entry->ste_child_free)
723 ste->ste_child_free = 1;
724 }
725
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000726 /* Check if any local variables must be converted to cell variables */
727 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
728 NULL))
729 goto error;
730 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
731 "__class__"))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000733 /* Records the results of the analysis in the symbol table entry */
734 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735 ste->ste_type == ClassBlock))
736 goto error;
737 if (!check_unoptimized(ste))
738 goto error;
739
Nick Coghlan650f0d02007-04-15 12:05:43 +0000740 if (!PyNumber_InPlaceOr(free, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000742 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743 success = 1;
744 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000745 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 Py_XDECREF(newbound);
748 Py_XDECREF(newglobal);
749 Py_XDECREF(newfree);
750 if (!success)
751 assert(PyErr_Occurred());
752 return success;
753}
754
755static int
756symtable_analyze(struct symtable *st)
757{
758 PyObject *free, *global;
759 int r;
760
Nick Coghlan650f0d02007-04-15 12:05:43 +0000761 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762 if (!free)
763 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000764 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000766 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 return 0;
768 }
769 r = analyze_block(st->st_top, NULL, free, global);
770 Py_DECREF(free);
771 Py_DECREF(global);
772 return r;
773}
774
775
776static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000777symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778{
779 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000780 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
782 PyErr_SetString(PyExc_SyntaxError, msg);
783 PyErr_SyntaxLocation(st->st_filename,
784 st->st_cur->ste_lineno);
785 }
786 return 0;
787 }
788 return 1;
789}
790
791/* symtable_enter_block() gets a reference via PySTEntry_New().
792 This reference is released when the block is exited, via the DECREF
793 in symtable_exit_block().
794*/
795
796static int
797symtable_exit_block(struct symtable *st, void *ast)
798{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000799 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000801 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 end = PyList_GET_SIZE(st->st_stack) - 1;
803 if (end >= 0) {
804 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
805 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000806 if (st->st_cur == NULL)
807 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 Py_INCREF(st->st_cur);
809 if (PySequence_DelItem(st->st_stack, end) < 0)
810 return 0;
811 }
812 return 1;
813}
814
815static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000816symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 void *ast, int lineno)
818{
819 PySTEntryObject *prev = NULL;
820
821 if (st->st_cur) {
822 prev = st->st_cur;
823 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 return 0;
825 }
826 Py_DECREF(st->st_cur);
827 }
828 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000829 if (st->st_cur == NULL)
830 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 if (name == GET_IDENTIFIER(top))
832 st->st_global = st->st_cur->ste_symbols;
833 if (prev) {
834 if (PyList_Append(prev->ste_children,
835 (PyObject *)st->st_cur) < 0) {
836 return 0;
837 }
838 }
839 return 1;
840}
841
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000842static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843symtable_lookup(struct symtable *st, PyObject *name)
844{
845 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000846 PyObject *mangled = _Py_Mangle(st->st_private, name);
847 if (!mangled)
848 return 0;
849 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
850 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 if (!o)
852 return 0;
853 return PyInt_AsLong(o);
854}
855
856static int
857symtable_add_def(struct symtable *st, PyObject *name, int flag)
858{
859 PyObject *o;
860 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000861 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000862 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863
Jeremy Hylton81e95022007-02-27 06:50:52 +0000864
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000865 if (!mangled)
866 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000868 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 val = PyInt_AS_LONG(o);
870 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000871 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
873 PyString_AsString(name));
874 PyErr_SyntaxLocation(st->st_filename,
875 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000876 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877 }
878 val |= flag;
879 } else
880 val = flag;
881 o = PyInt_FromLong(val);
882 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000883 goto error;
884 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000886 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 }
888 Py_DECREF(o);
889
890 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000891 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
892 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893 } else if (flag & DEF_GLOBAL) {
894 /* XXX need to update DEF_GLOBAL for other flags too;
895 perhaps only DEF_FREE_GLOBAL */
896 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000897 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898 val |= PyInt_AS_LONG(o);
899 }
900 o = PyInt_FromLong(val);
901 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000902 goto error;
903 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000905 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 }
907 Py_DECREF(o);
908 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000909 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000911
912error:
913 Py_DECREF(mangled);
914 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915}
916
917/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
918 They use the ASDL name to synthesize the name of the C type and the visit
919 function.
920
921 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
922 useful if the first node in the sequence requires special treatment.
923*/
924
925#define VISIT(ST, TYPE, V) \
926 if (!symtable_visit_ ## TYPE((ST), (V))) \
927 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000928
929#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
930 if (!symtable_visit_ ## TYPE((ST), (V))) { \
931 symtable_exit_block((ST), (S)); \
932 return 0; \
933 }
934
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935#define VISIT_SEQ(ST, TYPE, SEQ) { \
936 int i; \
937 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
938 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 if (!symtable_visit_ ## TYPE((ST), elt)) \
941 return 0; \
942 } \
943}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000944
945#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
946 int i; \
947 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
948 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000949 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000950 if (!symtable_visit_ ## TYPE((ST), elt)) { \
951 symtable_exit_block((ST), (S)); \
952 return 0; \
953 } \
954 } \
955}
956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
958 int i; \
959 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
960 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000961 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962 if (!symtable_visit_ ## TYPE((ST), elt)) \
963 return 0; \
964 } \
965}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000966
967#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
968 int i; \
969 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
970 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000971 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000972 if (!symtable_visit_ ## TYPE((ST), elt)) { \
973 symtable_exit_block((ST), (S)); \
974 return 0; \
975 } \
976 } \
977}
978
Guido van Rossum4f72a782006-10-27 23:31:49 +0000979#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
980 int i = 0; \
981 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
982 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
983 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
984 if (!elt) continue; /* can be NULL */ \
985 if (!symtable_visit_expr((ST), elt)) \
986 return 0; \
987 } \
988}
989
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000991symtable_new_tmpname(struct symtable *st)
992{
993 char tmpname[256];
994 identifier tmp;
995
996 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
997 ++st->st_cur->ste_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000998 tmp = PyUnicode_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000999 if (!tmp)
1000 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001001 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1002 return 0;
1003 Py_DECREF(tmp);
1004 return 1;
1005}
1006
Guido van Rossum4f72a782006-10-27 23:31:49 +00001007
1008
Guido van Rossumc2e20742006-02-27 22:32:47 +00001009static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010symtable_visit_stmt(struct symtable *st, stmt_ty s)
1011{
1012 switch (s->kind) {
1013 case FunctionDef_kind:
1014 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1015 return 0;
1016 if (s->v.FunctionDef.args->defaults)
1017 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001018 if (s->v.FunctionDef.args->kw_defaults)
1019 VISIT_KWONLYDEFAULTS(st,
1020 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001021 if (!symtable_visit_annotations(st, s))
1022 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001023 if (s->v.FunctionDef.decorator_list)
1024 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1026 FunctionBlock, (void *)s, s->lineno))
1027 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001028 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1029 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 if (!symtable_exit_block(st, s))
1031 return 0;
1032 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001033 case ClassDef_kind: {
1034 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1036 return 0;
1037 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001038 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1039 if (s->v.ClassDef.starargs)
1040 VISIT(st, expr, s->v.ClassDef.starargs);
1041 if (s->v.ClassDef.kwargs)
1042 VISIT(st, expr, s->v.ClassDef.kwargs);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001043 if (s->v.ClassDef.decorator_list)
1044 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1046 (void *)s, s->lineno))
1047 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001048 if (!GET_IDENTIFIER(__class__) ||
1049 !symtable_add_def(st, __class__, DEF_LOCAL)) {
1050 symtable_exit_block(st, s);
1051 return 0;
1052 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001053 tmp = st->st_private;
1054 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001055 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001056 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 if (!symtable_exit_block(st, s))
1058 return 0;
1059 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001060 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001062 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001064 st->st_cur->ste_returns_value = 1;
1065 if (st->st_cur->ste_generator) {
1066 PyErr_SetString(PyExc_SyntaxError,
1067 RETURN_VAL_IN_GENERATOR);
1068 PyErr_SyntaxLocation(st->st_filename,
1069 s->lineno);
1070 return 0;
1071 }
1072 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 break;
1074 case Delete_kind:
1075 VISIT_SEQ(st, expr, s->v.Delete.targets);
1076 break;
1077 case Assign_kind:
1078 VISIT_SEQ(st, expr, s->v.Assign.targets);
1079 VISIT(st, expr, s->v.Assign.value);
1080 break;
1081 case AugAssign_kind:
1082 VISIT(st, expr, s->v.AugAssign.target);
1083 VISIT(st, expr, s->v.AugAssign.value);
1084 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 case For_kind:
1086 VISIT(st, expr, s->v.For.target);
1087 VISIT(st, expr, s->v.For.iter);
1088 VISIT_SEQ(st, stmt, s->v.For.body);
1089 if (s->v.For.orelse)
1090 VISIT_SEQ(st, stmt, s->v.For.orelse);
1091 break;
1092 case While_kind:
1093 VISIT(st, expr, s->v.While.test);
1094 VISIT_SEQ(st, stmt, s->v.While.body);
1095 if (s->v.While.orelse)
1096 VISIT_SEQ(st, stmt, s->v.While.orelse);
1097 break;
1098 case If_kind:
1099 /* XXX if 0: and lookup_yield() hacks */
1100 VISIT(st, expr, s->v.If.test);
1101 VISIT_SEQ(st, stmt, s->v.If.body);
1102 if (s->v.If.orelse)
1103 VISIT_SEQ(st, stmt, s->v.If.orelse);
1104 break;
1105 case Raise_kind:
1106 if (s->v.Raise.type) {
1107 VISIT(st, expr, s->v.Raise.type);
1108 if (s->v.Raise.inst) {
1109 VISIT(st, expr, s->v.Raise.inst);
1110 if (s->v.Raise.tback)
1111 VISIT(st, expr, s->v.Raise.tback);
1112 }
1113 }
1114 break;
1115 case TryExcept_kind:
1116 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1117 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1118 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1119 break;
1120 case TryFinally_kind:
1121 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1122 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1123 break;
1124 case Assert_kind:
1125 VISIT(st, expr, s->v.Assert.test);
1126 if (s->v.Assert.msg)
1127 VISIT(st, expr, s->v.Assert.msg);
1128 break;
1129 case Import_kind:
1130 VISIT_SEQ(st, alias, s->v.Import.names);
1131 /* XXX Don't have the lineno available inside
1132 visit_alias */
1133 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1134 st->st_cur->ste_opt_lineno = s->lineno;
1135 break;
1136 case ImportFrom_kind:
1137 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1138 /* XXX Don't have the lineno available inside
1139 visit_alias */
1140 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1141 st->st_cur->ste_opt_lineno = s->lineno;
1142 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 case Global_kind: {
1144 int i;
1145 asdl_seq *seq = s->v.Global.names;
1146 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001147 identifier name = (identifier)asdl_seq_GET(seq, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001148 char *c_name = PyUnicode_AsString(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001149 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 if (cur < 0)
1151 return 0;
1152 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001153 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 if (cur & DEF_LOCAL)
1155 PyOS_snprintf(buf, sizeof(buf),
1156 GLOBAL_AFTER_ASSIGN,
1157 c_name);
1158 else
1159 PyOS_snprintf(buf, sizeof(buf),
1160 GLOBAL_AFTER_USE,
1161 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001162 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 return 0;
1164 }
1165 if (!symtable_add_def(st, name, DEF_GLOBAL))
1166 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 break;
1169 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001170 case Nonlocal_kind: {
1171 int i;
1172 asdl_seq *seq = s->v.Nonlocal.names;
1173 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1174 identifier name = (identifier)asdl_seq_GET(seq, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001175 char *c_name = PyUnicode_AsString(name);
Jeremy Hylton81e95022007-02-27 06:50:52 +00001176 long cur = symtable_lookup(st, name);
1177 if (cur < 0)
1178 return 0;
1179 if (cur & (DEF_LOCAL | USE)) {
1180 char buf[256];
1181 if (cur & DEF_LOCAL)
1182 PyOS_snprintf(buf, sizeof(buf),
1183 NONLOCAL_AFTER_ASSIGN,
1184 c_name);
1185 else
1186 PyOS_snprintf(buf, sizeof(buf),
1187 NONLOCAL_AFTER_USE,
1188 c_name);
1189 if (!symtable_warn(st, buf, s->lineno))
1190 return 0;
1191 }
1192 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1193 return 0;
1194 }
1195 break;
1196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 case Expr_kind:
1198 VISIT(st, expr, s->v.Expr.value);
1199 break;
1200 case Pass_kind:
1201 case Break_kind:
1202 case Continue_kind:
1203 /* nothing to do here */
1204 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001205 case With_kind:
1206 if (!symtable_new_tmpname(st))
1207 return 0;
1208 VISIT(st, expr, s->v.With.context_expr);
1209 if (s->v.With.optional_vars) {
1210 if (!symtable_new_tmpname(st))
1211 return 0;
1212 VISIT(st, expr, s->v.With.optional_vars);
1213 }
1214 VISIT_SEQ(st, stmt, s->v.With.body);
1215 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 }
1217 return 1;
1218}
1219
1220static int
1221symtable_visit_expr(struct symtable *st, expr_ty e)
1222{
1223 switch (e->kind) {
1224 case BoolOp_kind:
1225 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1226 break;
1227 case BinOp_kind:
1228 VISIT(st, expr, e->v.BinOp.left);
1229 VISIT(st, expr, e->v.BinOp.right);
1230 break;
1231 case UnaryOp_kind:
1232 VISIT(st, expr, e->v.UnaryOp.operand);
1233 break;
1234 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001235 if (!GET_IDENTIFIER(lambda) ||
1236 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237 return 0;
1238 if (e->v.Lambda.args->defaults)
1239 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1240 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001241 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 FunctionBlock, (void *)e, 0))
1243 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001244 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1245 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 if (!symtable_exit_block(st, (void *)e))
1247 return 0;
1248 break;
1249 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001250 case IfExp_kind:
1251 VISIT(st, expr, e->v.IfExp.test);
1252 VISIT(st, expr, e->v.IfExp.body);
1253 VISIT(st, expr, e->v.IfExp.orelse);
1254 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 case Dict_kind:
1256 VISIT_SEQ(st, expr, e->v.Dict.keys);
1257 VISIT_SEQ(st, expr, e->v.Dict.values);
1258 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001259 case Set_kind:
1260 VISIT_SEQ(st, expr, e->v.Set.elts);
1261 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001262 case GeneratorExp_kind:
1263 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001266 case ListComp_kind:
1267 if (!symtable_visit_listcomp(st, e))
1268 return 0;
1269 break;
1270 case SetComp_kind:
1271 if (!symtable_visit_setcomp(st, e))
1272 return 0;
1273 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001274 case DictComp_kind:
1275 if (!symtable_visit_dictcomp(st, e))
1276 return 0;
1277 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 case Yield_kind:
1279 if (e->v.Yield.value)
1280 VISIT(st, expr, e->v.Yield.value);
1281 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001282 if (st->st_cur->ste_returns_value) {
1283 PyErr_SetString(PyExc_SyntaxError,
1284 RETURN_VAL_IN_GENERATOR);
1285 PyErr_SyntaxLocation(st->st_filename,
1286 e->lineno);
1287 return 0;
1288 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 break;
1290 case Compare_kind:
1291 VISIT(st, expr, e->v.Compare.left);
1292 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1293 break;
1294 case Call_kind:
1295 VISIT(st, expr, e->v.Call.func);
1296 VISIT_SEQ(st, expr, e->v.Call.args);
1297 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1298 if (e->v.Call.starargs)
1299 VISIT(st, expr, e->v.Call.starargs);
1300 if (e->v.Call.kwargs)
1301 VISIT(st, expr, e->v.Call.kwargs);
1302 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 case Num_kind:
1304 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001305 case Bytes_kind:
1306 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 /* Nothing to do here. */
1308 break;
1309 /* The following exprs can be assignment targets. */
1310 case Attribute_kind:
1311 VISIT(st, expr, e->v.Attribute.value);
1312 break;
1313 case Subscript_kind:
1314 VISIT(st, expr, e->v.Subscript.value);
1315 VISIT(st, slice, e->v.Subscript.slice);
1316 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001317 case Starred_kind:
1318 VISIT(st, expr, e->v.Starred.value);
1319 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 case Name_kind:
1321 if (!symtable_add_def(st, e->v.Name.id,
1322 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1323 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001324 /* Special-case super: it counts as a use of __class__ */
1325 if (e->v.Name.ctx == Load &&
1326 st->st_cur->ste_type == FunctionBlock &&
1327 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1328 if (!GET_IDENTIFIER(__class__) ||
1329 !symtable_add_def(st, __class__, USE))
1330 return 0;
1331 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 break;
1333 /* child nodes of List and Tuple will have expr_context set */
1334 case List_kind:
1335 VISIT_SEQ(st, expr, e->v.List.elts);
1336 break;
1337 case Tuple_kind:
1338 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1339 break;
1340 }
1341 return 1;
1342}
1343
1344static int
1345symtable_implicit_arg(struct symtable *st, int pos)
1346{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001347 PyObject *id = PyUnicode_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348 if (id == NULL)
1349 return 0;
1350 if (!symtable_add_def(st, id, DEF_PARAM)) {
1351 Py_DECREF(id);
1352 return 0;
1353 }
1354 Py_DECREF(id);
1355 return 1;
1356}
1357
1358static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001359symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001361 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001362
1363 if (!args)
1364 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001367 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001368 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 return 0;
1370 }
1371
1372 return 1;
1373}
1374
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001375static int
1376symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377{
1378 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001379
1380 if (!args)
1381 return -1;
1382
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001384 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001385 if (arg->annotation)
1386 VISIT(st, expr, arg->annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001388
1389 return 1;
1390}
1391
Neal Norwitzc1505362006-12-28 06:47:50 +00001392static int
1393symtable_visit_annotations(struct symtable *st, stmt_ty s)
1394{
1395 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001397 if (a->args && !symtable_visit_argannotations(st, a->args))
Neal Norwitzc1505362006-12-28 06:47:50 +00001398 return 0;
1399 if (a->varargannotation)
1400 VISIT(st, expr, a->varargannotation);
1401 if (a->kwargannotation)
1402 VISIT(st, expr, a->kwargannotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001403 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
Neal Norwitzc1505362006-12-28 06:47:50 +00001404 return 0;
1405 if (s->v.FunctionDef.returns)
1406 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 return 1;
1408}
1409
1410static int
1411symtable_visit_arguments(struct symtable *st, arguments_ty a)
1412{
1413 /* skip default arguments inside function block
1414 XXX should ast be different?
1415 */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001416 if (a->args && !symtable_visit_params(st, a->args))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001418 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001419 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 if (a->vararg) {
1421 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1422 return 0;
1423 st->st_cur->ste_varargs = 1;
1424 }
1425 if (a->kwarg) {
1426 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1427 return 0;
1428 st->st_cur->ste_varkeywords = 1;
1429 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 return 1;
1431}
1432
1433
1434static int
1435symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1436{
1437 if (eh->type)
1438 VISIT(st, expr, eh->type);
1439 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001440 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1441 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 VISIT_SEQ(st, stmt, eh->body);
1443 return 1;
1444}
1445
1446
1447static int
1448symtable_visit_alias(struct symtable *st, alias_ty a)
1449{
1450 /* Compute store_name, the name actually bound by the import
1451 operation. It is diferent than a->name when a->name is a
1452 dotted package name (e.g. spam.eggs)
1453 */
1454 PyObject *store_name;
1455 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001456 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1457 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001458 if (dot) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001459 store_name = PyUnicode_FromUnicode(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001460 if (!store_name)
1461 return 0;
1462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 else {
1464 store_name = name;
1465 Py_INCREF(store_name);
1466 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001467 if (PyUnicode_CompareWithASCIIString(name, "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1469 Py_DECREF(store_name);
1470 return r;
1471 }
1472 else {
1473 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001474 int lineno = st->st_cur->ste_lineno;
1475 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001476 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001478 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 }
1480 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001481 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 return 1;
1483 }
1484}
1485
1486
1487static int
1488symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1489{
1490 VISIT(st, expr, lc->target);
1491 VISIT(st, expr, lc->iter);
1492 VISIT_SEQ(st, expr, lc->ifs);
1493 return 1;
1494}
1495
1496
1497static int
1498symtable_visit_keyword(struct symtable *st, keyword_ty k)
1499{
1500 VISIT(st, expr, k->value);
1501 return 1;
1502}
1503
1504
1505static int
1506symtable_visit_slice(struct symtable *st, slice_ty s)
1507{
1508 switch (s->kind) {
1509 case Slice_kind:
1510 if (s->v.Slice.lower)
1511 VISIT(st, expr, s->v.Slice.lower)
1512 if (s->v.Slice.upper)
1513 VISIT(st, expr, s->v.Slice.upper)
1514 if (s->v.Slice.step)
1515 VISIT(st, expr, s->v.Slice.step)
1516 break;
1517 case ExtSlice_kind:
1518 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1519 break;
1520 case Index_kind:
1521 VISIT(st, expr, s->v.Index.value)
1522 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523 }
1524 return 1;
1525}
1526
1527static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001528symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001529 identifier scope_name, asdl_seq *generators,
1530 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001532 int is_generator = (e->kind == GeneratorExp_kind);
1533 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001535 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 /* Outermost iterator is evaluated in current scope */
1537 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001538 /* Create comprehension scope for the rest */
1539 if (!scope_name ||
1540 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 return 0;
1542 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001543 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 /* Outermost iter is received as an argument */
1545 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001546 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 return 0;
1548 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001549 /* Allocate temporary name if needed */
1550 if (needs_tmp && !symtable_new_tmpname(st)) {
1551 symtable_exit_block(st, (void *)e);
1552 return 0;
1553 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001554 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1555 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1556 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001557 generators, 1, (void*)e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001558 if (value)
1559 VISIT_IN_BLOCK(st, expr, value, (void*)e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001560 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001561 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001563
1564static int
1565symtable_visit_genexp(struct symtable *st, expr_ty e)
1566{
1567 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1568 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001569 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001570}
1571
1572static int
1573symtable_visit_listcomp(struct symtable *st, expr_ty e)
1574{
1575 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1576 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001577 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001578}
1579
1580static int
1581symtable_visit_setcomp(struct symtable *st, expr_ty e)
1582{
1583 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1584 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001585 e->v.SetComp.elt, NULL);
1586}
1587
1588static int
1589symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1590{
1591 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1592 e->v.DictComp.generators,
1593 e->v.DictComp.key,
1594 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001595}