blob: 732b85cf178fd3a2b8deed736675a77d92c8b4cd [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
Benjamin Peterson55e00f22008-08-17 18:02:44 +000025
Neal Norwitz090b3dd2006-02-28 22:36:46 +000026static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000027ste_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},
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000117 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
118 {"nested", T_INT, OFF(ste_nested), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119 {"type", T_INT, OFF(ste_type), READONLY},
120 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121 {NULL}
122};
123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124PyTypeObject PySTEntry_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000125 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000128 0,
129 (destructor)ste_dealloc, /* tp_dealloc */
130 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000131 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000132 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000133 0, /* tp_reserved */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000134 (reprfunc)ste_repr, /* tp_repr */
135 0, /* tp_as_number */
136 0, /* tp_as_sequence */
137 0, /* tp_as_mapping */
138 0, /* tp_hash */
139 0, /* tp_call */
140 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000141 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000142 0, /* tp_setattro */
143 0, /* tp_as_buffer */
144 Py_TPFLAGS_DEFAULT, /* tp_flags */
145 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000146 0, /* tp_traverse */
147 0, /* tp_clear */
148 0, /* tp_richcompare */
149 0, /* tp_weaklistoffset */
150 0, /* tp_iter */
151 0, /* tp_iternext */
152 0, /* tp_methods */
153 ste_memberlist, /* tp_members */
154 0, /* tp_getset */
155 0, /* tp_base */
156 0, /* tp_dict */
157 0, /* tp_descr_get */
158 0, /* tp_descr_set */
159 0, /* tp_dictoffset */
160 0, /* tp_init */
161 0, /* tp_alloc */
162 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000163};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164
165static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000166static int symtable_warn(struct symtable *st, char *msg, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_enter_block(struct symtable *st, identifier name,
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000168 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169static int symtable_exit_block(struct symtable *st, void *ast);
170static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
171static int symtable_visit_expr(struct symtable *st, expr_ty s);
172static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000173static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
174static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000175static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000176static int symtable_visit_arguments(struct symtable *st, arguments_ty);
177static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
178static int symtable_visit_alias(struct symtable *st, alias_ty);
179static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
180static int symtable_visit_keyword(struct symtable *st, keyword_ty);
181static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000182static int symtable_visit_params(struct symtable *st, asdl_seq *args);
183static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000185static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186
187
Nick Coghlan650f0d02007-04-15 12:05:43 +0000188static identifier top = NULL, lambda = NULL, genexpr = NULL,
Benjamin Petersondcaf3292009-03-03 00:54:05 +0000189 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
190 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
192#define GET_IDENTIFIER(VAR) \
Martin v. Löwis5b222132007-06-10 09:51:05 +0000193 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194
195#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000196"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
198static struct symtable *
199symtable_new(void)
200{
201 struct symtable *st;
202
203 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
204 if (st == NULL)
205 return NULL;
206
207 st->st_filename = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000208 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000209
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210 if ((st->st_stack = PyList_New(0)) == NULL)
211 goto fail;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000212 if ((st->st_blocks = PyDict_New()) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213 goto fail;
214 st->st_cur = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215 st->st_private = NULL;
216 return st;
217 fail:
218 PySymtable_Free(st);
219 return NULL;
220}
221
222struct symtable *
223PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
224{
225 struct symtable *st = symtable_new();
226 asdl_seq *seq;
227 int i;
228
229 if (st == NULL)
230 return st;
231 st->st_filename = filename;
232 st->st_future = future;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000233 /* Make the initial symbol information gathering pass */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000234 if (!GET_IDENTIFIER(top) ||
235 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000236 PySymtable_Free(st);
237 return NULL;
238 }
239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240 st->st_top = st->st_cur;
241 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242 switch (mod->kind) {
243 case Module_kind:
244 seq = mod->v.Module.body;
245 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246 if (!symtable_visit_stmt(st,
247 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 goto error;
249 break;
250 case Expression_kind:
251 if (!symtable_visit_expr(st, mod->v.Expression.body))
252 goto error;
253 break;
254 case Interactive_kind:
255 seq = mod->v.Interactive.body;
256 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257 if (!symtable_visit_stmt(st,
258 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259 goto error;
260 break;
261 case Suite_kind:
262 PyErr_SetString(PyExc_RuntimeError,
263 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000264 goto error;
265 }
266 if (!symtable_exit_block(st, (void *)mod)) {
267 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 return NULL;
269 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000270 /* Make the second symbol analysis pass */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271 if (symtable_analyze(st))
272 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000273 PySymtable_Free(st);
274 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000276 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277 PySymtable_Free(st);
278 return NULL;
279}
280
281void
282PySymtable_Free(struct symtable *st)
283{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000284 Py_XDECREF(st->st_blocks);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 Py_XDECREF(st->st_stack);
286 PyMem_Free((void *)st);
287}
288
289PySTEntryObject *
290PySymtable_Lookup(struct symtable *st, void *key)
291{
292 PyObject *k, *v;
293
294 k = PyLong_FromVoidPtr(key);
295 if (k == NULL)
296 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000297 v = PyDict_GetItem(st->st_blocks, k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298 if (v) {
299 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301 }
302 else {
303 PyErr_SetString(PyExc_KeyError,
304 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000306
307 Py_DECREF(k);
308 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309}
310
311int
312PyST_GetScope(PySTEntryObject *ste, PyObject *name)
313{
314 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
315 if (!v)
316 return 0;
Christian Heimes217cfd12007-12-02 14:31:20 +0000317 assert(PyLong_Check(v));
318 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319}
320
321
322/* Analyze raw symbol information to determine scope of each name.
323
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000324 The next several functions are helpers for symtable_analyze(),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325 which determines whether a name is local, global, or free. In addition,
326 it determines which local variables are cell variables; they provide
327 bindings that are used for free variables in enclosed blocks.
328
Nick Coghlan650f0d02007-04-15 12:05:43 +0000329 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330 explicit global is declared with the global statement. An implicit
331 global is a free variable for which the compiler has found no binding
332 in an enclosing function scope. The implicit global is either a global
333 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
334 to handle these names to implement slightly odd semantics. In such a
335 block, the name is treated as global until it is assigned to; then it
336 is treated as a local.
337
338 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000339 The first pass collects raw facts from the AST via the symtable_visit_*
340 functions: the name is a parameter here, the name is used but not defined
341 here, etc. The second pass analyzes these facts during a pass over the
342 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343
344 When a function is entered during the second pass, the parent passes
345 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000346 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000347 Names which are explicitly declared nonlocal must exist in this set of
348 visible names - if they do not, a syntax error is raised. After doing
349 the local analysis, it analyzes each of its child blocks using an
350 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351
Nick Coghlan650f0d02007-04-15 12:05:43 +0000352 The children update the free variable set. If a local variable is added to
353 the free variable set by the child, the variable is marked as a cell. The
354 function object being defined must provide runtime storage for the variable
355 that may outlive the function's frame. Cell variables are removed from the
356 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000357
Nick Coghlan650f0d02007-04-15 12:05:43 +0000358 During analysis, the names are:
359 symbols: dict mapping from symbol names to flag values (including offset scope values)
360 scopes: dict mapping from symbol names to scope values (no offset)
361 local: set of all symbol names local to the current scope
362 bound: set of all symbol names local to a containing function scope
363 free: set of all symbol names referenced but not bound in child scopes
364 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365*/
366
367#define SET_SCOPE(DICT, NAME, I) { \
Christian Heimes217cfd12007-12-02 14:31:20 +0000368 PyObject *o = PyLong_FromLong(I); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369 if (!o) \
370 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000371 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
372 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000374 } \
375 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376}
377
378/* Decide on scope of name, given flags.
379
380 The dicts passed in as arguments are modified as necessary.
381 ste is passed so that flags can be updated.
382*/
383
384static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000385analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386 PyObject *bound, PyObject *local, PyObject *free,
387 PyObject *global)
388{
389 if (flags & DEF_GLOBAL) {
390 if (flags & DEF_PARAM) {
391 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000392 "name '%U' is parameter and global",
393 name);
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000394 PyErr_SyntaxLocation(ste->ste_table->st_filename,
395 ste->ste_lineno);
396
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397 return 0;
398 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000399 if (flags & DEF_NONLOCAL) {
400 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000401 "name '%U' is nonlocal and global",
402 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000403 return 0;
404 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000405 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
406 if (PySet_Add(global, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000408 if (bound && (PySet_Discard(bound, name) < 0))
409 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410 return 1;
411 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000412 if (flags & DEF_NONLOCAL) {
413 if (flags & DEF_PARAM) {
414 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000415 "name '%U' is parameter and nonlocal",
416 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000417 return 0;
418 }
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000419 if (!bound) {
420 PyErr_Format(PyExc_SyntaxError,
421 "nonlocal declaration not allowed at module level");
422 return 0;
423 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000424 if (!PySet_Contains(bound, name)) {
Jeremy Hylton81e95022007-02-27 06:50:52 +0000425 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000426 "no binding for nonlocal '%U' found",
427 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000428
429 return 0;
430 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000431 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000432 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000433 return PySet_Add(free, name) >= 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000434 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435 if (flags & DEF_BOUND) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000436 SET_SCOPE(scopes, name, LOCAL);
437 if (PySet_Add(local, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000438 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000439 if (PySet_Discard(global, name) < 0)
440 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441 return 1;
442 }
443 /* If an enclosing block has a binding for this name, it
444 is a free variable rather than a global variable.
445 Note that having a non-NULL bound implies that the block
446 is nested.
447 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000448 if (bound && PySet_Contains(bound, name)) {
449 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000451 return PySet_Add(free, name) >= 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 }
453 /* If a parent has a global statement, then call it global
454 explicit? It could also be global implicit.
455 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000456 if (global && PySet_Contains(global, name)) {
457 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458 return 1;
459 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000460 if (ste->ste_nested)
461 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000462 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000463 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464}
465
466#undef SET_SCOPE
467
468/* If a name is defined in free and also in locals, then this block
469 provides the binding for the free variable. The name should be
470 marked CELL in this block and removed from the free list.
471
472 Note that the current block's free variables are included in free.
473 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000474
Martin v. Löwis2673a572007-10-29 19:54:24 +0000475 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000476 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477*/
478
479static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000480analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000482 PyObject *name, *v, *v_cell;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000483 int success = 0;
484 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485
Christian Heimes217cfd12007-12-02 14:31:20 +0000486 v_cell = PyLong_FromLong(CELL);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000487 if (!v_cell)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000489 while (PyDict_Next(scopes, &pos, &name, &v)) {
490 long scope;
Christian Heimes217cfd12007-12-02 14:31:20 +0000491 assert(PyLong_Check(v));
492 scope = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000493 if (scope != LOCAL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494 continue;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000495 if (!PySet_Contains(free, name))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496 continue;
Martin v. Löwis2673a572007-10-29 19:54:24 +0000497 if (restricted != NULL &&
498 PyUnicode_CompareWithASCIIString(name, restricted))
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000499 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 /* Replace LOCAL with CELL for this name, and remove
501 from free. It is safe to replace the value of name
502 in the dict, because it will not cause a resize.
503 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000504 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000505 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000506 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507 goto error;
508 }
509 success = 1;
510 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000511 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 return success;
513}
514
515/* Check for illegal statements in unoptimized namespaces */
516static int
517check_unoptimized(const PySTEntryObject* ste) {
Armin Rigo31441302005-10-21 12:57:31 +0000518 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000520 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 || !(ste->ste_free || ste->ste_child_free))
522 return 1;
523
Armin Rigo31441302005-10-21 12:57:31 +0000524 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525 "contains a nested function with free variables" :
526 "is a nested function");
527
528 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000529 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530 return 1;
531 case OPT_IMPORT_STAR:
Walter Dörwald3ef72bb2007-06-11 16:12:10 +0000532 PyErr_Format(PyExc_SyntaxError,
533 "import * is not allowed in function '%U' because it %s",
Walter Dörwalde42109d2007-06-11 16:08:41 +0000534 ste->ste_name, trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536 }
537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 PyErr_SyntaxLocation(ste->ste_table->st_filename,
539 ste->ste_opt_lineno);
540 return 0;
541}
542
Nick Coghlan650f0d02007-04-15 12:05:43 +0000543/* Enter the final scope information into the ste_symbols dict.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544 *
545 * All arguments are dicts. Modifies symbols, others are read-only.
546*/
547static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000548update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000551 PyObject *name = NULL, *itr = NULL;
552 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000553 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554
Nick Coghlan650f0d02007-04-15 12:05:43 +0000555 /* Update scope information for all symbols in this scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 while (PyDict_Next(symbols, &pos, &name, &v)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000557 long scope, flags;
Christian Heimes217cfd12007-12-02 14:31:20 +0000558 assert(PyLong_Check(v));
559 flags = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000560 v_scope = PyDict_GetItem(scopes, name);
Christian Heimes217cfd12007-12-02 14:31:20 +0000561 assert(v_scope && PyLong_Check(v_scope));
562 scope = PyLong_AS_LONG(v_scope);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000563 flags |= (scope << SCOPE_OFFSET);
Christian Heimes217cfd12007-12-02 14:31:20 +0000564 v_new = PyLong_FromLong(flags);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000565 if (!v_new)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000566 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000567 if (PyDict_SetItem(symbols, name, v_new) < 0) {
568 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569 return 0;
570 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000571 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572 }
573
Nick Coghlan650f0d02007-04-15 12:05:43 +0000574 /* Record not yet resolved free variables from children (if any) */
Christian Heimes217cfd12007-12-02 14:31:20 +0000575 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000576 if (!v_free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577 return 0;
578
Nick Coghlan650f0d02007-04-15 12:05:43 +0000579 itr = PyObject_GetIter(free);
580 if (!itr)
581 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582
Nick Coghlan650f0d02007-04-15 12:05:43 +0000583 while ((name = PyIter_Next(itr))) {
584 v = PyDict_GetItem(symbols, name);
585
586 /* Handle symbol that already exists in this scope */
587 if (v) {
588 /* Handle a free variable in a method of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589 the class that has the same name as a local
590 or global in the class scope.
591 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592 if (classflag &&
Christian Heimes217cfd12007-12-02 14:31:20 +0000593 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
594 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
595 v_new = PyLong_FromLong(flags);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000596 if (!v_new) {
597 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000599 if (PyDict_SetItem(symbols, name, v_new) < 0) {
600 Py_DECREF(v_new);
601 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000603 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000605 /* It's a cell, or already free in this scope */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000606 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607 continue;
608 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000609 /* Handle global symbol */
610 if (!PySet_Contains(bound, name)) {
611 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 continue; /* it's a global */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000614 /* Propagate new free symbol up the lexical stack */
615 if (PyDict_SetItem(symbols, name, v_free) < 0) {
616 goto error;
617 }
618 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000620 Py_DECREF(itr);
621 Py_DECREF(v_free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000623error:
624 Py_XDECREF(v_free);
625 Py_XDECREF(itr);
626 Py_XDECREF(name);
627 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628}
629
630/* Make final symbol table decisions for block of ste.
631 Arguments:
632 ste -- current symtable entry (input/output)
633 bound -- set of variables bound in enclosing scopes (input)
634 free -- set of free variables in enclosed scopes (output)
635 globals -- set of declared global variables in enclosing scopes (input)
636*/
637
638static int
639analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
640 PyObject *global)
641{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000642 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000644 int i, success = 0;
645 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646
Nick Coghlan650f0d02007-04-15 12:05:43 +0000647 scopes = PyDict_New();
648 if (!scopes)
649 goto error;
650 local = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651 if (!local)
652 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000653 newglobal = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654 if (!newglobal)
655 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000656 newfree = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657 if (!newfree)
658 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000659 newbound = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660 if (!newbound)
661 goto error;
662
Nick Coghlan650f0d02007-04-15 12:05:43 +0000663 /* Class namespace has no effect on names visible in
664 nested functions, so populate the global and bound
665 sets to be passed to child blocks before analyzing
666 this one.
667 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 if (ste->ste_type == ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000669 /* Pass down previously bound symbols */
670 if (bound) {
671 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000673 Py_DECREF(newbound);
674 }
675 /* Pass down known globals */
676 if (!PyNumber_InPlaceOr(newglobal, global))
677 goto error;
678 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 }
680
Nick Coghlan650f0d02007-04-15 12:05:43 +0000681 /* Analyze symbols in current scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 assert(PySTEntry_Check(ste));
683 assert(PyDict_Check(ste->ste_symbols));
684 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000685 long flags = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000686 if (!analyze_name(ste, scopes, name, flags, bound, local, free,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 global))
688 goto error;
689 }
690
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000691 /* Populate global and bound sets to be passed to children. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000693 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694 if (ste->ste_type == FunctionBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000695 if (!PyNumber_InPlaceOr(newbound, local))
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 previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 if (bound) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000701 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000703 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000705 /* Pass down known globals */
706 if (!PyNumber_InPlaceOr(newglobal, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000708 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000710 else {
711 /* Special-case __class__ */
712 if (!GET_IDENTIFIER(__class__))
713 goto error;
714 assert(PySet_Contains(local, __class__) == 1);
715 if (PySet_Add(newbound, __class__) < 0)
716 goto error;
717 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718
719 /* Recursively call analyze_block() on each child block */
720 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
721 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000722 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000724 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725 if (!analyze_block(entry, newbound, newfree, newglobal))
726 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000727 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728 if (entry->ste_free || entry->ste_child_free)
729 ste->ste_child_free = 1;
730 }
731
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000732 /* Check if any local variables must be converted to cell variables */
733 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
734 NULL))
735 goto error;
736 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
737 "__class__"))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000739 /* Records the results of the analysis in the symbol table entry */
740 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741 ste->ste_type == ClassBlock))
742 goto error;
743 if (!check_unoptimized(ste))
744 goto error;
745
Nick Coghlan650f0d02007-04-15 12:05:43 +0000746 if (!PyNumber_InPlaceOr(free, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000748 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 success = 1;
750 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000751 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 Py_XDECREF(newbound);
754 Py_XDECREF(newglobal);
755 Py_XDECREF(newfree);
756 if (!success)
757 assert(PyErr_Occurred());
758 return success;
759}
760
761static int
762symtable_analyze(struct symtable *st)
763{
764 PyObject *free, *global;
765 int r;
766
Nick Coghlan650f0d02007-04-15 12:05:43 +0000767 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 if (!free)
769 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000770 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000772 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 return 0;
774 }
775 r = analyze_block(st->st_top, NULL, free, global);
776 Py_DECREF(free);
777 Py_DECREF(global);
778 return r;
779}
780
781
782static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000783symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784{
785 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000786 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
788 PyErr_SetString(PyExc_SyntaxError, msg);
789 PyErr_SyntaxLocation(st->st_filename,
790 st->st_cur->ste_lineno);
791 }
792 return 0;
793 }
794 return 1;
795}
796
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000797/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 This reference is released when the block is exited, via the DECREF
799 in symtable_exit_block().
800*/
801
802static int
803symtable_exit_block(struct symtable *st, void *ast)
804{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000805 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000807 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808 end = PyList_GET_SIZE(st->st_stack) - 1;
809 if (end >= 0) {
810 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
811 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000812 if (st->st_cur == NULL)
813 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 Py_INCREF(st->st_cur);
815 if (PySequence_DelItem(st->st_stack, end) < 0)
816 return 0;
817 }
818 return 1;
819}
820
821static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000822symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000823 void *ast, int lineno)
824{
825 PySTEntryObject *prev = NULL;
826
827 if (st->st_cur) {
828 prev = st->st_cur;
829 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830 return 0;
831 }
832 Py_DECREF(st->st_cur);
833 }
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000834 st->st_cur = ste_new(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000835 if (st->st_cur == NULL)
836 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 if (name == GET_IDENTIFIER(top))
838 st->st_global = st->st_cur->ste_symbols;
839 if (prev) {
840 if (PyList_Append(prev->ste_children,
841 (PyObject *)st->st_cur) < 0) {
842 return 0;
843 }
844 }
845 return 1;
846}
847
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000848static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849symtable_lookup(struct symtable *st, PyObject *name)
850{
851 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000852 PyObject *mangled = _Py_Mangle(st->st_private, name);
853 if (!mangled)
854 return 0;
855 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
856 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 if (!o)
858 return 0;
Christian Heimes217cfd12007-12-02 14:31:20 +0000859 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860}
861
862static int
863symtable_add_def(struct symtable *st, PyObject *name, int flag)
864{
865 PyObject *o;
866 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000867 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000868 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869
Jeremy Hylton81e95022007-02-27 06:50:52 +0000870
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000871 if (!mangled)
872 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000874 if ((o = PyDict_GetItem(dict, mangled))) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000875 val = PyLong_AS_LONG(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000877 /* Is it better to use 'mangled' or 'name' here? */
Neal Norwitza5d16a32007-08-24 22:53:58 +0000878 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879 PyErr_SyntaxLocation(st->st_filename,
880 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000881 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 }
883 val |= flag;
884 } else
885 val = flag;
Christian Heimes217cfd12007-12-02 14:31:20 +0000886 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000888 goto error;
889 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000891 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 }
893 Py_DECREF(o);
894
895 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000896 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
897 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898 } else if (flag & DEF_GLOBAL) {
899 /* XXX need to update DEF_GLOBAL for other flags too;
900 perhaps only DEF_FREE_GLOBAL */
901 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000902 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000903 val |= PyLong_AS_LONG(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000905 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000907 goto error;
908 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000910 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911 }
912 Py_DECREF(o);
913 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000914 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000916
917error:
918 Py_DECREF(mangled);
919 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920}
921
922/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
923 They use the ASDL name to synthesize the name of the C type and the visit
924 function.
925
926 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
927 useful if the first node in the sequence requires special treatment.
928*/
929
930#define VISIT(ST, TYPE, V) \
931 if (!symtable_visit_ ## TYPE((ST), (V))) \
932 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000933
934#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
935 if (!symtable_visit_ ## TYPE((ST), (V))) { \
936 symtable_exit_block((ST), (S)); \
937 return 0; \
938 }
939
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940#define VISIT_SEQ(ST, TYPE, SEQ) { \
941 int i; \
942 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
943 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000944 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945 if (!symtable_visit_ ## TYPE((ST), elt)) \
946 return 0; \
947 } \
948}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000949
950#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
951 int i; \
952 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
953 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000954 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000955 if (!symtable_visit_ ## TYPE((ST), elt)) { \
956 symtable_exit_block((ST), (S)); \
957 return 0; \
958 } \
959 } \
960}
961
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
963 int i; \
964 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
965 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000966 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967 if (!symtable_visit_ ## TYPE((ST), elt)) \
968 return 0; \
969 } \
970}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000971
972#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
973 int i; \
974 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
975 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000976 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000977 if (!symtable_visit_ ## TYPE((ST), elt)) { \
978 symtable_exit_block((ST), (S)); \
979 return 0; \
980 } \
981 } \
982}
983
Guido van Rossum4f72a782006-10-27 23:31:49 +0000984#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
985 int i = 0; \
986 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
987 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
988 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
989 if (!elt) continue; /* can be NULL */ \
990 if (!symtable_visit_expr((ST), elt)) \
991 return 0; \
992 } \
993}
994
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000996symtable_new_tmpname(struct symtable *st)
997{
998 char tmpname[256];
999 identifier tmp;
1000
1001 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1002 ++st->st_cur->ste_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001003 tmp = PyUnicode_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001004 if (!tmp)
1005 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001006 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1007 return 0;
1008 Py_DECREF(tmp);
1009 return 1;
1010}
1011
Guido van Rossum4f72a782006-10-27 23:31:49 +00001012
1013
Guido van Rossumc2e20742006-02-27 22:32:47 +00001014static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015symtable_visit_stmt(struct symtable *st, stmt_ty s)
1016{
1017 switch (s->kind) {
1018 case FunctionDef_kind:
1019 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1020 return 0;
1021 if (s->v.FunctionDef.args->defaults)
1022 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001023 if (s->v.FunctionDef.args->kw_defaults)
1024 VISIT_KWONLYDEFAULTS(st,
1025 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001026 if (!symtable_visit_annotations(st, s))
1027 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001028 if (s->v.FunctionDef.decorator_list)
1029 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1031 FunctionBlock, (void *)s, s->lineno))
1032 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001033 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1034 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 if (!symtable_exit_block(st, s))
1036 return 0;
1037 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001038 case ClassDef_kind: {
1039 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1041 return 0;
1042 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001043 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1044 if (s->v.ClassDef.starargs)
1045 VISIT(st, expr, s->v.ClassDef.starargs);
1046 if (s->v.ClassDef.kwargs)
1047 VISIT(st, expr, s->v.ClassDef.kwargs);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001048 if (s->v.ClassDef.decorator_list)
1049 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1051 (void *)s, s->lineno))
1052 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001053 if (!GET_IDENTIFIER(__class__) ||
Benjamin Petersondcaf3292009-03-03 00:54:05 +00001054 !symtable_add_def(st, __class__, DEF_LOCAL) ||
1055 !GET_IDENTIFIER(__locals__) ||
1056 !symtable_add_def(st, __locals__, DEF_PARAM)) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001057 symtable_exit_block(st, s);
1058 return 0;
1059 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001060 tmp = st->st_private;
1061 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001062 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001063 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064 if (!symtable_exit_block(st, s))
1065 return 0;
1066 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001067 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001069 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001071 st->st_cur->ste_returns_value = 1;
1072 if (st->st_cur->ste_generator) {
1073 PyErr_SetString(PyExc_SyntaxError,
1074 RETURN_VAL_IN_GENERATOR);
1075 PyErr_SyntaxLocation(st->st_filename,
1076 s->lineno);
1077 return 0;
1078 }
1079 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 break;
1081 case Delete_kind:
1082 VISIT_SEQ(st, expr, s->v.Delete.targets);
1083 break;
1084 case Assign_kind:
1085 VISIT_SEQ(st, expr, s->v.Assign.targets);
1086 VISIT(st, expr, s->v.Assign.value);
1087 break;
1088 case AugAssign_kind:
1089 VISIT(st, expr, s->v.AugAssign.target);
1090 VISIT(st, expr, s->v.AugAssign.value);
1091 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092 case For_kind:
1093 VISIT(st, expr, s->v.For.target);
1094 VISIT(st, expr, s->v.For.iter);
1095 VISIT_SEQ(st, stmt, s->v.For.body);
1096 if (s->v.For.orelse)
1097 VISIT_SEQ(st, stmt, s->v.For.orelse);
1098 break;
1099 case While_kind:
1100 VISIT(st, expr, s->v.While.test);
1101 VISIT_SEQ(st, stmt, s->v.While.body);
1102 if (s->v.While.orelse)
1103 VISIT_SEQ(st, stmt, s->v.While.orelse);
1104 break;
1105 case If_kind:
1106 /* XXX if 0: and lookup_yield() hacks */
1107 VISIT(st, expr, s->v.If.test);
1108 VISIT_SEQ(st, stmt, s->v.If.body);
1109 if (s->v.If.orelse)
1110 VISIT_SEQ(st, stmt, s->v.If.orelse);
1111 break;
1112 case Raise_kind:
Collin Winter828f04a2007-08-31 00:04:24 +00001113 if (s->v.Raise.exc) {
1114 VISIT(st, expr, s->v.Raise.exc);
1115 if (s->v.Raise.cause) {
1116 VISIT(st, expr, s->v.Raise.cause);
1117 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118 }
1119 break;
1120 case TryExcept_kind:
1121 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1122 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1123 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1124 break;
1125 case TryFinally_kind:
1126 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1127 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1128 break;
1129 case Assert_kind:
1130 VISIT(st, expr, s->v.Assert.test);
1131 if (s->v.Assert.msg)
1132 VISIT(st, expr, s->v.Assert.msg);
1133 break;
1134 case Import_kind:
1135 VISIT_SEQ(st, alias, s->v.Import.names);
1136 /* XXX Don't have the lineno available inside
1137 visit_alias */
1138 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1139 st->st_cur->ste_opt_lineno = s->lineno;
1140 break;
1141 case ImportFrom_kind:
1142 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1143 /* XXX Don't have the lineno available inside
1144 visit_alias */
1145 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1146 st->st_cur->ste_opt_lineno = s->lineno;
1147 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 case Global_kind: {
1149 int i;
1150 asdl_seq *seq = s->v.Global.names;
1151 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152 identifier name = (identifier)asdl_seq_GET(seq, i);
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001153 char *c_name = _PyUnicode_AsString(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001154 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 if (cur < 0)
1156 return 0;
1157 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001158 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 if (cur & DEF_LOCAL)
1160 PyOS_snprintf(buf, sizeof(buf),
1161 GLOBAL_AFTER_ASSIGN,
1162 c_name);
1163 else
1164 PyOS_snprintf(buf, sizeof(buf),
1165 GLOBAL_AFTER_USE,
1166 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001167 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 return 0;
1169 }
1170 if (!symtable_add_def(st, name, DEF_GLOBAL))
1171 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 break;
1174 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001175 case Nonlocal_kind: {
1176 int i;
1177 asdl_seq *seq = s->v.Nonlocal.names;
1178 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1179 identifier name = (identifier)asdl_seq_GET(seq, i);
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001180 char *c_name = _PyUnicode_AsString(name);
Jeremy Hylton81e95022007-02-27 06:50:52 +00001181 long cur = symtable_lookup(st, name);
1182 if (cur < 0)
1183 return 0;
1184 if (cur & (DEF_LOCAL | USE)) {
1185 char buf[256];
1186 if (cur & DEF_LOCAL)
1187 PyOS_snprintf(buf, sizeof(buf),
1188 NONLOCAL_AFTER_ASSIGN,
1189 c_name);
1190 else
1191 PyOS_snprintf(buf, sizeof(buf),
1192 NONLOCAL_AFTER_USE,
1193 c_name);
1194 if (!symtable_warn(st, buf, s->lineno))
1195 return 0;
1196 }
1197 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1198 return 0;
1199 }
1200 break;
1201 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 case Expr_kind:
1203 VISIT(st, expr, s->v.Expr.value);
1204 break;
1205 case Pass_kind:
1206 case Break_kind:
1207 case Continue_kind:
1208 /* nothing to do here */
1209 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001210 case With_kind:
1211 if (!symtable_new_tmpname(st))
1212 return 0;
1213 VISIT(st, expr, s->v.With.context_expr);
1214 if (s->v.With.optional_vars) {
1215 if (!symtable_new_tmpname(st))
1216 return 0;
1217 VISIT(st, expr, s->v.With.optional_vars);
1218 }
1219 VISIT_SEQ(st, stmt, s->v.With.body);
1220 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 }
1222 return 1;
1223}
1224
1225static int
1226symtable_visit_expr(struct symtable *st, expr_ty e)
1227{
1228 switch (e->kind) {
1229 case BoolOp_kind:
1230 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1231 break;
1232 case BinOp_kind:
1233 VISIT(st, expr, e->v.BinOp.left);
1234 VISIT(st, expr, e->v.BinOp.right);
1235 break;
1236 case UnaryOp_kind:
1237 VISIT(st, expr, e->v.UnaryOp.operand);
1238 break;
1239 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001240 if (!GET_IDENTIFIER(lambda) ||
1241 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 return 0;
1243 if (e->v.Lambda.args->defaults)
1244 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1245 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001246 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247 FunctionBlock, (void *)e, 0))
1248 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001249 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1250 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 if (!symtable_exit_block(st, (void *)e))
1252 return 0;
1253 break;
1254 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001255 case IfExp_kind:
1256 VISIT(st, expr, e->v.IfExp.test);
1257 VISIT(st, expr, e->v.IfExp.body);
1258 VISIT(st, expr, e->v.IfExp.orelse);
1259 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 case Dict_kind:
1261 VISIT_SEQ(st, expr, e->v.Dict.keys);
1262 VISIT_SEQ(st, expr, e->v.Dict.values);
1263 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001264 case Set_kind:
1265 VISIT_SEQ(st, expr, e->v.Set.elts);
1266 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001267 case GeneratorExp_kind:
1268 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001271 case ListComp_kind:
1272 if (!symtable_visit_listcomp(st, e))
1273 return 0;
1274 break;
1275 case SetComp_kind:
1276 if (!symtable_visit_setcomp(st, e))
1277 return 0;
1278 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001279 case DictComp_kind:
1280 if (!symtable_visit_dictcomp(st, e))
1281 return 0;
1282 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 case Yield_kind:
1284 if (e->v.Yield.value)
1285 VISIT(st, expr, e->v.Yield.value);
1286 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001287 if (st->st_cur->ste_returns_value) {
1288 PyErr_SetString(PyExc_SyntaxError,
1289 RETURN_VAL_IN_GENERATOR);
1290 PyErr_SyntaxLocation(st->st_filename,
1291 e->lineno);
1292 return 0;
1293 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 break;
1295 case Compare_kind:
1296 VISIT(st, expr, e->v.Compare.left);
1297 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1298 break;
1299 case Call_kind:
1300 VISIT(st, expr, e->v.Call.func);
1301 VISIT_SEQ(st, expr, e->v.Call.args);
1302 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1303 if (e->v.Call.starargs)
1304 VISIT(st, expr, e->v.Call.starargs);
1305 if (e->v.Call.kwargs)
1306 VISIT(st, expr, e->v.Call.kwargs);
1307 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 case Num_kind:
1309 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001310 case Bytes_kind:
1311 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 /* Nothing to do here. */
1313 break;
1314 /* The following exprs can be assignment targets. */
1315 case Attribute_kind:
1316 VISIT(st, expr, e->v.Attribute.value);
1317 break;
1318 case Subscript_kind:
1319 VISIT(st, expr, e->v.Subscript.value);
1320 VISIT(st, slice, e->v.Subscript.slice);
1321 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001322 case Starred_kind:
1323 VISIT(st, expr, e->v.Starred.value);
1324 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 case Name_kind:
1326 if (!symtable_add_def(st, e->v.Name.id,
1327 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1328 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001329 /* Special-case super: it counts as a use of __class__ */
1330 if (e->v.Name.ctx == Load &&
1331 st->st_cur->ste_type == FunctionBlock &&
1332 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1333 if (!GET_IDENTIFIER(__class__) ||
1334 !symtable_add_def(st, __class__, USE))
1335 return 0;
1336 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337 break;
1338 /* child nodes of List and Tuple will have expr_context set */
1339 case List_kind:
1340 VISIT_SEQ(st, expr, e->v.List.elts);
1341 break;
1342 case Tuple_kind:
1343 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1344 break;
1345 }
1346 return 1;
1347}
1348
1349static int
1350symtable_implicit_arg(struct symtable *st, int pos)
1351{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001352 PyObject *id = PyUnicode_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353 if (id == NULL)
1354 return 0;
1355 if (!symtable_add_def(st, id, DEF_PARAM)) {
1356 Py_DECREF(id);
1357 return 0;
1358 }
1359 Py_DECREF(id);
1360 return 1;
1361}
1362
1363static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001364symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001366 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001367
1368 if (!args)
1369 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001372 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001373 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 return 0;
1375 }
1376
1377 return 1;
1378}
1379
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001380static int
1381symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382{
1383 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001384
1385 if (!args)
1386 return -1;
1387
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001389 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001390 if (arg->annotation)
1391 VISIT(st, expr, arg->annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001393
1394 return 1;
1395}
1396
Neal Norwitzc1505362006-12-28 06:47:50 +00001397static int
1398symtable_visit_annotations(struct symtable *st, stmt_ty s)
1399{
1400 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001402 if (a->args && !symtable_visit_argannotations(st, a->args))
Neal Norwitzc1505362006-12-28 06:47:50 +00001403 return 0;
1404 if (a->varargannotation)
1405 VISIT(st, expr, a->varargannotation);
1406 if (a->kwargannotation)
1407 VISIT(st, expr, a->kwargannotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001408 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
Neal Norwitzc1505362006-12-28 06:47:50 +00001409 return 0;
1410 if (s->v.FunctionDef.returns)
1411 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 return 1;
1413}
1414
1415static int
1416symtable_visit_arguments(struct symtable *st, arguments_ty a)
1417{
1418 /* skip default arguments inside function block
1419 XXX should ast be different?
1420 */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001421 if (a->args && !symtable_visit_params(st, a->args))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001423 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 if (a->vararg) {
1426 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1427 return 0;
1428 st->st_cur->ste_varargs = 1;
1429 }
1430 if (a->kwarg) {
1431 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1432 return 0;
1433 st->st_cur->ste_varkeywords = 1;
1434 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435 return 1;
1436}
1437
1438
1439static int
1440symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1441{
Neal Norwitzad74aa82008-03-31 05:14:30 +00001442 if (eh->v.ExceptHandler.type)
1443 VISIT(st, expr, eh->v.ExceptHandler.type);
1444 if (eh->v.ExceptHandler.name)
1445 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
Guido van Rossum16be03e2007-01-10 18:51:35 +00001446 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00001447 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 return 1;
1449}
1450
1451
1452static int
1453symtable_visit_alias(struct symtable *st, alias_ty a)
1454{
1455 /* Compute store_name, the name actually bound by the import
1456 operation. It is diferent than a->name when a->name is a
1457 dotted package name (e.g. spam.eggs)
1458 */
1459 PyObject *store_name;
1460 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001461 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1462 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001463 if (dot) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001464 store_name = PyUnicode_FromUnicode(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001465 if (!store_name)
1466 return 0;
1467 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 else {
1469 store_name = name;
1470 Py_INCREF(store_name);
1471 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001472 if (PyUnicode_CompareWithASCIIString(name, "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1474 Py_DECREF(store_name);
1475 return r;
1476 }
1477 else {
1478 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001479 int lineno = st->st_cur->ste_lineno;
Guido van Rossum33d26892007-08-05 15:29:28 +00001480 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1481 PyErr_SyntaxLocation(st->st_filename, lineno);
1482 Py_DECREF(store_name);
1483 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 }
1485 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001486 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 return 1;
1488 }
1489}
1490
1491
1492static int
1493symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1494{
1495 VISIT(st, expr, lc->target);
1496 VISIT(st, expr, lc->iter);
1497 VISIT_SEQ(st, expr, lc->ifs);
1498 return 1;
1499}
1500
1501
1502static int
1503symtable_visit_keyword(struct symtable *st, keyword_ty k)
1504{
1505 VISIT(st, expr, k->value);
1506 return 1;
1507}
1508
1509
1510static int
1511symtable_visit_slice(struct symtable *st, slice_ty s)
1512{
1513 switch (s->kind) {
1514 case Slice_kind:
1515 if (s->v.Slice.lower)
1516 VISIT(st, expr, s->v.Slice.lower)
1517 if (s->v.Slice.upper)
1518 VISIT(st, expr, s->v.Slice.upper)
1519 if (s->v.Slice.step)
1520 VISIT(st, expr, s->v.Slice.step)
1521 break;
1522 case ExtSlice_kind:
1523 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1524 break;
1525 case Index_kind:
1526 VISIT(st, expr, s->v.Index.value)
1527 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 }
1529 return 1;
1530}
1531
1532static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001533symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001534 identifier scope_name, asdl_seq *generators,
1535 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001537 int is_generator = (e->kind == GeneratorExp_kind);
1538 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001540 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 /* Outermost iterator is evaluated in current scope */
1542 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001543 /* Create comprehension scope for the rest */
1544 if (!scope_name ||
1545 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 return 0;
1547 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001548 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 /* Outermost iter is received as an argument */
1550 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001551 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552 return 0;
1553 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001554 /* Allocate temporary name if needed */
1555 if (needs_tmp && !symtable_new_tmpname(st)) {
1556 symtable_exit_block(st, (void *)e);
1557 return 0;
1558 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001559 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1560 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1561 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001562 generators, 1, (void*)e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001563 if (value)
1564 VISIT_IN_BLOCK(st, expr, value, (void*)e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001565 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001566 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001568
1569static int
1570symtable_visit_genexp(struct symtable *st, expr_ty e)
1571{
1572 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1573 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001574 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001575}
1576
1577static int
1578symtable_visit_listcomp(struct symtable *st, expr_ty e)
1579{
1580 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1581 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001582 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001583}
1584
1585static int
1586symtable_visit_setcomp(struct symtable *st, expr_ty e)
1587{
1588 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1589 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001590 e->v.SetComp.elt, NULL);
1591}
1592
1593static int
1594symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1595{
1596 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1597 e->v.DictComp.generators,
1598 e->v.DictComp.key,
1599 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001600}