blob: 968fe2522ea58844010970096880ecb907c80d78 [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 = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000122 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000123 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000125 0,
126 (destructor)ste_dealloc, /* tp_dealloc */
127 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000128 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000129 0, /* tp_setattr */
130 0, /* tp_compare */
131 (reprfunc)ste_repr, /* tp_repr */
132 0, /* tp_as_number */
133 0, /* tp_as_sequence */
134 0, /* tp_as_mapping */
135 0, /* tp_hash */
136 0, /* tp_call */
137 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000138 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000139 0, /* tp_setattro */
140 0, /* tp_as_buffer */
141 Py_TPFLAGS_DEFAULT, /* tp_flags */
142 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000143 0, /* tp_traverse */
144 0, /* tp_clear */
145 0, /* tp_richcompare */
146 0, /* tp_weaklistoffset */
147 0, /* tp_iter */
148 0, /* tp_iternext */
149 0, /* tp_methods */
150 ste_memberlist, /* tp_members */
151 0, /* tp_getset */
152 0, /* tp_base */
153 0, /* tp_dict */
154 0, /* tp_descr_get */
155 0, /* tp_descr_set */
156 0, /* tp_dictoffset */
157 0, /* tp_init */
158 0, /* tp_alloc */
159 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000160};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161
162static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000163static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000165 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166static int symtable_exit_block(struct symtable *st, void *ast);
167static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
168static int symtable_visit_expr(struct symtable *st, expr_ty s);
169static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000170static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
171static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000172static int symtable_visit_dictcomp(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,
Guido van Rossum992d4a32007-07-11 13:09:30 +0000186 listcomp = NULL, setcomp = NULL, dictcomp = NULL, __class__ = 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 \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000192"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
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
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000320 The next several functions are helpers for symtable_analyze(),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 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.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000467
Martin v. Löwis2673a572007-10-29 19:54:24 +0000468 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000469 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470*/
471
472static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000473analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000475 PyObject *name, *v, *v_cell;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000476 int success = 0;
477 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478
Nick Coghlan650f0d02007-04-15 12:05:43 +0000479 v_cell = PyInt_FromLong(CELL);
480 if (!v_cell)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000482 while (PyDict_Next(scopes, &pos, &name, &v)) {
483 long scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000485 scope = PyInt_AS_LONG(v);
486 if (scope != LOCAL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 continue;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000488 if (!PySet_Contains(free, name))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489 continue;
Martin v. Löwis2673a572007-10-29 19:54:24 +0000490 if (restricted != NULL &&
491 PyUnicode_CompareWithASCIIString(name, restricted))
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000492 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 /* Replace LOCAL with CELL for this name, and remove
494 from free. It is safe to replace the value of name
495 in the dict, because it will not cause a resize.
496 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000497 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000499 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 goto error;
501 }
502 success = 1;
503 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000504 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000505 return success;
506}
507
508/* Check for illegal statements in unoptimized namespaces */
509static int
510check_unoptimized(const PySTEntryObject* ste) {
Armin Rigo31441302005-10-21 12:57:31 +0000511 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000513 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 || !(ste->ste_free || ste->ste_child_free))
515 return 1;
516
Armin Rigo31441302005-10-21 12:57:31 +0000517 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518 "contains a nested function with free variables" :
519 "is a nested function");
520
521 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000522 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 return 1;
524 case OPT_IMPORT_STAR:
Walter Dörwald3ef72bb2007-06-11 16:12:10 +0000525 PyErr_Format(PyExc_SyntaxError,
526 "import * is not allowed in function '%U' because it %s",
Walter Dörwalde42109d2007-06-11 16:08:41 +0000527 ste->ste_name, trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 }
530
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 PyErr_SyntaxLocation(ste->ste_table->st_filename,
532 ste->ste_opt_lineno);
533 return 0;
534}
535
Nick Coghlan650f0d02007-04-15 12:05:43 +0000536/* Enter the final scope information into the ste_symbols dict.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 *
538 * All arguments are dicts. Modifies symbols, others are read-only.
539*/
540static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000541update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000544 PyObject *name = NULL, *itr = NULL;
545 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000546 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
Nick Coghlan650f0d02007-04-15 12:05:43 +0000548 /* Update scope information for all symbols in this scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549 while (PyDict_Next(symbols, &pos, &name, &v)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000550 long scope, flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551 assert(PyInt_Check(v));
552 flags = PyInt_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000553 v_scope = PyDict_GetItem(scopes, name);
554 assert(v_scope && PyInt_Check(v_scope));
555 scope = PyInt_AS_LONG(v_scope);
556 flags |= (scope << SCOPE_OFFSET);
557 v_new = PyInt_FromLong(flags);
558 if (!v_new)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000559 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000560 if (PyDict_SetItem(symbols, name, v_new) < 0) {
561 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562 return 0;
563 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000564 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565 }
566
Nick Coghlan650f0d02007-04-15 12:05:43 +0000567 /* Record not yet resolved free variables from children (if any) */
568 v_free = PyInt_FromLong(FREE << SCOPE_OFFSET);
569 if (!v_free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 return 0;
571
Nick Coghlan650f0d02007-04-15 12:05:43 +0000572 itr = PyObject_GetIter(free);
573 if (!itr)
574 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575
Nick Coghlan650f0d02007-04-15 12:05:43 +0000576 while ((name = PyIter_Next(itr))) {
577 v = PyDict_GetItem(symbols, name);
578
579 /* Handle symbol that already exists in this scope */
580 if (v) {
581 /* Handle a free variable in a method of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582 the class that has the same name as a local
583 or global in the class scope.
584 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000585 if (classflag &&
Nick Coghlan650f0d02007-04-15 12:05:43 +0000586 PyInt_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
587 long flags = PyInt_AS_LONG(v) | DEF_FREE_CLASS;
588 v_new = PyInt_FromLong(flags);
589 if (!v_new) {
590 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000592 if (PyDict_SetItem(symbols, name, v_new) < 0) {
593 Py_DECREF(v_new);
594 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000596 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000598 /* It's a cell, or already free in this scope */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000599 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 continue;
601 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000602 /* Handle global symbol */
603 if (!PySet_Contains(bound, name)) {
604 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 continue; /* it's a global */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000607 /* Propagate new free symbol up the lexical stack */
608 if (PyDict_SetItem(symbols, name, v_free) < 0) {
609 goto error;
610 }
611 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000613 Py_DECREF(itr);
614 Py_DECREF(v_free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000616error:
617 Py_XDECREF(v_free);
618 Py_XDECREF(itr);
619 Py_XDECREF(name);
620 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621}
622
623/* Make final symbol table decisions for block of ste.
624 Arguments:
625 ste -- current symtable entry (input/output)
626 bound -- set of variables bound in enclosing scopes (input)
627 free -- set of free variables in enclosed scopes (output)
628 globals -- set of declared global variables in enclosing scopes (input)
629*/
630
631static int
632analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
633 PyObject *global)
634{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000635 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000637 int i, success = 0;
638 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639
Nick Coghlan650f0d02007-04-15 12:05:43 +0000640 scopes = PyDict_New();
641 if (!scopes)
642 goto error;
643 local = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644 if (!local)
645 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000646 newglobal = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 if (!newglobal)
648 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000649 newfree = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 if (!newfree)
651 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000652 newbound = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653 if (!newbound)
654 goto error;
655
Nick Coghlan650f0d02007-04-15 12:05:43 +0000656 /* Class namespace has no effect on names visible in
657 nested functions, so populate the global and bound
658 sets to be passed to child blocks before analyzing
659 this one.
660 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 if (ste->ste_type == ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000662 /* Pass down previously bound symbols */
663 if (bound) {
664 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000666 Py_DECREF(newbound);
667 }
668 /* Pass down known globals */
669 if (!PyNumber_InPlaceOr(newglobal, global))
670 goto error;
671 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 }
673
Nick Coghlan650f0d02007-04-15 12:05:43 +0000674 /* Analyze symbols in current scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 assert(PySTEntry_Check(ste));
676 assert(PyDict_Check(ste->ste_symbols));
677 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000678 long flags = PyInt_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000679 if (!analyze_name(ste, scopes, name, flags, bound, local, free,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 global))
681 goto error;
682 }
683
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000684 /* Populate global and bound sets to be passed to children. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000686 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 if (ste->ste_type == FunctionBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000688 if (!PyNumber_InPlaceOr(newbound, local))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000690 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000692 /* Pass down previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 if (bound) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000694 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000696 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000698 /* Pass down known globals */
699 if (!PyNumber_InPlaceOr(newglobal, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000701 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000703 else {
704 /* Special-case __class__ */
705 if (!GET_IDENTIFIER(__class__))
706 goto error;
707 assert(PySet_Contains(local, __class__) == 1);
708 if (PySet_Add(newbound, __class__) < 0)
709 goto error;
710 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711
712 /* Recursively call analyze_block() on each child block */
713 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
714 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000715 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000717 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 if (!analyze_block(entry, newbound, newfree, newglobal))
719 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000720 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721 if (entry->ste_free || entry->ste_child_free)
722 ste->ste_child_free = 1;
723 }
724
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000725 /* Check if any local variables must be converted to cell variables */
726 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
727 NULL))
728 goto error;
729 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
730 "__class__"))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000732 /* Records the results of the analysis in the symbol table entry */
733 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734 ste->ste_type == ClassBlock))
735 goto error;
736 if (!check_unoptimized(ste))
737 goto error;
738
Nick Coghlan650f0d02007-04-15 12:05:43 +0000739 if (!PyNumber_InPlaceOr(free, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000741 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742 success = 1;
743 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000744 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 Py_XDECREF(newbound);
747 Py_XDECREF(newglobal);
748 Py_XDECREF(newfree);
749 if (!success)
750 assert(PyErr_Occurred());
751 return success;
752}
753
754static int
755symtable_analyze(struct symtable *st)
756{
757 PyObject *free, *global;
758 int r;
759
Nick Coghlan650f0d02007-04-15 12:05:43 +0000760 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 if (!free)
762 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000763 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000765 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766 return 0;
767 }
768 r = analyze_block(st->st_top, NULL, free, global);
769 Py_DECREF(free);
770 Py_DECREF(global);
771 return r;
772}
773
774
775static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000776symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777{
778 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000779 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
781 PyErr_SetString(PyExc_SyntaxError, msg);
782 PyErr_SyntaxLocation(st->st_filename,
783 st->st_cur->ste_lineno);
784 }
785 return 0;
786 }
787 return 1;
788}
789
790/* symtable_enter_block() gets a reference via PySTEntry_New().
791 This reference is released when the block is exited, via the DECREF
792 in symtable_exit_block().
793*/
794
795static int
796symtable_exit_block(struct symtable *st, void *ast)
797{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000798 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000800 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 end = PyList_GET_SIZE(st->st_stack) - 1;
802 if (end >= 0) {
803 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
804 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000805 if (st->st_cur == NULL)
806 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 Py_INCREF(st->st_cur);
808 if (PySequence_DelItem(st->st_stack, end) < 0)
809 return 0;
810 }
811 return 1;
812}
813
814static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000815symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 void *ast, int lineno)
817{
818 PySTEntryObject *prev = NULL;
819
820 if (st->st_cur) {
821 prev = st->st_cur;
822 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 return 0;
824 }
825 Py_DECREF(st->st_cur);
826 }
827 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000828 if (st->st_cur == NULL)
829 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 if (name == GET_IDENTIFIER(top))
831 st->st_global = st->st_cur->ste_symbols;
832 if (prev) {
833 if (PyList_Append(prev->ste_children,
834 (PyObject *)st->st_cur) < 0) {
835 return 0;
836 }
837 }
838 return 1;
839}
840
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000841static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842symtable_lookup(struct symtable *st, PyObject *name)
843{
844 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000845 PyObject *mangled = _Py_Mangle(st->st_private, name);
846 if (!mangled)
847 return 0;
848 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
849 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 if (!o)
851 return 0;
852 return PyInt_AsLong(o);
853}
854
855static int
856symtable_add_def(struct symtable *st, PyObject *name, int flag)
857{
858 PyObject *o;
859 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000860 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000861 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862
Jeremy Hylton81e95022007-02-27 06:50:52 +0000863
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000864 if (!mangled)
865 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000867 if ((o = PyDict_GetItem(dict, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 val = PyInt_AS_LONG(o);
869 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000870 /* Is it better to use 'mangled' or 'name' here? */
Neal Norwitza5d16a32007-08-24 22:53:58 +0000871 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 PyErr_SyntaxLocation(st->st_filename,
873 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000874 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 }
876 val |= flag;
877 } else
878 val = flag;
879 o = PyInt_FromLong(val);
880 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000881 goto error;
882 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000884 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885 }
886 Py_DECREF(o);
887
888 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000889 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
890 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891 } else if (flag & DEF_GLOBAL) {
892 /* XXX need to update DEF_GLOBAL for other flags too;
893 perhaps only DEF_FREE_GLOBAL */
894 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000895 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896 val |= PyInt_AS_LONG(o);
897 }
898 o = PyInt_FromLong(val);
899 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000900 goto error;
901 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000903 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904 }
905 Py_DECREF(o);
906 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000907 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000909
910error:
911 Py_DECREF(mangled);
912 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913}
914
915/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
916 They use the ASDL name to synthesize the name of the C type and the visit
917 function.
918
919 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
920 useful if the first node in the sequence requires special treatment.
921*/
922
923#define VISIT(ST, TYPE, V) \
924 if (!symtable_visit_ ## TYPE((ST), (V))) \
925 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000926
927#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
928 if (!symtable_visit_ ## TYPE((ST), (V))) { \
929 symtable_exit_block((ST), (S)); \
930 return 0; \
931 }
932
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933#define VISIT_SEQ(ST, TYPE, SEQ) { \
934 int i; \
935 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
936 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000937 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938 if (!symtable_visit_ ## TYPE((ST), elt)) \
939 return 0; \
940 } \
941}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000942
943#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
944 int i; \
945 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
946 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000947 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000948 if (!symtable_visit_ ## TYPE((ST), elt)) { \
949 symtable_exit_block((ST), (S)); \
950 return 0; \
951 } \
952 } \
953}
954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
956 int i; \
957 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
958 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000959 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 if (!symtable_visit_ ## TYPE((ST), elt)) \
961 return 0; \
962 } \
963}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000964
965#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
966 int i; \
967 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
968 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000969 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000970 if (!symtable_visit_ ## TYPE((ST), elt)) { \
971 symtable_exit_block((ST), (S)); \
972 return 0; \
973 } \
974 } \
975}
976
Guido van Rossum4f72a782006-10-27 23:31:49 +0000977#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
978 int i = 0; \
979 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
980 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
981 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
982 if (!elt) continue; /* can be NULL */ \
983 if (!symtable_visit_expr((ST), elt)) \
984 return 0; \
985 } \
986}
987
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000989symtable_new_tmpname(struct symtable *st)
990{
991 char tmpname[256];
992 identifier tmp;
993
994 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
995 ++st->st_cur->ste_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000996 tmp = PyUnicode_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000997 if (!tmp)
998 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000999 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1000 return 0;
1001 Py_DECREF(tmp);
1002 return 1;
1003}
1004
Guido van Rossum4f72a782006-10-27 23:31:49 +00001005
1006
Guido van Rossumc2e20742006-02-27 22:32:47 +00001007static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008symtable_visit_stmt(struct symtable *st, stmt_ty s)
1009{
1010 switch (s->kind) {
1011 case FunctionDef_kind:
1012 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1013 return 0;
1014 if (s->v.FunctionDef.args->defaults)
1015 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001016 if (s->v.FunctionDef.args->kw_defaults)
1017 VISIT_KWONLYDEFAULTS(st,
1018 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001019 if (!symtable_visit_annotations(st, s))
1020 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001021 if (s->v.FunctionDef.decorator_list)
1022 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1024 FunctionBlock, (void *)s, s->lineno))
1025 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001026 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1027 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028 if (!symtable_exit_block(st, s))
1029 return 0;
1030 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001031 case ClassDef_kind: {
1032 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1034 return 0;
1035 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001036 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1037 if (s->v.ClassDef.starargs)
1038 VISIT(st, expr, s->v.ClassDef.starargs);
1039 if (s->v.ClassDef.kwargs)
1040 VISIT(st, expr, s->v.ClassDef.kwargs);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001041 if (s->v.ClassDef.decorator_list)
1042 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1044 (void *)s, s->lineno))
1045 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001046 if (!GET_IDENTIFIER(__class__) ||
1047 !symtable_add_def(st, __class__, DEF_LOCAL)) {
1048 symtable_exit_block(st, s);
1049 return 0;
1050 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001051 tmp = st->st_private;
1052 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001053 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001054 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 if (!symtable_exit_block(st, s))
1056 return 0;
1057 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001058 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001060 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001062 st->st_cur->ste_returns_value = 1;
1063 if (st->st_cur->ste_generator) {
1064 PyErr_SetString(PyExc_SyntaxError,
1065 RETURN_VAL_IN_GENERATOR);
1066 PyErr_SyntaxLocation(st->st_filename,
1067 s->lineno);
1068 return 0;
1069 }
1070 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071 break;
1072 case Delete_kind:
1073 VISIT_SEQ(st, expr, s->v.Delete.targets);
1074 break;
1075 case Assign_kind:
1076 VISIT_SEQ(st, expr, s->v.Assign.targets);
1077 VISIT(st, expr, s->v.Assign.value);
1078 break;
1079 case AugAssign_kind:
1080 VISIT(st, expr, s->v.AugAssign.target);
1081 VISIT(st, expr, s->v.AugAssign.value);
1082 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 case For_kind:
1084 VISIT(st, expr, s->v.For.target);
1085 VISIT(st, expr, s->v.For.iter);
1086 VISIT_SEQ(st, stmt, s->v.For.body);
1087 if (s->v.For.orelse)
1088 VISIT_SEQ(st, stmt, s->v.For.orelse);
1089 break;
1090 case While_kind:
1091 VISIT(st, expr, s->v.While.test);
1092 VISIT_SEQ(st, stmt, s->v.While.body);
1093 if (s->v.While.orelse)
1094 VISIT_SEQ(st, stmt, s->v.While.orelse);
1095 break;
1096 case If_kind:
1097 /* XXX if 0: and lookup_yield() hacks */
1098 VISIT(st, expr, s->v.If.test);
1099 VISIT_SEQ(st, stmt, s->v.If.body);
1100 if (s->v.If.orelse)
1101 VISIT_SEQ(st, stmt, s->v.If.orelse);
1102 break;
1103 case Raise_kind:
Collin Winter828f04a2007-08-31 00:04:24 +00001104 if (s->v.Raise.exc) {
1105 VISIT(st, expr, s->v.Raise.exc);
1106 if (s->v.Raise.cause) {
1107 VISIT(st, expr, s->v.Raise.cause);
1108 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109 }
1110 break;
1111 case TryExcept_kind:
1112 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1113 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1114 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1115 break;
1116 case TryFinally_kind:
1117 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1118 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1119 break;
1120 case Assert_kind:
1121 VISIT(st, expr, s->v.Assert.test);
1122 if (s->v.Assert.msg)
1123 VISIT(st, expr, s->v.Assert.msg);
1124 break;
1125 case Import_kind:
1126 VISIT_SEQ(st, alias, s->v.Import.names);
1127 /* XXX Don't have the lineno available inside
1128 visit_alias */
1129 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1130 st->st_cur->ste_opt_lineno = s->lineno;
1131 break;
1132 case ImportFrom_kind:
1133 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1134 /* XXX Don't have the lineno available inside
1135 visit_alias */
1136 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1137 st->st_cur->ste_opt_lineno = s->lineno;
1138 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 case Global_kind: {
1140 int i;
1141 asdl_seq *seq = s->v.Global.names;
1142 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001143 identifier name = (identifier)asdl_seq_GET(seq, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001144 char *c_name = PyUnicode_AsString(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001145 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 if (cur < 0)
1147 return 0;
1148 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001149 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 if (cur & DEF_LOCAL)
1151 PyOS_snprintf(buf, sizeof(buf),
1152 GLOBAL_AFTER_ASSIGN,
1153 c_name);
1154 else
1155 PyOS_snprintf(buf, sizeof(buf),
1156 GLOBAL_AFTER_USE,
1157 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001158 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 return 0;
1160 }
1161 if (!symtable_add_def(st, name, DEF_GLOBAL))
1162 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 break;
1165 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001166 case Nonlocal_kind: {
1167 int i;
1168 asdl_seq *seq = s->v.Nonlocal.names;
1169 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1170 identifier name = (identifier)asdl_seq_GET(seq, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001171 char *c_name = PyUnicode_AsString(name);
Jeremy Hylton81e95022007-02-27 06:50:52 +00001172 long cur = symtable_lookup(st, name);
1173 if (cur < 0)
1174 return 0;
1175 if (cur & (DEF_LOCAL | USE)) {
1176 char buf[256];
1177 if (cur & DEF_LOCAL)
1178 PyOS_snprintf(buf, sizeof(buf),
1179 NONLOCAL_AFTER_ASSIGN,
1180 c_name);
1181 else
1182 PyOS_snprintf(buf, sizeof(buf),
1183 NONLOCAL_AFTER_USE,
1184 c_name);
1185 if (!symtable_warn(st, buf, s->lineno))
1186 return 0;
1187 }
1188 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1189 return 0;
1190 }
1191 break;
1192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 case Expr_kind:
1194 VISIT(st, expr, s->v.Expr.value);
1195 break;
1196 case Pass_kind:
1197 case Break_kind:
1198 case Continue_kind:
1199 /* nothing to do here */
1200 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001201 case With_kind:
1202 if (!symtable_new_tmpname(st))
1203 return 0;
1204 VISIT(st, expr, s->v.With.context_expr);
1205 if (s->v.With.optional_vars) {
1206 if (!symtable_new_tmpname(st))
1207 return 0;
1208 VISIT(st, expr, s->v.With.optional_vars);
1209 }
1210 VISIT_SEQ(st, stmt, s->v.With.body);
1211 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 }
1213 return 1;
1214}
1215
1216static int
1217symtable_visit_expr(struct symtable *st, expr_ty e)
1218{
1219 switch (e->kind) {
1220 case BoolOp_kind:
1221 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1222 break;
1223 case BinOp_kind:
1224 VISIT(st, expr, e->v.BinOp.left);
1225 VISIT(st, expr, e->v.BinOp.right);
1226 break;
1227 case UnaryOp_kind:
1228 VISIT(st, expr, e->v.UnaryOp.operand);
1229 break;
1230 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001231 if (!GET_IDENTIFIER(lambda) ||
1232 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 return 0;
1234 if (e->v.Lambda.args->defaults)
1235 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1236 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001237 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 FunctionBlock, (void *)e, 0))
1239 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001240 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1241 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 if (!symtable_exit_block(st, (void *)e))
1243 return 0;
1244 break;
1245 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001246 case IfExp_kind:
1247 VISIT(st, expr, e->v.IfExp.test);
1248 VISIT(st, expr, e->v.IfExp.body);
1249 VISIT(st, expr, e->v.IfExp.orelse);
1250 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 case Dict_kind:
1252 VISIT_SEQ(st, expr, e->v.Dict.keys);
1253 VISIT_SEQ(st, expr, e->v.Dict.values);
1254 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001255 case Set_kind:
1256 VISIT_SEQ(st, expr, e->v.Set.elts);
1257 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001258 case GeneratorExp_kind:
1259 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001262 case ListComp_kind:
1263 if (!symtable_visit_listcomp(st, e))
1264 return 0;
1265 break;
1266 case SetComp_kind:
1267 if (!symtable_visit_setcomp(st, e))
1268 return 0;
1269 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001270 case DictComp_kind:
1271 if (!symtable_visit_dictcomp(st, e))
1272 return 0;
1273 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 case Yield_kind:
1275 if (e->v.Yield.value)
1276 VISIT(st, expr, e->v.Yield.value);
1277 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001278 if (st->st_cur->ste_returns_value) {
1279 PyErr_SetString(PyExc_SyntaxError,
1280 RETURN_VAL_IN_GENERATOR);
1281 PyErr_SyntaxLocation(st->st_filename,
1282 e->lineno);
1283 return 0;
1284 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 break;
1286 case Compare_kind:
1287 VISIT(st, expr, e->v.Compare.left);
1288 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1289 break;
1290 case Call_kind:
1291 VISIT(st, expr, e->v.Call.func);
1292 VISIT_SEQ(st, expr, e->v.Call.args);
1293 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1294 if (e->v.Call.starargs)
1295 VISIT(st, expr, e->v.Call.starargs);
1296 if (e->v.Call.kwargs)
1297 VISIT(st, expr, e->v.Call.kwargs);
1298 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299 case Num_kind:
1300 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001301 case Bytes_kind:
1302 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 /* Nothing to do here. */
1304 break;
1305 /* The following exprs can be assignment targets. */
1306 case Attribute_kind:
1307 VISIT(st, expr, e->v.Attribute.value);
1308 break;
1309 case Subscript_kind:
1310 VISIT(st, expr, e->v.Subscript.value);
1311 VISIT(st, slice, e->v.Subscript.slice);
1312 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001313 case Starred_kind:
1314 VISIT(st, expr, e->v.Starred.value);
1315 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 case Name_kind:
1317 if (!symtable_add_def(st, e->v.Name.id,
1318 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1319 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001320 /* Special-case super: it counts as a use of __class__ */
1321 if (e->v.Name.ctx == Load &&
1322 st->st_cur->ste_type == FunctionBlock &&
1323 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1324 if (!GET_IDENTIFIER(__class__) ||
1325 !symtable_add_def(st, __class__, USE))
1326 return 0;
1327 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001328 break;
1329 /* child nodes of List and Tuple will have expr_context set */
1330 case List_kind:
1331 VISIT_SEQ(st, expr, e->v.List.elts);
1332 break;
1333 case Tuple_kind:
1334 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1335 break;
1336 }
1337 return 1;
1338}
1339
1340static int
1341symtable_implicit_arg(struct symtable *st, int pos)
1342{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001343 PyObject *id = PyUnicode_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 if (id == NULL)
1345 return 0;
1346 if (!symtable_add_def(st, id, DEF_PARAM)) {
1347 Py_DECREF(id);
1348 return 0;
1349 }
1350 Py_DECREF(id);
1351 return 1;
1352}
1353
1354static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001355symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001357 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001358
1359 if (!args)
1360 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001363 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001364 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 return 0;
1366 }
1367
1368 return 1;
1369}
1370
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001371static int
1372symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373{
1374 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001375
1376 if (!args)
1377 return -1;
1378
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001380 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001381 if (arg->annotation)
1382 VISIT(st, expr, arg->annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001384
1385 return 1;
1386}
1387
Neal Norwitzc1505362006-12-28 06:47:50 +00001388static int
1389symtable_visit_annotations(struct symtable *st, stmt_ty s)
1390{
1391 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001393 if (a->args && !symtable_visit_argannotations(st, a->args))
Neal Norwitzc1505362006-12-28 06:47:50 +00001394 return 0;
1395 if (a->varargannotation)
1396 VISIT(st, expr, a->varargannotation);
1397 if (a->kwargannotation)
1398 VISIT(st, expr, a->kwargannotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001399 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
Neal Norwitzc1505362006-12-28 06:47:50 +00001400 return 0;
1401 if (s->v.FunctionDef.returns)
1402 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 return 1;
1404}
1405
1406static int
1407symtable_visit_arguments(struct symtable *st, arguments_ty a)
1408{
1409 /* skip default arguments inside function block
1410 XXX should ast be different?
1411 */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001412 if (a->args && !symtable_visit_params(st, a->args))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001414 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001415 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 if (a->vararg) {
1417 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1418 return 0;
1419 st->st_cur->ste_varargs = 1;
1420 }
1421 if (a->kwarg) {
1422 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1423 return 0;
1424 st->st_cur->ste_varkeywords = 1;
1425 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 return 1;
1427}
1428
1429
1430static int
1431symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1432{
1433 if (eh->type)
1434 VISIT(st, expr, eh->type);
1435 if (eh->name)
Guido van Rossum16be03e2007-01-10 18:51:35 +00001436 if (!symtable_add_def(st, eh->name, DEF_LOCAL))
1437 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 VISIT_SEQ(st, stmt, eh->body);
1439 return 1;
1440}
1441
1442
1443static int
1444symtable_visit_alias(struct symtable *st, alias_ty a)
1445{
1446 /* Compute store_name, the name actually bound by the import
1447 operation. It is diferent than a->name when a->name is a
1448 dotted package name (e.g. spam.eggs)
1449 */
1450 PyObject *store_name;
1451 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001452 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1453 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001454 if (dot) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001455 store_name = PyUnicode_FromUnicode(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001456 if (!store_name)
1457 return 0;
1458 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 else {
1460 store_name = name;
1461 Py_INCREF(store_name);
1462 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001463 if (PyUnicode_CompareWithASCIIString(name, "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1465 Py_DECREF(store_name);
1466 return r;
1467 }
1468 else {
1469 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001470 int lineno = st->st_cur->ste_lineno;
Guido van Rossum33d26892007-08-05 15:29:28 +00001471 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1472 PyErr_SyntaxLocation(st->st_filename, lineno);
1473 Py_DECREF(store_name);
1474 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 }
1476 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001477 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 return 1;
1479 }
1480}
1481
1482
1483static int
1484symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1485{
1486 VISIT(st, expr, lc->target);
1487 VISIT(st, expr, lc->iter);
1488 VISIT_SEQ(st, expr, lc->ifs);
1489 return 1;
1490}
1491
1492
1493static int
1494symtable_visit_keyword(struct symtable *st, keyword_ty k)
1495{
1496 VISIT(st, expr, k->value);
1497 return 1;
1498}
1499
1500
1501static int
1502symtable_visit_slice(struct symtable *st, slice_ty s)
1503{
1504 switch (s->kind) {
1505 case Slice_kind:
1506 if (s->v.Slice.lower)
1507 VISIT(st, expr, s->v.Slice.lower)
1508 if (s->v.Slice.upper)
1509 VISIT(st, expr, s->v.Slice.upper)
1510 if (s->v.Slice.step)
1511 VISIT(st, expr, s->v.Slice.step)
1512 break;
1513 case ExtSlice_kind:
1514 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1515 break;
1516 case Index_kind:
1517 VISIT(st, expr, s->v.Index.value)
1518 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 }
1520 return 1;
1521}
1522
1523static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001524symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001525 identifier scope_name, asdl_seq *generators,
1526 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001528 int is_generator = (e->kind == GeneratorExp_kind);
1529 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001531 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 /* Outermost iterator is evaluated in current scope */
1533 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001534 /* Create comprehension scope for the rest */
1535 if (!scope_name ||
1536 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537 return 0;
1538 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001539 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 /* Outermost iter is received as an argument */
1541 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001542 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 return 0;
1544 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001545 /* Allocate temporary name if needed */
1546 if (needs_tmp && !symtable_new_tmpname(st)) {
1547 symtable_exit_block(st, (void *)e);
1548 return 0;
1549 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001550 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1551 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1552 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001553 generators, 1, (void*)e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001554 if (value)
1555 VISIT_IN_BLOCK(st, expr, value, (void*)e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001556 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001557 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001559
1560static int
1561symtable_visit_genexp(struct symtable *st, expr_ty e)
1562{
1563 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1564 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001565 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001566}
1567
1568static int
1569symtable_visit_listcomp(struct symtable *st, expr_ty e)
1570{
1571 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1572 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001573 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001574}
1575
1576static int
1577symtable_visit_setcomp(struct symtable *st, expr_ty e)
1578{
1579 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1580 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001581 e->v.SetComp.elt, NULL);
1582}
1583
1584static int
1585symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1586{
1587 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1588 e->v.DictComp.generators,
1589 e->v.DictComp.key,
1590 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001591}