blob: 68c182133a941467d33fd18ec8edfd994d16b300 [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);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173static int symtable_visit_arguments(struct symtable *st, arguments_ty);
174static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
175static int symtable_visit_alias(struct symtable *st, alias_ty);
176static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
177static int symtable_visit_keyword(struct symtable *st, keyword_ty);
178static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000179static int symtable_visit_params(struct symtable *st, asdl_seq *args);
180static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000182static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183
184
Nick Coghlan650f0d02007-04-15 12:05:43 +0000185static identifier top = NULL, lambda = NULL, genexpr = NULL,
186 listcomp = NULL, setcomp = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
188#define GET_IDENTIFIER(VAR) \
Martin v. Löwis5b222132007-06-10 09:51:05 +0000189 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191#define DUPLICATE_ARGUMENT \
192"duplicate argument '%s' in function definition"
193
194static struct symtable *
195symtable_new(void)
196{
197 struct symtable *st;
198
199 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
200 if (st == NULL)
201 return NULL;
202
203 st->st_filename = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000204 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000205
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206 if ((st->st_stack = PyList_New(0)) == NULL)
207 goto fail;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000208 if ((st->st_blocks = PyDict_New()) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 goto fail;
210 st->st_cur = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211 st->st_private = NULL;
212 return st;
213 fail:
214 PySymtable_Free(st);
215 return NULL;
216}
217
218struct symtable *
219PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
220{
221 struct symtable *st = symtable_new();
222 asdl_seq *seq;
223 int i;
224
225 if (st == NULL)
226 return st;
227 st->st_filename = filename;
228 st->st_future = future;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000229 /* Make the initial symbol information gathering pass */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000230 if (!GET_IDENTIFIER(top) ||
231 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000232 PySymtable_Free(st);
233 return NULL;
234 }
235
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236 st->st_top = st->st_cur;
237 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 switch (mod->kind) {
239 case Module_kind:
240 seq = mod->v.Module.body;
241 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000242 if (!symtable_visit_stmt(st,
243 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 goto error;
245 break;
246 case Expression_kind:
247 if (!symtable_visit_expr(st, mod->v.Expression.body))
248 goto error;
249 break;
250 case Interactive_kind:
251 seq = mod->v.Interactive.body;
252 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000253 if (!symtable_visit_stmt(st,
254 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255 goto error;
256 break;
257 case Suite_kind:
258 PyErr_SetString(PyExc_RuntimeError,
259 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000260 goto error;
261 }
262 if (!symtable_exit_block(st, (void *)mod)) {
263 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 return NULL;
265 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000266 /* Make the second symbol analysis pass */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267 if (symtable_analyze(st))
268 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000269 PySymtable_Free(st);
270 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000272 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273 PySymtable_Free(st);
274 return NULL;
275}
276
277void
278PySymtable_Free(struct symtable *st)
279{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000280 Py_XDECREF(st->st_blocks);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281 Py_XDECREF(st->st_stack);
282 PyMem_Free((void *)st);
283}
284
285PySTEntryObject *
286PySymtable_Lookup(struct symtable *st, void *key)
287{
288 PyObject *k, *v;
289
290 k = PyLong_FromVoidPtr(key);
291 if (k == NULL)
292 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000293 v = PyDict_GetItem(st->st_blocks, k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294 if (v) {
295 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000296 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297 }
298 else {
299 PyErr_SetString(PyExc_KeyError,
300 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000302
303 Py_DECREF(k);
304 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305}
306
307int
308PyST_GetScope(PySTEntryObject *ste, PyObject *name)
309{
310 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
311 if (!v)
312 return 0;
313 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000314 return (PyInt_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315}
316
317
318/* Analyze raw symbol information to determine scope of each name.
319
320 The next several functions are helpers for PySymtable_Analyze(),
321 which determines whether a name is local, global, or free. In addition,
322 it determines which local variables are cell variables; they provide
323 bindings that are used for free variables in enclosed blocks.
324
Nick Coghlan650f0d02007-04-15 12:05:43 +0000325 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000326 explicit global is declared with the global statement. An implicit
327 global is a free variable for which the compiler has found no binding
328 in an enclosing function scope. The implicit global is either a global
329 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
330 to handle these names to implement slightly odd semantics. In such a
331 block, the name is treated as global until it is assigned to; then it
332 is treated as a local.
333
334 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000335 The first pass collects raw facts from the AST via the symtable_visit_*
336 functions: the name is a parameter here, the name is used but not defined
337 here, etc. The second pass analyzes these facts during a pass over the
338 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339
340 When a function is entered during the second pass, the parent passes
341 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000342 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000343 Names which are explicitly declared nonlocal must exist in this set of
344 visible names - if they do not, a syntax error is raised. After doing
345 the local analysis, it analyzes each of its child blocks using an
346 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
Nick Coghlan650f0d02007-04-15 12:05:43 +0000348 The children update the free variable set. If a local variable is added to
349 the free variable set by the child, the variable is marked as a cell. The
350 function object being defined must provide runtime storage for the variable
351 that may outlive the function's frame. Cell variables are removed from the
352 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000353
Nick Coghlan650f0d02007-04-15 12:05:43 +0000354 During analysis, the names are:
355 symbols: dict mapping from symbol names to flag values (including offset scope values)
356 scopes: dict mapping from symbol names to scope values (no offset)
357 local: set of all symbol names local to the current scope
358 bound: set of all symbol names local to a containing function scope
359 free: set of all symbol names referenced but not bound in child scopes
360 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361*/
362
363#define SET_SCOPE(DICT, NAME, I) { \
364 PyObject *o = PyInt_FromLong(I); \
365 if (!o) \
366 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000367 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
368 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000370 } \
371 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372}
373
374/* Decide on scope of name, given flags.
375
376 The dicts passed in as arguments are modified as necessary.
377 ste is passed so that flags can be updated.
378*/
379
380static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000381analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 PyObject *bound, PyObject *local, PyObject *free,
383 PyObject *global)
384{
385 if (flags & DEF_GLOBAL) {
386 if (flags & DEF_PARAM) {
387 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000388 "name '%U' is parameter and global",
389 name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390 return 0;
391 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000392 if (flags & DEF_NONLOCAL) {
393 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000394 "name '%U' is nonlocal and global",
395 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000396 return 0;
397 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000398 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
399 if (PySet_Add(global, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000401 if (bound && (PySet_Discard(bound, name) < 0))
402 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403 return 1;
404 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000405 if (flags & DEF_NONLOCAL) {
406 if (flags & DEF_PARAM) {
407 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000408 "name '%U' is parameter and nonlocal",
409 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000410 return 0;
411 }
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000412 if (!bound) {
413 PyErr_Format(PyExc_SyntaxError,
414 "nonlocal declaration not allowed at module level");
415 return 0;
416 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000417 if (!PySet_Contains(bound, name)) {
Jeremy Hylton81e95022007-02-27 06:50:52 +0000418 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000419 "no binding for nonlocal '%U' found",
420 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000421
422 return 0;
423 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000424 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000425 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000426 return PySet_Add(free, name) >= 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000427 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428 if (flags & DEF_BOUND) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000429 SET_SCOPE(scopes, name, LOCAL);
430 if (PySet_Add(local, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000432 if (PySet_Discard(global, name) < 0)
433 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434 return 1;
435 }
436 /* If an enclosing block has a binding for this name, it
437 is a free variable rather than a global variable.
438 Note that having a non-NULL bound implies that the block
439 is nested.
440 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000441 if (bound && PySet_Contains(bound, name)) {
442 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000443 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000444 return PySet_Add(free, name) >= 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445 }
446 /* If a parent has a global statement, then call it global
447 explicit? It could also be global implicit.
448 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000449 if (global && PySet_Contains(global, name)) {
450 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451 return 1;
452 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000453 if (ste->ste_nested)
454 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000455 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000456 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457}
458
459#undef SET_SCOPE
460
461/* If a name is defined in free and also in locals, then this block
462 provides the binding for the free variable. The name should be
463 marked CELL in this block and removed from the free list.
464
465 Note that the current block's free variables are included in free.
466 That's safe because no name can be free and local in the same scope.
467*/
468
469static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000470analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000472 PyObject *name, *v, *v_cell;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000473 int success = 0;
474 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475
Nick Coghlan650f0d02007-04-15 12:05:43 +0000476 v_cell = PyInt_FromLong(CELL);
477 if (!v_cell)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000479 while (PyDict_Next(scopes, &pos, &name, &v)) {
480 long scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000482 scope = PyInt_AS_LONG(v);
483 if (scope != LOCAL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484 continue;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000485 if (!PySet_Contains(free, name))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486 continue;
487 /* Replace LOCAL with CELL for this name, and remove
488 from free. It is safe to replace the value of name
489 in the dict, because it will not cause a resize.
490 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000491 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000493 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494 goto error;
495 }
496 success = 1;
497 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000498 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499 return success;
500}
501
502/* Check for illegal statements in unoptimized namespaces */
503static int
504check_unoptimized(const PySTEntryObject* ste) {
Armin Rigo31441302005-10-21 12:57:31 +0000505 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000507 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508 || !(ste->ste_free || ste->ste_child_free))
509 return 1;
510
Armin Rigo31441302005-10-21 12:57:31 +0000511 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 "contains a nested function with free variables" :
513 "is a nested function");
514
515 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000516 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517 return 1;
518 case OPT_IMPORT_STAR:
Walter Dörwald3ef72bb2007-06-11 16:12:10 +0000519 PyErr_Format(PyExc_SyntaxError,
520 "import * is not allowed in function '%U' because it %s",
Walter Dörwalde42109d2007-06-11 16:08:41 +0000521 ste->ste_name, trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 }
524
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525 PyErr_SyntaxLocation(ste->ste_table->st_filename,
526 ste->ste_opt_lineno);
527 return 0;
528}
529
Nick Coghlan650f0d02007-04-15 12:05:43 +0000530/* Enter the final scope information into the ste_symbols dict.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 *
532 * All arguments are dicts. Modifies symbols, others are read-only.
533*/
534static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000535update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000536 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000538 PyObject *name = NULL, *itr = NULL;
539 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000540 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
Nick Coghlan650f0d02007-04-15 12:05:43 +0000542 /* Update scope information for all symbols in this scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543 while (PyDict_Next(symbols, &pos, &name, &v)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000544 long scope, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 assert(PyInt_Check(v));
546 flags = PyInt_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000547 v_scope = PyDict_GetItem(scopes, name);
548 assert(v_scope && PyInt_Check(v_scope));
549 scope = PyInt_AS_LONG(v_scope);
550 flags |= (scope << SCOPE_OFFSET);
551 v_new = PyInt_FromLong(flags);
552 if (!v_new)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000553 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000554 if (PyDict_SetItem(symbols, name, v_new) < 0) {
555 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 return 0;
557 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000558 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559 }
560
Nick Coghlan650f0d02007-04-15 12:05:43 +0000561 /* Record not yet resolved free variables from children (if any) */
562 v_free = PyInt_FromLong(FREE << SCOPE_OFFSET);
563 if (!v_free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 return 0;
565
Nick Coghlan650f0d02007-04-15 12:05:43 +0000566 itr = PyObject_GetIter(free);
567 if (!itr)
568 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569
Nick Coghlan650f0d02007-04-15 12:05:43 +0000570 while ((name = PyIter_Next(itr))) {
571 v = PyDict_GetItem(symbols, name);
572
573 /* Handle symbol that already exists in this scope */
574 if (v) {
575 /* Handle a free variable in a method of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576 the class that has the same name as a local
577 or global in the class scope.
578 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 if (classflag &&
Nick Coghlan650f0d02007-04-15 12:05:43 +0000580 PyInt_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
581 long flags = PyInt_AS_LONG(v) | DEF_FREE_CLASS;
582 v_new = PyInt_FromLong(flags);
583 if (!v_new) {
584 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000586 if (PyDict_SetItem(symbols, name, v_new) < 0) {
587 Py_DECREF(v_new);
588 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000590 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000592 /* It's a cell, or already a free variable in this scope */
593 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594 continue;
595 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000596 /* Handle global symbol */
597 if (!PySet_Contains(bound, name)) {
598 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 continue; /* it's a global */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000601 /* Propagate new free symbol up the lexical stack */
602 if (PyDict_SetItem(symbols, name, v_free) < 0) {
603 goto error;
604 }
605 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000607 Py_DECREF(itr);
608 Py_DECREF(v_free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000610error:
611 Py_XDECREF(v_free);
612 Py_XDECREF(itr);
613 Py_XDECREF(name);
614 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615}
616
617/* Make final symbol table decisions for block of ste.
618 Arguments:
619 ste -- current symtable entry (input/output)
620 bound -- set of variables bound in enclosing scopes (input)
621 free -- set of free variables in enclosed scopes (output)
622 globals -- set of declared global variables in enclosing scopes (input)
623*/
624
625static int
626analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
627 PyObject *global)
628{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000629 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000631 int i, success = 0;
632 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633
Nick Coghlan650f0d02007-04-15 12:05:43 +0000634 scopes = PyDict_New();
635 if (!scopes)
636 goto error;
637 local = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 if (!local)
639 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000640 newglobal = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641 if (!newglobal)
642 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000643 newfree = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644 if (!newfree)
645 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000646 newbound = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 if (!newbound)
648 goto error;
649
Nick Coghlan650f0d02007-04-15 12:05:43 +0000650 /* Class namespace has no effect on names visible in
651 nested functions, so populate the global and bound
652 sets to be passed to child blocks before analyzing
653 this one.
654 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655 if (ste->ste_type == ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000656 /* Pass down previously bound symbols */
657 if (bound) {
658 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000660 Py_DECREF(newbound);
661 }
662 /* Pass down known globals */
663 if (!PyNumber_InPlaceOr(newglobal, global))
664 goto error;
665 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 }
667
Nick Coghlan650f0d02007-04-15 12:05:43 +0000668 /* Analyze symbols in current scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 assert(PySTEntry_Check(ste));
670 assert(PyDict_Check(ste->ste_symbols));
671 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000672 long flags = PyInt_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000673 if (!analyze_name(ste, scopes, name, flags, bound, local, free,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674 global))
675 goto error;
676 }
677
Nick Coghlan650f0d02007-04-15 12:05:43 +0000678 /* Populate global and bound sets to be passed to children.
679 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000681 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 if (ste->ste_type == FunctionBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000683 if (!PyNumber_InPlaceOr(newbound, local))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000685 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000687 /* Pass down previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 if (bound) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000689 if (!PyNumber_InPlaceOr(newbound, bound))
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 known globals */
694 if (!PyNumber_InPlaceOr(newglobal, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000696 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 }
698
699 /* Recursively call analyze_block() on each child block */
700 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
701 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000702 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000704 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705 if (!analyze_block(entry, newbound, newfree, newglobal))
706 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000707 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708 if (entry->ste_free || entry->ste_child_free)
709 ste->ste_child_free = 1;
710 }
711
Nick Coghlan650f0d02007-04-15 12:05:43 +0000712 /* Check if any local variables need to be converted to cell variables */
713 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000715 /* Records the results of the analysis in the symbol table entry */
716 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717 ste->ste_type == ClassBlock))
718 goto error;
719 if (!check_unoptimized(ste))
720 goto error;
721
Nick Coghlan650f0d02007-04-15 12:05:43 +0000722 if (!PyNumber_InPlaceOr(free, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000724 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725 success = 1;
726 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000727 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 Py_XDECREF(newbound);
730 Py_XDECREF(newglobal);
731 Py_XDECREF(newfree);
732 if (!success)
733 assert(PyErr_Occurred());
734 return success;
735}
736
737static int
738symtable_analyze(struct symtable *st)
739{
740 PyObject *free, *global;
741 int r;
742
Nick Coghlan650f0d02007-04-15 12:05:43 +0000743 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 if (!free)
745 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000746 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000748 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 return 0;
750 }
751 r = analyze_block(st->st_top, NULL, free, global);
752 Py_DECREF(free);
753 Py_DECREF(global);
754 return r;
755}
756
757
758static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000759symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760{
761 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000762 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
764 PyErr_SetString(PyExc_SyntaxError, msg);
765 PyErr_SyntaxLocation(st->st_filename,
766 st->st_cur->ste_lineno);
767 }
768 return 0;
769 }
770 return 1;
771}
772
773/* symtable_enter_block() gets a reference via PySTEntry_New().
774 This reference is released when the block is exited, via the DECREF
775 in symtable_exit_block().
776*/
777
778static int
779symtable_exit_block(struct symtable *st, void *ast)
780{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000781 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000783 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784 end = PyList_GET_SIZE(st->st_stack) - 1;
785 if (end >= 0) {
786 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
787 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000788 if (st->st_cur == NULL)
789 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 Py_INCREF(st->st_cur);
791 if (PySequence_DelItem(st->st_stack, end) < 0)
792 return 0;
793 }
794 return 1;
795}
796
797static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000798symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 void *ast, int lineno)
800{
801 PySTEntryObject *prev = NULL;
802
803 if (st->st_cur) {
804 prev = st->st_cur;
805 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806 return 0;
807 }
808 Py_DECREF(st->st_cur);
809 }
810 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000811 if (st->st_cur == NULL)
812 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 if (name == GET_IDENTIFIER(top))
814 st->st_global = st->st_cur->ste_symbols;
815 if (prev) {
816 if (PyList_Append(prev->ste_children,
817 (PyObject *)st->st_cur) < 0) {
818 return 0;
819 }
820 }
821 return 1;
822}
823
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000824static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825symtable_lookup(struct symtable *st, PyObject *name)
826{
827 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000828 PyObject *mangled = _Py_Mangle(st->st_private, name);
829 if (!mangled)
830 return 0;
831 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
832 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 if (!o)
834 return 0;
835 return PyInt_AsLong(o);
836}
837
838static int
839symtable_add_def(struct symtable *st, PyObject *name, int flag)
840{
841 PyObject *o;
842 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000843 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000844 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845
Jeremy Hylton81e95022007-02-27 06:50:52 +0000846
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000847 if (!mangled)
848 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000850 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 val = PyInt_AS_LONG(o);
852 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000853 /* Is it better to use 'mangled' or 'name' here? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
855 PyString_AsString(name));
856 PyErr_SyntaxLocation(st->st_filename,
857 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000858 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859 }
860 val |= flag;
861 } else
862 val = flag;
863 o = PyInt_FromLong(val);
864 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000865 goto error;
866 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000868 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869 }
870 Py_DECREF(o);
871
872 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000873 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
874 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 } else if (flag & DEF_GLOBAL) {
876 /* XXX need to update DEF_GLOBAL for other flags too;
877 perhaps only DEF_FREE_GLOBAL */
878 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000879 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 val |= PyInt_AS_LONG(o);
881 }
882 o = PyInt_FromLong(val);
883 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000884 goto error;
885 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000887 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 }
889 Py_DECREF(o);
890 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000891 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000893
894error:
895 Py_DECREF(mangled);
896 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897}
898
899/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
900 They use the ASDL name to synthesize the name of the C type and the visit
901 function.
902
903 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
904 useful if the first node in the sequence requires special treatment.
905*/
906
907#define VISIT(ST, TYPE, V) \
908 if (!symtable_visit_ ## TYPE((ST), (V))) \
909 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000910
911#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
912 if (!symtable_visit_ ## TYPE((ST), (V))) { \
913 symtable_exit_block((ST), (S)); \
914 return 0; \
915 }
916
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917#define VISIT_SEQ(ST, TYPE, SEQ) { \
918 int i; \
919 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
920 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 if (!symtable_visit_ ## TYPE((ST), elt)) \
923 return 0; \
924 } \
925}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000926
927#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
928 int i; \
929 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
930 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000931 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000932 if (!symtable_visit_ ## TYPE((ST), elt)) { \
933 symtable_exit_block((ST), (S)); \
934 return 0; \
935 } \
936 } \
937}
938
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
940 int i; \
941 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
942 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000943 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944 if (!symtable_visit_ ## TYPE((ST), elt)) \
945 return 0; \
946 } \
947}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000948
949#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
950 int i; \
951 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
952 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000953 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000954 if (!symtable_visit_ ## TYPE((ST), elt)) { \
955 symtable_exit_block((ST), (S)); \
956 return 0; \
957 } \
958 } \
959}
960
Guido van Rossum4f72a782006-10-27 23:31:49 +0000961#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
962 int i = 0; \
963 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
964 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
965 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
966 if (!elt) continue; /* can be NULL */ \
967 if (!symtable_visit_expr((ST), elt)) \
968 return 0; \
969 } \
970}
971
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000973symtable_new_tmpname(struct symtable *st)
974{
975 char tmpname[256];
976 identifier tmp;
977
978 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
979 ++st->st_cur->ste_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000980 tmp = PyUnicode_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000981 if (!tmp)
982 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000983 if (!symtable_add_def(st, tmp, DEF_LOCAL))
984 return 0;
985 Py_DECREF(tmp);
986 return 1;
987}
988
Guido van Rossum4f72a782006-10-27 23:31:49 +0000989
990
Guido van Rossumc2e20742006-02-27 22:32:47 +0000991static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992symtable_visit_stmt(struct symtable *st, stmt_ty s)
993{
994 switch (s->kind) {
995 case FunctionDef_kind:
996 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
997 return 0;
998 if (s->v.FunctionDef.args->defaults)
999 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001000 if (s->v.FunctionDef.args->kw_defaults)
1001 VISIT_KWONLYDEFAULTS(st,
1002 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001003 if (!symtable_visit_annotations(st, s))
1004 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001005 if (s->v.FunctionDef.decorator_list)
1006 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1008 FunctionBlock, (void *)s, s->lineno))
1009 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001010 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1011 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 if (!symtable_exit_block(st, s))
1013 return 0;
1014 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001015 case ClassDef_kind: {
1016 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1018 return 0;
1019 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001020 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1021 if (s->v.ClassDef.starargs)
1022 VISIT(st, expr, s->v.ClassDef.starargs);
1023 if (s->v.ClassDef.kwargs)
1024 VISIT(st, expr, s->v.ClassDef.kwargs);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001025 if (s->v.ClassDef.decorator_list)
1026 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1028 (void *)s, s->lineno))
1029 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001030 tmp = st->st_private;
1031 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001032 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001033 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 if (!symtable_exit_block(st, s))
1035 return 0;
1036 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001037 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001039 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001041 st->st_cur->ste_returns_value = 1;
1042 if (st->st_cur->ste_generator) {
1043 PyErr_SetString(PyExc_SyntaxError,
1044 RETURN_VAL_IN_GENERATOR);
1045 PyErr_SyntaxLocation(st->st_filename,
1046 s->lineno);
1047 return 0;
1048 }
1049 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050 break;
1051 case Delete_kind:
1052 VISIT_SEQ(st, expr, s->v.Delete.targets);
1053 break;
1054 case Assign_kind:
1055 VISIT_SEQ(st, expr, s->v.Assign.targets);
1056 VISIT(st, expr, s->v.Assign.value);
1057 break;
1058 case AugAssign_kind:
1059 VISIT(st, expr, s->v.AugAssign.target);
1060 VISIT(st, expr, s->v.AugAssign.value);
1061 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 case For_kind:
1063 VISIT(st, expr, s->v.For.target);
1064 VISIT(st, expr, s->v.For.iter);
1065 VISIT_SEQ(st, stmt, s->v.For.body);
1066 if (s->v.For.orelse)
1067 VISIT_SEQ(st, stmt, s->v.For.orelse);
1068 break;
1069 case While_kind:
1070 VISIT(st, expr, s->v.While.test);
1071 VISIT_SEQ(st, stmt, s->v.While.body);
1072 if (s->v.While.orelse)
1073 VISIT_SEQ(st, stmt, s->v.While.orelse);
1074 break;
1075 case If_kind:
1076 /* XXX if 0: and lookup_yield() hacks */
1077 VISIT(st, expr, s->v.If.test);
1078 VISIT_SEQ(st, stmt, s->v.If.body);
1079 if (s->v.If.orelse)
1080 VISIT_SEQ(st, stmt, s->v.If.orelse);
1081 break;
1082 case Raise_kind:
1083 if (s->v.Raise.type) {
1084 VISIT(st, expr, s->v.Raise.type);
1085 if (s->v.Raise.inst) {
1086 VISIT(st, expr, s->v.Raise.inst);
1087 if (s->v.Raise.tback)
1088 VISIT(st, expr, s->v.Raise.tback);
1089 }
1090 }
1091 break;
1092 case TryExcept_kind:
1093 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1094 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1095 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1096 break;
1097 case TryFinally_kind:
1098 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1099 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1100 break;
1101 case Assert_kind:
1102 VISIT(st, expr, s->v.Assert.test);
1103 if (s->v.Assert.msg)
1104 VISIT(st, expr, s->v.Assert.msg);
1105 break;
1106 case Import_kind:
1107 VISIT_SEQ(st, alias, s->v.Import.names);
1108 /* XXX Don't have the lineno available inside
1109 visit_alias */
1110 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1111 st->st_cur->ste_opt_lineno = s->lineno;
1112 break;
1113 case ImportFrom_kind:
1114 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1115 /* XXX Don't have the lineno available inside
1116 visit_alias */
1117 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1118 st->st_cur->ste_opt_lineno = s->lineno;
1119 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 case Global_kind: {
1121 int i;
1122 asdl_seq *seq = s->v.Global.names;
1123 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001124 identifier name = (identifier)asdl_seq_GET(seq, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001125 char *c_name = PyUnicode_AsString(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001126 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 if (cur < 0)
1128 return 0;
1129 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001130 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 if (cur & DEF_LOCAL)
1132 PyOS_snprintf(buf, sizeof(buf),
1133 GLOBAL_AFTER_ASSIGN,
1134 c_name);
1135 else
1136 PyOS_snprintf(buf, sizeof(buf),
1137 GLOBAL_AFTER_USE,
1138 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001139 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 return 0;
1141 }
1142 if (!symtable_add_def(st, name, DEF_GLOBAL))
1143 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 break;
1146 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001147 case Nonlocal_kind: {
1148 int i;
1149 asdl_seq *seq = s->v.Nonlocal.names;
1150 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1151 identifier name = (identifier)asdl_seq_GET(seq, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001152 char *c_name = PyUnicode_AsString(name);
Jeremy Hylton81e95022007-02-27 06:50:52 +00001153 long cur = symtable_lookup(st, name);
1154 if (cur < 0)
1155 return 0;
1156 if (cur & (DEF_LOCAL | USE)) {
1157 char buf[256];
1158 if (cur & DEF_LOCAL)
1159 PyOS_snprintf(buf, sizeof(buf),
1160 NONLOCAL_AFTER_ASSIGN,
1161 c_name);
1162 else
1163 PyOS_snprintf(buf, sizeof(buf),
1164 NONLOCAL_AFTER_USE,
1165 c_name);
1166 if (!symtable_warn(st, buf, s->lineno))
1167 return 0;
1168 }
1169 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1170 return 0;
1171 }
1172 break;
1173 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 case Expr_kind:
1175 VISIT(st, expr, s->v.Expr.value);
1176 break;
1177 case Pass_kind:
1178 case Break_kind:
1179 case Continue_kind:
1180 /* nothing to do here */
1181 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001182 case With_kind:
1183 if (!symtable_new_tmpname(st))
1184 return 0;
1185 VISIT(st, expr, s->v.With.context_expr);
1186 if (s->v.With.optional_vars) {
1187 if (!symtable_new_tmpname(st))
1188 return 0;
1189 VISIT(st, expr, s->v.With.optional_vars);
1190 }
1191 VISIT_SEQ(st, stmt, s->v.With.body);
1192 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 }
1194 return 1;
1195}
1196
1197static int
1198symtable_visit_expr(struct symtable *st, expr_ty e)
1199{
1200 switch (e->kind) {
1201 case BoolOp_kind:
1202 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1203 break;
1204 case BinOp_kind:
1205 VISIT(st, expr, e->v.BinOp.left);
1206 VISIT(st, expr, e->v.BinOp.right);
1207 break;
1208 case UnaryOp_kind:
1209 VISIT(st, expr, e->v.UnaryOp.operand);
1210 break;
1211 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001212 if (!GET_IDENTIFIER(lambda) ||
1213 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 return 0;
1215 if (e->v.Lambda.args->defaults)
1216 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1217 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001218 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 FunctionBlock, (void *)e, 0))
1220 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001221 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1222 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 if (!symtable_exit_block(st, (void *)e))
1224 return 0;
1225 break;
1226 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001227 case IfExp_kind:
1228 VISIT(st, expr, e->v.IfExp.test);
1229 VISIT(st, expr, e->v.IfExp.body);
1230 VISIT(st, expr, e->v.IfExp.orelse);
1231 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 case Dict_kind:
1233 VISIT_SEQ(st, expr, e->v.Dict.keys);
1234 VISIT_SEQ(st, expr, e->v.Dict.values);
1235 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001236 case Set_kind:
1237 VISIT_SEQ(st, expr, e->v.Set.elts);
1238 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001239 case GeneratorExp_kind:
1240 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001243 case ListComp_kind:
1244 if (!symtable_visit_listcomp(st, e))
1245 return 0;
1246 break;
1247 case SetComp_kind:
1248 if (!symtable_visit_setcomp(st, e))
1249 return 0;
1250 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 case Yield_kind:
1252 if (e->v.Yield.value)
1253 VISIT(st, expr, e->v.Yield.value);
1254 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001255 if (st->st_cur->ste_returns_value) {
1256 PyErr_SetString(PyExc_SyntaxError,
1257 RETURN_VAL_IN_GENERATOR);
1258 PyErr_SyntaxLocation(st->st_filename,
1259 e->lineno);
1260 return 0;
1261 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 break;
1263 case Compare_kind:
1264 VISIT(st, expr, e->v.Compare.left);
1265 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1266 break;
1267 case Call_kind:
1268 VISIT(st, expr, e->v.Call.func);
1269 VISIT_SEQ(st, expr, e->v.Call.args);
1270 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1271 if (e->v.Call.starargs)
1272 VISIT(st, expr, e->v.Call.starargs);
1273 if (e->v.Call.kwargs)
1274 VISIT(st, expr, e->v.Call.kwargs);
1275 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 case Num_kind:
1277 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001278 case Bytes_kind:
1279 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 /* Nothing to do here. */
1281 break;
1282 /* The following exprs can be assignment targets. */
1283 case Attribute_kind:
1284 VISIT(st, expr, e->v.Attribute.value);
1285 break;
1286 case Subscript_kind:
1287 VISIT(st, expr, e->v.Subscript.value);
1288 VISIT(st, slice, e->v.Subscript.slice);
1289 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001290 case Starred_kind:
1291 VISIT(st, expr, e->v.Starred.value);
1292 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293 case Name_kind:
1294 if (!symtable_add_def(st, e->v.Name.id,
1295 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1296 return 0;
1297 break;
1298 /* child nodes of List and Tuple will have expr_context set */
1299 case List_kind:
1300 VISIT_SEQ(st, expr, e->v.List.elts);
1301 break;
1302 case Tuple_kind:
1303 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1304 break;
1305 }
1306 return 1;
1307}
1308
1309static int
1310symtable_implicit_arg(struct symtable *st, int pos)
1311{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001312 PyObject *id = PyUnicode_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 if (id == NULL)
1314 return 0;
1315 if (!symtable_add_def(st, id, DEF_PARAM)) {
1316 Py_DECREF(id);
1317 return 0;
1318 }
1319 Py_DECREF(id);
1320 return 1;
1321}
1322
1323static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001324symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001326 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001327
1328 if (!args)
1329 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001332 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001333 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 return 0;
1335 }
1336
1337 return 1;
1338}
1339
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001340static int
1341symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342{
1343 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001344
1345 if (!args)
1346 return -1;
1347
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001349 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001350 if (arg->annotation)
1351 VISIT(st, expr, arg->annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001353
1354 return 1;
1355}
1356
Neal Norwitzc1505362006-12-28 06:47:50 +00001357static int
1358symtable_visit_annotations(struct symtable *st, stmt_ty s)
1359{
1360 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001362 if (a->args && !symtable_visit_argannotations(st, a->args))
Neal Norwitzc1505362006-12-28 06:47:50 +00001363 return 0;
1364 if (a->varargannotation)
1365 VISIT(st, expr, a->varargannotation);
1366 if (a->kwargannotation)
1367 VISIT(st, expr, a->kwargannotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001368 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
Neal Norwitzc1505362006-12-28 06:47:50 +00001369 return 0;
1370 if (s->v.FunctionDef.returns)
1371 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 return 1;
1373}
1374
1375static int
1376symtable_visit_arguments(struct symtable *st, arguments_ty a)
1377{
1378 /* skip default arguments inside function block
1379 XXX should ast be different?
1380 */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001381 if (a->args && !symtable_visit_params(st, a->args))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382 return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001383 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001384 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 if (a->vararg) {
1386 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1387 return 0;
1388 st->st_cur->ste_varargs = 1;
1389 }
1390 if (a->kwarg) {
1391 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1392 return 0;
1393 st->st_cur->ste_varkeywords = 1;
1394 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 return 1;
1396}
1397
1398
1399static int
1400symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1401{
1402 if (eh->type)
1403 VISIT(st, expr, eh->type);
1404 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001405 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1406 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 VISIT_SEQ(st, stmt, eh->body);
1408 return 1;
1409}
1410
1411
1412static int
1413symtable_visit_alias(struct symtable *st, alias_ty a)
1414{
1415 /* Compute store_name, the name actually bound by the import
1416 operation. It is diferent than a->name when a->name is a
1417 dotted package name (e.g. spam.eggs)
1418 */
1419 PyObject *store_name;
1420 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001421 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1422 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001423 if (dot) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001424 store_name = PyUnicode_FromUnicode(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001425 if (!store_name)
1426 return 0;
1427 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 else {
1429 store_name = name;
1430 Py_INCREF(store_name);
1431 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001432 if (PyUnicode_CompareWithASCIIString(name, "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1434 Py_DECREF(store_name);
1435 return r;
1436 }
1437 else {
1438 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001439 int lineno = st->st_cur->ste_lineno;
1440 if (!symtable_warn(st, IMPORT_STAR_WARNING, lineno)) {
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001441 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 return 0;
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001443 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 }
1445 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001446 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 return 1;
1448 }
1449}
1450
1451
1452static int
1453symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1454{
1455 VISIT(st, expr, lc->target);
1456 VISIT(st, expr, lc->iter);
1457 VISIT_SEQ(st, expr, lc->ifs);
1458 return 1;
1459}
1460
1461
1462static int
1463symtable_visit_keyword(struct symtable *st, keyword_ty k)
1464{
1465 VISIT(st, expr, k->value);
1466 return 1;
1467}
1468
1469
1470static int
1471symtable_visit_slice(struct symtable *st, slice_ty s)
1472{
1473 switch (s->kind) {
1474 case Slice_kind:
1475 if (s->v.Slice.lower)
1476 VISIT(st, expr, s->v.Slice.lower)
1477 if (s->v.Slice.upper)
1478 VISIT(st, expr, s->v.Slice.upper)
1479 if (s->v.Slice.step)
1480 VISIT(st, expr, s->v.Slice.step)
1481 break;
1482 case ExtSlice_kind:
1483 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1484 break;
1485 case Index_kind:
1486 VISIT(st, expr, s->v.Index.value)
1487 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488 }
1489 return 1;
1490}
1491
1492static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001493symtable_handle_comprehension(struct symtable *st, expr_ty e,
1494 identifier scope_name,
1495 asdl_seq *generators, expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001497 int is_generator = (e->kind == GeneratorExp_kind);
1498 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001500 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 /* Outermost iterator is evaluated in current scope */
1502 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001503 /* Create comprehension scope for the rest */
1504 if (!scope_name ||
1505 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 return 0;
1507 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001508 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 /* Outermost iter is received as an argument */
1510 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001511 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 return 0;
1513 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001514 /* Allocate temporary name if needed */
1515 if (needs_tmp && !symtable_new_tmpname(st)) {
1516 symtable_exit_block(st, (void *)e);
1517 return 0;
1518 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001519 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1520 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1521 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001522 generators, 1, (void*)e);
1523 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001524 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001526
1527static int
1528symtable_visit_genexp(struct symtable *st, expr_ty e)
1529{
1530 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1531 e->v.GeneratorExp.generators,
1532 e->v.GeneratorExp.elt);
1533}
1534
1535static int
1536symtable_visit_listcomp(struct symtable *st, expr_ty e)
1537{
1538 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1539 e->v.ListComp.generators,
1540 e->v.ListComp.elt);
1541}
1542
1543static int
1544symtable_visit_setcomp(struct symtable *st, expr_ty e)
1545{
1546 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1547 e->v.SetComp.generators,
1548 e->v.SetComp.elt);
1549}