blob: 0c11ee1aab31f22f339541f6eb866b04c1909778 [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;
Christian Heimes08976cb2008-03-16 00:32:36 +000036 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
37 if (ste == NULL)
38 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000039 ste->ste_table = st;
40 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000041 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043 ste->ste_name = name;
44 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000045
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000046 ste->ste_symbols = NULL;
47 ste->ste_varnames = NULL;
48 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000049
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000050 ste->ste_symbols = PyDict_New();
51 if (ste->ste_symbols == NULL)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000052 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000053
54 ste->ste_varnames = PyList_New(0);
55 if (ste->ste_varnames == NULL)
56 goto fail;
57
58 ste->ste_children = PyList_New(0);
59 if (ste->ste_children == NULL)
60 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000062 ste->ste_type = block;
63 ste->ste_unoptimized = 0;
64 ste->ste_nested = 0;
65 ste->ste_free = 0;
66 ste->ste_varargs = 0;
67 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000068 ste->ste_opt_lineno = 0;
Jeremy Hylton4d508ad2003-05-21 17:34:50 +000069 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000071
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000072 if (st->st_cur != NULL &&
73 (st->st_cur->ste_nested ||
74 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000075 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000076 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000077 ste->ste_generator = 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000078 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000079
Nick Coghlan650f0d02007-04-15 12:05:43 +000080 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000081 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000082
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084 fail:
85 Py_XDECREF(ste);
86 return NULL;
87}
88
89static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000090ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000091{
Walter Dörwald5d4ede12007-06-11 16:03:16 +000092 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
93 ste->ste_name,
Christian Heimes217cfd12007-12-02 14:31:20 +000094 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000095}
96
97static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000099{
100 ste->ste_table = NULL;
101 Py_XDECREF(ste->ste_id);
102 Py_XDECREF(ste->ste_name);
103 Py_XDECREF(ste->ste_symbols);
104 Py_XDECREF(ste->ste_varnames);
105 Py_XDECREF(ste->ste_children);
106 PyObject_Del(ste);
107}
108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000110
Guido van Rossum6f799372001-09-20 20:46:19 +0000111static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000112 {"id", T_OBJECT, OFF(ste_id), READONLY},
113 {"name", T_OBJECT, OFF(ste_name), READONLY},
114 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
115 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
116 {"children", T_OBJECT, OFF(ste_children), READONLY},
117 {"type", T_INT, OFF(ste_type), READONLY},
118 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119 {NULL}
120};
121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122PyTypeObject PySTEntry_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000123 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126 0,
127 (destructor)ste_dealloc, /* tp_dealloc */
128 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000129 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130 0, /* tp_setattr */
131 0, /* tp_compare */
132 (reprfunc)ste_repr, /* tp_repr */
133 0, /* tp_as_number */
134 0, /* tp_as_sequence */
135 0, /* tp_as_mapping */
136 0, /* tp_hash */
137 0, /* tp_call */
138 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000139 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000140 0, /* tp_setattro */
141 0, /* tp_as_buffer */
142 Py_TPFLAGS_DEFAULT, /* tp_flags */
143 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000144 0, /* tp_traverse */
145 0, /* tp_clear */
146 0, /* tp_richcompare */
147 0, /* tp_weaklistoffset */
148 0, /* tp_iter */
149 0, /* tp_iternext */
150 0, /* tp_methods */
151 ste_memberlist, /* tp_members */
152 0, /* tp_getset */
153 0, /* tp_base */
154 0, /* tp_dict */
155 0, /* tp_descr_get */
156 0, /* tp_descr_set */
157 0, /* tp_dictoffset */
158 0, /* tp_init */
159 0, /* tp_alloc */
160 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000161};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162
163static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000164static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000166 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_exit_block(struct symtable *st, void *ast);
168static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
169static int symtable_visit_expr(struct symtable *st, expr_ty s);
170static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000171static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
172static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000173static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static int symtable_visit_arguments(struct symtable *st, arguments_ty);
175static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
176static int symtable_visit_alias(struct symtable *st, alias_ty);
177static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
178static int symtable_visit_keyword(struct symtable *st, keyword_ty);
179static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000180static int symtable_visit_params(struct symtable *st, asdl_seq *args);
181static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000183static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184
185
Nick Coghlan650f0d02007-04-15 12:05:43 +0000186static identifier top = NULL, lambda = NULL, genexpr = NULL,
Guido van Rossum992d4a32007-07-11 13:09:30 +0000187 listcomp = NULL, setcomp = NULL, dictcomp = NULL, __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188
189#define GET_IDENTIFIER(VAR) \
Martin v. Löwis5b222132007-06-10 09:51:05 +0000190 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
192#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000193"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194
195static struct symtable *
196symtable_new(void)
197{
198 struct symtable *st;
199
200 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
201 if (st == NULL)
202 return NULL;
203
204 st->st_filename = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000205 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000206
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 if ((st->st_stack = PyList_New(0)) == NULL)
208 goto fail;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000209 if ((st->st_blocks = PyDict_New()) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 goto fail;
211 st->st_cur = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212 st->st_private = NULL;
213 return st;
214 fail:
215 PySymtable_Free(st);
216 return NULL;
217}
218
219struct symtable *
220PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
221{
222 struct symtable *st = symtable_new();
223 asdl_seq *seq;
224 int i;
225
226 if (st == NULL)
227 return st;
228 st->st_filename = filename;
229 st->st_future = future;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000230 /* Make the initial symbol information gathering pass */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000231 if (!GET_IDENTIFIER(top) ||
232 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000233 PySymtable_Free(st);
234 return NULL;
235 }
236
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 st->st_top = st->st_cur;
238 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 switch (mod->kind) {
240 case Module_kind:
241 seq = mod->v.Module.body;
242 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000243 if (!symtable_visit_stmt(st,
244 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245 goto error;
246 break;
247 case Expression_kind:
248 if (!symtable_visit_expr(st, mod->v.Expression.body))
249 goto error;
250 break;
251 case Interactive_kind:
252 seq = mod->v.Interactive.body;
253 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000254 if (!symtable_visit_stmt(st,
255 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256 goto error;
257 break;
258 case Suite_kind:
259 PyErr_SetString(PyExc_RuntimeError,
260 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000261 goto error;
262 }
263 if (!symtable_exit_block(st, (void *)mod)) {
264 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 return NULL;
266 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000267 /* Make the second symbol analysis pass */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 if (symtable_analyze(st))
269 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000270 PySymtable_Free(st);
271 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000273 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 PySymtable_Free(st);
275 return NULL;
276}
277
278void
279PySymtable_Free(struct symtable *st)
280{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000281 Py_XDECREF(st->st_blocks);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282 Py_XDECREF(st->st_stack);
283 PyMem_Free((void *)st);
284}
285
286PySTEntryObject *
287PySymtable_Lookup(struct symtable *st, void *key)
288{
289 PyObject *k, *v;
290
291 k = PyLong_FromVoidPtr(key);
292 if (k == NULL)
293 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000294 v = PyDict_GetItem(st->st_blocks, k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295 if (v) {
296 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298 }
299 else {
300 PyErr_SetString(PyExc_KeyError,
301 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000303
304 Py_DECREF(k);
305 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306}
307
308int
309PyST_GetScope(PySTEntryObject *ste, PyObject *name)
310{
311 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
312 if (!v)
313 return 0;
Christian Heimes217cfd12007-12-02 14:31:20 +0000314 assert(PyLong_Check(v));
315 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000316}
317
318
319/* Analyze raw symbol information to determine scope of each name.
320
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000321 The next several functions are helpers for symtable_analyze(),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322 which determines whether a name is local, global, or free. In addition,
323 it determines which local variables are cell variables; they provide
324 bindings that are used for free variables in enclosed blocks.
325
Nick Coghlan650f0d02007-04-15 12:05:43 +0000326 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327 explicit global is declared with the global statement. An implicit
328 global is a free variable for which the compiler has found no binding
329 in an enclosing function scope. The implicit global is either a global
330 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
331 to handle these names to implement slightly odd semantics. In such a
332 block, the name is treated as global until it is assigned to; then it
333 is treated as a local.
334
335 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000336 The first pass collects raw facts from the AST via the symtable_visit_*
337 functions: the name is a parameter here, the name is used but not defined
338 here, etc. The second pass analyzes these facts during a pass over the
339 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340
341 When a function is entered during the second pass, the parent passes
342 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000343 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000344 Names which are explicitly declared nonlocal must exist in this set of
345 visible names - if they do not, a syntax error is raised. After doing
346 the local analysis, it analyzes each of its child blocks using an
347 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348
Nick Coghlan650f0d02007-04-15 12:05:43 +0000349 The children update the free variable set. If a local variable is added to
350 the free variable set by the child, the variable is marked as a cell. The
351 function object being defined must provide runtime storage for the variable
352 that may outlive the function's frame. Cell variables are removed from the
353 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000354
Nick Coghlan650f0d02007-04-15 12:05:43 +0000355 During analysis, the names are:
356 symbols: dict mapping from symbol names to flag values (including offset scope values)
357 scopes: dict mapping from symbol names to scope values (no offset)
358 local: set of all symbol names local to the current scope
359 bound: set of all symbol names local to a containing function scope
360 free: set of all symbol names referenced but not bound in child scopes
361 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362*/
363
364#define SET_SCOPE(DICT, NAME, I) { \
Christian Heimes217cfd12007-12-02 14:31:20 +0000365 PyObject *o = PyLong_FromLong(I); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366 if (!o) \
367 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000368 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
369 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000371 } \
372 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373}
374
375/* Decide on scope of name, given flags.
376
377 The dicts passed in as arguments are modified as necessary.
378 ste is passed so that flags can be updated.
379*/
380
381static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000382analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 PyObject *bound, PyObject *local, PyObject *free,
384 PyObject *global)
385{
386 if (flags & DEF_GLOBAL) {
387 if (flags & DEF_PARAM) {
388 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000389 "name '%U' is parameter and global",
390 name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000391 return 0;
392 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000393 if (flags & DEF_NONLOCAL) {
394 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000395 "name '%U' is nonlocal and global",
396 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000397 return 0;
398 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000399 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
400 if (PySet_Add(global, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000401 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000402 if (bound && (PySet_Discard(bound, name) < 0))
403 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404 return 1;
405 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000406 if (flags & DEF_NONLOCAL) {
407 if (flags & DEF_PARAM) {
408 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000409 "name '%U' is parameter and nonlocal",
410 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000411 return 0;
412 }
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000413 if (!bound) {
414 PyErr_Format(PyExc_SyntaxError,
415 "nonlocal declaration not allowed at module level");
416 return 0;
417 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000418 if (!PySet_Contains(bound, name)) {
Jeremy Hylton81e95022007-02-27 06:50:52 +0000419 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000420 "no binding for nonlocal '%U' found",
421 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000422
423 return 0;
424 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000425 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000426 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000427 return PySet_Add(free, name) >= 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000428 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429 if (flags & DEF_BOUND) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000430 SET_SCOPE(scopes, name, LOCAL);
431 if (PySet_Add(local, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000433 if (PySet_Discard(global, name) < 0)
434 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435 return 1;
436 }
437 /* If an enclosing block has a binding for this name, it
438 is a free variable rather than a global variable.
439 Note that having a non-NULL bound implies that the block
440 is nested.
441 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000442 if (bound && PySet_Contains(bound, name)) {
443 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000445 return PySet_Add(free, name) >= 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446 }
447 /* If a parent has a global statement, then call it global
448 explicit? It could also be global implicit.
449 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000450 if (global && PySet_Contains(global, name)) {
451 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 return 1;
453 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000454 if (ste->ste_nested)
455 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000456 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000457 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458}
459
460#undef SET_SCOPE
461
462/* If a name is defined in free and also in locals, then this block
463 provides the binding for the free variable. The name should be
464 marked CELL in this block and removed from the free list.
465
466 Note that the current block's free variables are included in free.
467 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000468
Martin v. Löwis2673a572007-10-29 19:54:24 +0000469 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000470 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471*/
472
473static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000474analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000476 PyObject *name, *v, *v_cell;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000477 int success = 0;
478 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479
Christian Heimes217cfd12007-12-02 14:31:20 +0000480 v_cell = PyLong_FromLong(CELL);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000481 if (!v_cell)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000483 while (PyDict_Next(scopes, &pos, &name, &v)) {
484 long scope;
Christian Heimes217cfd12007-12-02 14:31:20 +0000485 assert(PyLong_Check(v));
486 scope = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000487 if (scope != LOCAL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488 continue;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000489 if (!PySet_Contains(free, name))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 continue;
Martin v. Löwis2673a572007-10-29 19:54:24 +0000491 if (restricted != NULL &&
492 PyUnicode_CompareWithASCIIString(name, restricted))
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000493 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494 /* Replace LOCAL with CELL for this name, and remove
495 from free. It is safe to replace the value of name
496 in the dict, because it will not cause a resize.
497 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000498 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000500 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501 goto error;
502 }
503 success = 1;
504 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000505 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 return success;
507}
508
509/* Check for illegal statements in unoptimized namespaces */
510static int
511check_unoptimized(const PySTEntryObject* ste) {
Armin Rigo31441302005-10-21 12:57:31 +0000512 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000514 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 || !(ste->ste_free || ste->ste_child_free))
516 return 1;
517
Armin Rigo31441302005-10-21 12:57:31 +0000518 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 "contains a nested function with free variables" :
520 "is a nested function");
521
522 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000523 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 return 1;
525 case OPT_IMPORT_STAR:
Walter Dörwald3ef72bb2007-06-11 16:12:10 +0000526 PyErr_Format(PyExc_SyntaxError,
527 "import * is not allowed in function '%U' because it %s",
Walter Dörwalde42109d2007-06-11 16:08:41 +0000528 ste->ste_name, trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530 }
531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532 PyErr_SyntaxLocation(ste->ste_table->st_filename,
533 ste->ste_opt_lineno);
534 return 0;
535}
536
Nick Coghlan650f0d02007-04-15 12:05:43 +0000537/* Enter the final scope information into the ste_symbols dict.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 *
539 * All arguments are dicts. Modifies symbols, others are read-only.
540*/
541static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000542update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000545 PyObject *name = NULL, *itr = NULL;
546 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000547 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Nick Coghlan650f0d02007-04-15 12:05:43 +0000549 /* Update scope information for all symbols in this scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550 while (PyDict_Next(symbols, &pos, &name, &v)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000551 long scope, flags;
Christian Heimes217cfd12007-12-02 14:31:20 +0000552 assert(PyLong_Check(v));
553 flags = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000554 v_scope = PyDict_GetItem(scopes, name);
Christian Heimes217cfd12007-12-02 14:31:20 +0000555 assert(v_scope && PyLong_Check(v_scope));
556 scope = PyLong_AS_LONG(v_scope);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000557 flags |= (scope << SCOPE_OFFSET);
Christian Heimes217cfd12007-12-02 14:31:20 +0000558 v_new = PyLong_FromLong(flags);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000559 if (!v_new)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000560 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000561 if (PyDict_SetItem(symbols, name, v_new) < 0) {
562 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 return 0;
564 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000565 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566 }
567
Nick Coghlan650f0d02007-04-15 12:05:43 +0000568 /* Record not yet resolved free variables from children (if any) */
Christian Heimes217cfd12007-12-02 14:31:20 +0000569 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000570 if (!v_free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 return 0;
572
Nick Coghlan650f0d02007-04-15 12:05:43 +0000573 itr = PyObject_GetIter(free);
574 if (!itr)
575 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576
Nick Coghlan650f0d02007-04-15 12:05:43 +0000577 while ((name = PyIter_Next(itr))) {
578 v = PyDict_GetItem(symbols, name);
579
580 /* Handle symbol that already exists in this scope */
581 if (v) {
582 /* Handle a free variable in a method of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583 the class that has the same name as a local
584 or global in the class scope.
585 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000586 if (classflag &&
Christian Heimes217cfd12007-12-02 14:31:20 +0000587 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
588 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
589 v_new = PyLong_FromLong(flags);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000590 if (!v_new) {
591 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000593 if (PyDict_SetItem(symbols, name, v_new) < 0) {
594 Py_DECREF(v_new);
595 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000597 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000599 /* It's a cell, or already free in this scope */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000600 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 continue;
602 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000603 /* Handle global symbol */
604 if (!PySet_Contains(bound, name)) {
605 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 continue; /* it's a global */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000608 /* Propagate new free symbol up the lexical stack */
609 if (PyDict_SetItem(symbols, name, v_free) < 0) {
610 goto error;
611 }
612 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000614 Py_DECREF(itr);
615 Py_DECREF(v_free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000617error:
618 Py_XDECREF(v_free);
619 Py_XDECREF(itr);
620 Py_XDECREF(name);
621 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622}
623
624/* Make final symbol table decisions for block of ste.
625 Arguments:
626 ste -- current symtable entry (input/output)
627 bound -- set of variables bound in enclosing scopes (input)
628 free -- set of free variables in enclosed scopes (output)
629 globals -- set of declared global variables in enclosing scopes (input)
630*/
631
632static int
633analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
634 PyObject *global)
635{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000636 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000638 int i, success = 0;
639 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640
Nick Coghlan650f0d02007-04-15 12:05:43 +0000641 scopes = PyDict_New();
642 if (!scopes)
643 goto error;
644 local = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 if (!local)
646 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000647 newglobal = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648 if (!newglobal)
649 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000650 newfree = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 if (!newfree)
652 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000653 newbound = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654 if (!newbound)
655 goto error;
656
Nick Coghlan650f0d02007-04-15 12:05:43 +0000657 /* Class namespace has no effect on names visible in
658 nested functions, so populate the global and bound
659 sets to be passed to child blocks before analyzing
660 this one.
661 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662 if (ste->ste_type == ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000663 /* Pass down previously bound symbols */
664 if (bound) {
665 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000667 Py_DECREF(newbound);
668 }
669 /* Pass down known globals */
670 if (!PyNumber_InPlaceOr(newglobal, global))
671 goto error;
672 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 }
674
Nick Coghlan650f0d02007-04-15 12:05:43 +0000675 /* Analyze symbols in current scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 assert(PySTEntry_Check(ste));
677 assert(PyDict_Check(ste->ste_symbols));
678 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000679 long flags = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000680 if (!analyze_name(ste, scopes, name, flags, bound, local, free,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681 global))
682 goto error;
683 }
684
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000685 /* Populate global and bound sets to be passed to children. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000687 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 if (ste->ste_type == FunctionBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000689 if (!PyNumber_InPlaceOr(newbound, local))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000691 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000693 /* Pass down previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694 if (bound) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000695 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000697 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000699 /* Pass down known globals */
700 if (!PyNumber_InPlaceOr(newglobal, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000702 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000704 else {
705 /* Special-case __class__ */
706 if (!GET_IDENTIFIER(__class__))
707 goto error;
708 assert(PySet_Contains(local, __class__) == 1);
709 if (PySet_Add(newbound, __class__) < 0)
710 goto error;
711 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712
713 /* Recursively call analyze_block() on each child block */
714 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
715 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000716 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000718 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719 if (!analyze_block(entry, newbound, newfree, newglobal))
720 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000721 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 if (entry->ste_free || entry->ste_child_free)
723 ste->ste_child_free = 1;
724 }
725
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000726 /* Check if any local variables must be converted to cell variables */
727 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
728 NULL))
729 goto error;
730 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
731 "__class__"))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000733 /* Records the results of the analysis in the symbol table entry */
734 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735 ste->ste_type == ClassBlock))
736 goto error;
737 if (!check_unoptimized(ste))
738 goto error;
739
Nick Coghlan650f0d02007-04-15 12:05:43 +0000740 if (!PyNumber_InPlaceOr(free, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000742 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743 success = 1;
744 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000745 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 Py_XDECREF(newbound);
748 Py_XDECREF(newglobal);
749 Py_XDECREF(newfree);
750 if (!success)
751 assert(PyErr_Occurred());
752 return success;
753}
754
755static int
756symtable_analyze(struct symtable *st)
757{
758 PyObject *free, *global;
759 int r;
760
Nick Coghlan650f0d02007-04-15 12:05:43 +0000761 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762 if (!free)
763 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000764 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000766 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 return 0;
768 }
769 r = analyze_block(st->st_top, NULL, free, global);
770 Py_DECREF(free);
771 Py_DECREF(global);
772 return r;
773}
774
775
776static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000777symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000778{
779 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000780 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
782 PyErr_SetString(PyExc_SyntaxError, msg);
783 PyErr_SyntaxLocation(st->st_filename,
784 st->st_cur->ste_lineno);
785 }
786 return 0;
787 }
788 return 1;
789}
790
791/* symtable_enter_block() gets a reference via PySTEntry_New().
792 This reference is released when the block is exited, via the DECREF
793 in symtable_exit_block().
794*/
795
796static int
797symtable_exit_block(struct symtable *st, void *ast)
798{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000799 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000801 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 end = PyList_GET_SIZE(st->st_stack) - 1;
803 if (end >= 0) {
804 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
805 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000806 if (st->st_cur == NULL)
807 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 Py_INCREF(st->st_cur);
809 if (PySequence_DelItem(st->st_stack, end) < 0)
810 return 0;
811 }
812 return 1;
813}
814
815static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000816symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 void *ast, int lineno)
818{
819 PySTEntryObject *prev = NULL;
820
821 if (st->st_cur) {
822 prev = st->st_cur;
823 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824 return 0;
825 }
826 Py_DECREF(st->st_cur);
827 }
828 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000829 if (st->st_cur == NULL)
830 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 if (name == GET_IDENTIFIER(top))
832 st->st_global = st->st_cur->ste_symbols;
833 if (prev) {
834 if (PyList_Append(prev->ste_children,
835 (PyObject *)st->st_cur) < 0) {
836 return 0;
837 }
838 }
839 return 1;
840}
841
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000842static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843symtable_lookup(struct symtable *st, PyObject *name)
844{
845 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000846 PyObject *mangled = _Py_Mangle(st->st_private, name);
847 if (!mangled)
848 return 0;
849 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
850 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851 if (!o)
852 return 0;
Christian Heimes217cfd12007-12-02 14:31:20 +0000853 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854}
855
856static int
857symtable_add_def(struct symtable *st, PyObject *name, int flag)
858{
859 PyObject *o;
860 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000861 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000862 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863
Jeremy Hylton81e95022007-02-27 06:50:52 +0000864
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000865 if (!mangled)
866 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000868 if ((o = PyDict_GetItem(dict, mangled))) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000869 val = PyLong_AS_LONG(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000871 /* Is it better to use 'mangled' or 'name' here? */
Neal Norwitza5d16a32007-08-24 22:53:58 +0000872 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 PyErr_SyntaxLocation(st->st_filename,
874 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000875 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 }
877 val |= flag;
878 } else
879 val = flag;
Christian Heimes217cfd12007-12-02 14:31:20 +0000880 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000882 goto error;
883 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000885 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886 }
887 Py_DECREF(o);
888
889 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000890 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
891 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 } else if (flag & DEF_GLOBAL) {
893 /* XXX need to update DEF_GLOBAL for other flags too;
894 perhaps only DEF_FREE_GLOBAL */
895 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000896 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000897 val |= PyLong_AS_LONG(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000899 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000901 goto error;
902 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000904 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905 }
906 Py_DECREF(o);
907 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000908 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000910
911error:
912 Py_DECREF(mangled);
913 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914}
915
916/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
917 They use the ASDL name to synthesize the name of the C type and the visit
918 function.
919
920 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
921 useful if the first node in the sequence requires special treatment.
922*/
923
924#define VISIT(ST, TYPE, V) \
925 if (!symtable_visit_ ## TYPE((ST), (V))) \
926 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000927
928#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
929 if (!symtable_visit_ ## TYPE((ST), (V))) { \
930 symtable_exit_block((ST), (S)); \
931 return 0; \
932 }
933
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934#define VISIT_SEQ(ST, TYPE, SEQ) { \
935 int i; \
936 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
937 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000938 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 if (!symtable_visit_ ## TYPE((ST), elt)) \
940 return 0; \
941 } \
942}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000943
944#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
945 int i; \
946 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
947 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000948 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000949 if (!symtable_visit_ ## TYPE((ST), elt)) { \
950 symtable_exit_block((ST), (S)); \
951 return 0; \
952 } \
953 } \
954}
955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
957 int i; \
958 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
959 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000960 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961 if (!symtable_visit_ ## TYPE((ST), elt)) \
962 return 0; \
963 } \
964}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000965
966#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
967 int i; \
968 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
969 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000970 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000971 if (!symtable_visit_ ## TYPE((ST), elt)) { \
972 symtable_exit_block((ST), (S)); \
973 return 0; \
974 } \
975 } \
976}
977
Guido van Rossum4f72a782006-10-27 23:31:49 +0000978#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
979 int i = 0; \
980 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
981 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
982 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
983 if (!elt) continue; /* can be NULL */ \
984 if (!symtable_visit_expr((ST), elt)) \
985 return 0; \
986 } \
987}
988
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000990symtable_new_tmpname(struct symtable *st)
991{
992 char tmpname[256];
993 identifier tmp;
994
995 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
996 ++st->st_cur->ste_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000997 tmp = PyUnicode_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000998 if (!tmp)
999 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001000 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1001 return 0;
1002 Py_DECREF(tmp);
1003 return 1;
1004}
1005
Guido van Rossum4f72a782006-10-27 23:31:49 +00001006
1007
Guido van Rossumc2e20742006-02-27 22:32:47 +00001008static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009symtable_visit_stmt(struct symtable *st, stmt_ty s)
1010{
1011 switch (s->kind) {
1012 case FunctionDef_kind:
1013 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1014 return 0;
1015 if (s->v.FunctionDef.args->defaults)
1016 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001017 if (s->v.FunctionDef.args->kw_defaults)
1018 VISIT_KWONLYDEFAULTS(st,
1019 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001020 if (!symtable_visit_annotations(st, s))
1021 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001022 if (s->v.FunctionDef.decorator_list)
1023 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1025 FunctionBlock, (void *)s, s->lineno))
1026 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001027 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1028 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 if (!symtable_exit_block(st, s))
1030 return 0;
1031 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001032 case ClassDef_kind: {
1033 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1035 return 0;
1036 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001037 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1038 if (s->v.ClassDef.starargs)
1039 VISIT(st, expr, s->v.ClassDef.starargs);
1040 if (s->v.ClassDef.kwargs)
1041 VISIT(st, expr, s->v.ClassDef.kwargs);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001042 if (s->v.ClassDef.decorator_list)
1043 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1045 (void *)s, s->lineno))
1046 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001047 if (!GET_IDENTIFIER(__class__) ||
1048 !symtable_add_def(st, __class__, DEF_LOCAL)) {
1049 symtable_exit_block(st, s);
1050 return 0;
1051 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001052 tmp = st->st_private;
1053 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001054 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001055 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056 if (!symtable_exit_block(st, s))
1057 return 0;
1058 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001059 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001061 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001063 st->st_cur->ste_returns_value = 1;
1064 if (st->st_cur->ste_generator) {
1065 PyErr_SetString(PyExc_SyntaxError,
1066 RETURN_VAL_IN_GENERATOR);
1067 PyErr_SyntaxLocation(st->st_filename,
1068 s->lineno);
1069 return 0;
1070 }
1071 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 break;
1073 case Delete_kind:
1074 VISIT_SEQ(st, expr, s->v.Delete.targets);
1075 break;
1076 case Assign_kind:
1077 VISIT_SEQ(st, expr, s->v.Assign.targets);
1078 VISIT(st, expr, s->v.Assign.value);
1079 break;
1080 case AugAssign_kind:
1081 VISIT(st, expr, s->v.AugAssign.target);
1082 VISIT(st, expr, s->v.AugAssign.value);
1083 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 case For_kind:
1085 VISIT(st, expr, s->v.For.target);
1086 VISIT(st, expr, s->v.For.iter);
1087 VISIT_SEQ(st, stmt, s->v.For.body);
1088 if (s->v.For.orelse)
1089 VISIT_SEQ(st, stmt, s->v.For.orelse);
1090 break;
1091 case While_kind:
1092 VISIT(st, expr, s->v.While.test);
1093 VISIT_SEQ(st, stmt, s->v.While.body);
1094 if (s->v.While.orelse)
1095 VISIT_SEQ(st, stmt, s->v.While.orelse);
1096 break;
1097 case If_kind:
1098 /* XXX if 0: and lookup_yield() hacks */
1099 VISIT(st, expr, s->v.If.test);
1100 VISIT_SEQ(st, stmt, s->v.If.body);
1101 if (s->v.If.orelse)
1102 VISIT_SEQ(st, stmt, s->v.If.orelse);
1103 break;
1104 case Raise_kind:
Collin Winter828f04a2007-08-31 00:04:24 +00001105 if (s->v.Raise.exc) {
1106 VISIT(st, expr, s->v.Raise.exc);
1107 if (s->v.Raise.cause) {
1108 VISIT(st, expr, s->v.Raise.cause);
1109 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110 }
1111 break;
1112 case TryExcept_kind:
1113 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1114 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1115 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1116 break;
1117 case TryFinally_kind:
1118 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1119 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1120 break;
1121 case Assert_kind:
1122 VISIT(st, expr, s->v.Assert.test);
1123 if (s->v.Assert.msg)
1124 VISIT(st, expr, s->v.Assert.msg);
1125 break;
1126 case Import_kind:
1127 VISIT_SEQ(st, alias, s->v.Import.names);
1128 /* XXX Don't have the lineno available inside
1129 visit_alias */
1130 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1131 st->st_cur->ste_opt_lineno = s->lineno;
1132 break;
1133 case ImportFrom_kind:
1134 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1135 /* XXX Don't have the lineno available inside
1136 visit_alias */
1137 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1138 st->st_cur->ste_opt_lineno = s->lineno;
1139 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 case Global_kind: {
1141 int i;
1142 asdl_seq *seq = s->v.Global.names;
1143 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001144 identifier name = (identifier)asdl_seq_GET(seq, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001145 char *c_name = PyUnicode_AsString(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001146 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 if (cur < 0)
1148 return 0;
1149 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001150 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 if (cur & DEF_LOCAL)
1152 PyOS_snprintf(buf, sizeof(buf),
1153 GLOBAL_AFTER_ASSIGN,
1154 c_name);
1155 else
1156 PyOS_snprintf(buf, sizeof(buf),
1157 GLOBAL_AFTER_USE,
1158 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001159 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 return 0;
1161 }
1162 if (!symtable_add_def(st, name, DEF_GLOBAL))
1163 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 break;
1166 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001167 case Nonlocal_kind: {
1168 int i;
1169 asdl_seq *seq = s->v.Nonlocal.names;
1170 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1171 identifier name = (identifier)asdl_seq_GET(seq, i);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001172 char *c_name = PyUnicode_AsString(name);
Jeremy Hylton81e95022007-02-27 06:50:52 +00001173 long cur = symtable_lookup(st, name);
1174 if (cur < 0)
1175 return 0;
1176 if (cur & (DEF_LOCAL | USE)) {
1177 char buf[256];
1178 if (cur & DEF_LOCAL)
1179 PyOS_snprintf(buf, sizeof(buf),
1180 NONLOCAL_AFTER_ASSIGN,
1181 c_name);
1182 else
1183 PyOS_snprintf(buf, sizeof(buf),
1184 NONLOCAL_AFTER_USE,
1185 c_name);
1186 if (!symtable_warn(st, buf, s->lineno))
1187 return 0;
1188 }
1189 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1190 return 0;
1191 }
1192 break;
1193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 case Expr_kind:
1195 VISIT(st, expr, s->v.Expr.value);
1196 break;
1197 case Pass_kind:
1198 case Break_kind:
1199 case Continue_kind:
1200 /* nothing to do here */
1201 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001202 case With_kind:
1203 if (!symtable_new_tmpname(st))
1204 return 0;
1205 VISIT(st, expr, s->v.With.context_expr);
1206 if (s->v.With.optional_vars) {
1207 if (!symtable_new_tmpname(st))
1208 return 0;
1209 VISIT(st, expr, s->v.With.optional_vars);
1210 }
1211 VISIT_SEQ(st, stmt, s->v.With.body);
1212 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 }
1214 return 1;
1215}
1216
1217static int
1218symtable_visit_expr(struct symtable *st, expr_ty e)
1219{
1220 switch (e->kind) {
1221 case BoolOp_kind:
1222 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1223 break;
1224 case BinOp_kind:
1225 VISIT(st, expr, e->v.BinOp.left);
1226 VISIT(st, expr, e->v.BinOp.right);
1227 break;
1228 case UnaryOp_kind:
1229 VISIT(st, expr, e->v.UnaryOp.operand);
1230 break;
1231 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001232 if (!GET_IDENTIFIER(lambda) ||
1233 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 return 0;
1235 if (e->v.Lambda.args->defaults)
1236 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1237 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001238 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239 FunctionBlock, (void *)e, 0))
1240 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001241 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1242 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 if (!symtable_exit_block(st, (void *)e))
1244 return 0;
1245 break;
1246 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001247 case IfExp_kind:
1248 VISIT(st, expr, e->v.IfExp.test);
1249 VISIT(st, expr, e->v.IfExp.body);
1250 VISIT(st, expr, e->v.IfExp.orelse);
1251 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 case Dict_kind:
1253 VISIT_SEQ(st, expr, e->v.Dict.keys);
1254 VISIT_SEQ(st, expr, e->v.Dict.values);
1255 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001256 case Set_kind:
1257 VISIT_SEQ(st, expr, e->v.Set.elts);
1258 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001259 case GeneratorExp_kind:
1260 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001263 case ListComp_kind:
1264 if (!symtable_visit_listcomp(st, e))
1265 return 0;
1266 break;
1267 case SetComp_kind:
1268 if (!symtable_visit_setcomp(st, e))
1269 return 0;
1270 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001271 case DictComp_kind:
1272 if (!symtable_visit_dictcomp(st, e))
1273 return 0;
1274 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 case Yield_kind:
1276 if (e->v.Yield.value)
1277 VISIT(st, expr, e->v.Yield.value);
1278 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001279 if (st->st_cur->ste_returns_value) {
1280 PyErr_SetString(PyExc_SyntaxError,
1281 RETURN_VAL_IN_GENERATOR);
1282 PyErr_SyntaxLocation(st->st_filename,
1283 e->lineno);
1284 return 0;
1285 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 break;
1287 case Compare_kind:
1288 VISIT(st, expr, e->v.Compare.left);
1289 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1290 break;
1291 case Call_kind:
1292 VISIT(st, expr, e->v.Call.func);
1293 VISIT_SEQ(st, expr, e->v.Call.args);
1294 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1295 if (e->v.Call.starargs)
1296 VISIT(st, expr, e->v.Call.starargs);
1297 if (e->v.Call.kwargs)
1298 VISIT(st, expr, e->v.Call.kwargs);
1299 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 case Num_kind:
1301 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001302 case Bytes_kind:
1303 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 /* Nothing to do here. */
1305 break;
1306 /* The following exprs can be assignment targets. */
1307 case Attribute_kind:
1308 VISIT(st, expr, e->v.Attribute.value);
1309 break;
1310 case Subscript_kind:
1311 VISIT(st, expr, e->v.Subscript.value);
1312 VISIT(st, slice, e->v.Subscript.slice);
1313 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001314 case Starred_kind:
1315 VISIT(st, expr, e->v.Starred.value);
1316 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 case Name_kind:
1318 if (!symtable_add_def(st, e->v.Name.id,
1319 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1320 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001321 /* Special-case super: it counts as a use of __class__ */
1322 if (e->v.Name.ctx == Load &&
1323 st->st_cur->ste_type == FunctionBlock &&
1324 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1325 if (!GET_IDENTIFIER(__class__) ||
1326 !symtable_add_def(st, __class__, USE))
1327 return 0;
1328 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 break;
1330 /* child nodes of List and Tuple will have expr_context set */
1331 case List_kind:
1332 VISIT_SEQ(st, expr, e->v.List.elts);
1333 break;
1334 case Tuple_kind:
1335 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1336 break;
1337 }
1338 return 1;
1339}
1340
1341static int
1342symtable_implicit_arg(struct symtable *st, int pos)
1343{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001344 PyObject *id = PyUnicode_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 if (id == NULL)
1346 return 0;
1347 if (!symtable_add_def(st, id, DEF_PARAM)) {
1348 Py_DECREF(id);
1349 return 0;
1350 }
1351 Py_DECREF(id);
1352 return 1;
1353}
1354
1355static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001356symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001358 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001359
1360 if (!args)
1361 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001364 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001365 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366 return 0;
1367 }
1368
1369 return 1;
1370}
1371
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001372static int
1373symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374{
1375 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001376
1377 if (!args)
1378 return -1;
1379
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001381 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001382 if (arg->annotation)
1383 VISIT(st, expr, arg->annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001385
1386 return 1;
1387}
1388
Neal Norwitzc1505362006-12-28 06:47:50 +00001389static int
1390symtable_visit_annotations(struct symtable *st, stmt_ty s)
1391{
1392 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001394 if (a->args && !symtable_visit_argannotations(st, a->args))
Neal Norwitzc1505362006-12-28 06:47:50 +00001395 return 0;
1396 if (a->varargannotation)
1397 VISIT(st, expr, a->varargannotation);
1398 if (a->kwargannotation)
1399 VISIT(st, expr, a->kwargannotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001400 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
Neal Norwitzc1505362006-12-28 06:47:50 +00001401 return 0;
1402 if (s->v.FunctionDef.returns)
1403 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 return 1;
1405}
1406
1407static int
1408symtable_visit_arguments(struct symtable *st, arguments_ty a)
1409{
1410 /* skip default arguments inside function block
1411 XXX should ast be different?
1412 */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001413 if (a->args && !symtable_visit_params(st, a->args))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001415 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001416 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 if (a->vararg) {
1418 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1419 return 0;
1420 st->st_cur->ste_varargs = 1;
1421 }
1422 if (a->kwarg) {
1423 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1424 return 0;
1425 st->st_cur->ste_varkeywords = 1;
1426 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 return 1;
1428}
1429
1430
1431static int
1432symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1433{
Neal Norwitzad74aa82008-03-31 05:14:30 +00001434 if (eh->v.ExceptHandler.type)
1435 VISIT(st, expr, eh->v.ExceptHandler.type);
1436 if (eh->v.ExceptHandler.name)
1437 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
Guido van Rossum16be03e2007-01-10 18:51:35 +00001438 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00001439 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 return 1;
1441}
1442
1443
1444static int
1445symtable_visit_alias(struct symtable *st, alias_ty a)
1446{
1447 /* Compute store_name, the name actually bound by the import
1448 operation. It is diferent than a->name when a->name is a
1449 dotted package name (e.g. spam.eggs)
1450 */
1451 PyObject *store_name;
1452 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001453 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1454 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001455 if (dot) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001456 store_name = PyUnicode_FromUnicode(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001457 if (!store_name)
1458 return 0;
1459 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460 else {
1461 store_name = name;
1462 Py_INCREF(store_name);
1463 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001464 if (PyUnicode_CompareWithASCIIString(name, "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1466 Py_DECREF(store_name);
1467 return r;
1468 }
1469 else {
1470 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001471 int lineno = st->st_cur->ste_lineno;
Guido van Rossum33d26892007-08-05 15:29:28 +00001472 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1473 PyErr_SyntaxLocation(st->st_filename, lineno);
1474 Py_DECREF(store_name);
1475 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 }
1477 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001478 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 return 1;
1480 }
1481}
1482
1483
1484static int
1485symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1486{
1487 VISIT(st, expr, lc->target);
1488 VISIT(st, expr, lc->iter);
1489 VISIT_SEQ(st, expr, lc->ifs);
1490 return 1;
1491}
1492
1493
1494static int
1495symtable_visit_keyword(struct symtable *st, keyword_ty k)
1496{
1497 VISIT(st, expr, k->value);
1498 return 1;
1499}
1500
1501
1502static int
1503symtable_visit_slice(struct symtable *st, slice_ty s)
1504{
1505 switch (s->kind) {
1506 case Slice_kind:
1507 if (s->v.Slice.lower)
1508 VISIT(st, expr, s->v.Slice.lower)
1509 if (s->v.Slice.upper)
1510 VISIT(st, expr, s->v.Slice.upper)
1511 if (s->v.Slice.step)
1512 VISIT(st, expr, s->v.Slice.step)
1513 break;
1514 case ExtSlice_kind:
1515 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1516 break;
1517 case Index_kind:
1518 VISIT(st, expr, s->v.Index.value)
1519 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520 }
1521 return 1;
1522}
1523
1524static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001525symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001526 identifier scope_name, asdl_seq *generators,
1527 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001529 int is_generator = (e->kind == GeneratorExp_kind);
1530 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001532 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 /* Outermost iterator is evaluated in current scope */
1534 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001535 /* Create comprehension scope for the rest */
1536 if (!scope_name ||
1537 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 return 0;
1539 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001540 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 /* Outermost iter is received as an argument */
1542 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001543 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544 return 0;
1545 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001546 /* Allocate temporary name if needed */
1547 if (needs_tmp && !symtable_new_tmpname(st)) {
1548 symtable_exit_block(st, (void *)e);
1549 return 0;
1550 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001551 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1552 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1553 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001554 generators, 1, (void*)e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001555 if (value)
1556 VISIT_IN_BLOCK(st, expr, value, (void*)e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001557 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001558 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001560
1561static int
1562symtable_visit_genexp(struct symtable *st, expr_ty e)
1563{
1564 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1565 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001566 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001567}
1568
1569static int
1570symtable_visit_listcomp(struct symtable *st, expr_ty e)
1571{
1572 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1573 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001574 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001575}
1576
1577static int
1578symtable_visit_setcomp(struct symtable *st, expr_ty e)
1579{
1580 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1581 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001582 e->v.SetComp.elt, NULL);
1583}
1584
1585static int
1586symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1587{
1588 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1589 e->v.DictComp.generators,
1590 e->v.DictComp.key,
1591 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001592}