blob: e3bb2704804e18b30c68254289639173e4755046 [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 */
133 0, /* tp_compare */
134 (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,
Guido van Rossum992d4a32007-07-11 13:09:30 +0000189 listcomp = NULL, setcomp = NULL, dictcomp = NULL, __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191#define GET_IDENTIFIER(VAR) \
Martin v. Löwis5b222132007-06-10 09:51:05 +0000192 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
194#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000195"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
197static struct symtable *
198symtable_new(void)
199{
200 struct symtable *st;
201
202 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
203 if (st == NULL)
204 return NULL;
205
206 st->st_filename = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000207 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000208
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 if ((st->st_stack = PyList_New(0)) == NULL)
210 goto fail;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000211 if ((st->st_blocks = PyDict_New()) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212 goto fail;
213 st->st_cur = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214 st->st_private = NULL;
215 return st;
216 fail:
217 PySymtable_Free(st);
218 return NULL;
219}
220
221struct symtable *
222PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
223{
224 struct symtable *st = symtable_new();
225 asdl_seq *seq;
226 int i;
227
228 if (st == NULL)
229 return st;
230 st->st_filename = filename;
231 st->st_future = future;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000232 /* Make the initial symbol information gathering pass */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000233 if (!GET_IDENTIFIER(top) ||
234 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000235 PySymtable_Free(st);
236 return NULL;
237 }
238
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 st->st_top = st->st_cur;
240 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241 switch (mod->kind) {
242 case Module_kind:
243 seq = mod->v.Module.body;
244 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245 if (!symtable_visit_stmt(st,
246 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247 goto error;
248 break;
249 case Expression_kind:
250 if (!symtable_visit_expr(st, mod->v.Expression.body))
251 goto error;
252 break;
253 case Interactive_kind:
254 seq = mod->v.Interactive.body;
255 for (i = 0; i < asdl_seq_LEN(seq); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256 if (!symtable_visit_stmt(st,
257 (stmt_ty)asdl_seq_GET(seq, i)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258 goto error;
259 break;
260 case Suite_kind:
261 PyErr_SetString(PyExc_RuntimeError,
262 "this compiler does not handle Suites");
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000263 goto error;
264 }
265 if (!symtable_exit_block(st, (void *)mod)) {
266 PySymtable_Free(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267 return NULL;
268 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000269 /* Make the second symbol analysis pass */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 if (symtable_analyze(st))
271 return st;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000272 PySymtable_Free(st);
273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 error:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000275 (void) symtable_exit_block(st, (void *)mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000276 PySymtable_Free(st);
277 return NULL;
278}
279
280void
281PySymtable_Free(struct symtable *st)
282{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000283 Py_XDECREF(st->st_blocks);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284 Py_XDECREF(st->st_stack);
285 PyMem_Free((void *)st);
286}
287
288PySTEntryObject *
289PySymtable_Lookup(struct symtable *st, void *key)
290{
291 PyObject *k, *v;
292
293 k = PyLong_FromVoidPtr(key);
294 if (k == NULL)
295 return NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000296 v = PyDict_GetItem(st->st_blocks, k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297 if (v) {
298 assert(PySTEntry_Check(v));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299 Py_INCREF(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300 }
301 else {
302 PyErr_SetString(PyExc_KeyError,
303 "unknown symbol table entry");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000305
306 Py_DECREF(k);
307 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308}
309
310int
311PyST_GetScope(PySTEntryObject *ste, PyObject *name)
312{
313 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
314 if (!v)
315 return 0;
Christian Heimes217cfd12007-12-02 14:31:20 +0000316 assert(PyLong_Check(v));
317 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318}
319
320
321/* Analyze raw symbol information to determine scope of each name.
322
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000323 The next several functions are helpers for symtable_analyze(),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324 which determines whether a name is local, global, or free. In addition,
325 it determines which local variables are cell variables; they provide
326 bindings that are used for free variables in enclosed blocks.
327
Nick Coghlan650f0d02007-04-15 12:05:43 +0000328 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329 explicit global is declared with the global statement. An implicit
330 global is a free variable for which the compiler has found no binding
331 in an enclosing function scope. The implicit global is either a global
332 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
333 to handle these names to implement slightly odd semantics. In such a
334 block, the name is treated as global until it is assigned to; then it
335 is treated as a local.
336
337 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000338 The first pass collects raw facts from the AST via the symtable_visit_*
339 functions: the name is a parameter here, the name is used but not defined
340 here, etc. The second pass analyzes these facts during a pass over the
341 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342
343 When a function is entered during the second pass, the parent passes
344 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000345 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000346 Names which are explicitly declared nonlocal must exist in this set of
347 visible names - if they do not, a syntax error is raised. After doing
348 the local analysis, it analyzes each of its child blocks using an
349 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350
Nick Coghlan650f0d02007-04-15 12:05:43 +0000351 The children update the free variable set. If a local variable is added to
352 the free variable set by the child, the variable is marked as a cell. The
353 function object being defined must provide runtime storage for the variable
354 that may outlive the function's frame. Cell variables are removed from the
355 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000356
Nick Coghlan650f0d02007-04-15 12:05:43 +0000357 During analysis, the names are:
358 symbols: dict mapping from symbol names to flag values (including offset scope values)
359 scopes: dict mapping from symbol names to scope values (no offset)
360 local: set of all symbol names local to the current scope
361 bound: set of all symbol names local to a containing function scope
362 free: set of all symbol names referenced but not bound in child scopes
363 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364*/
365
366#define SET_SCOPE(DICT, NAME, I) { \
Christian Heimes217cfd12007-12-02 14:31:20 +0000367 PyObject *o = PyLong_FromLong(I); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368 if (!o) \
369 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000370 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
371 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372 return 0; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000373 } \
374 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375}
376
377/* Decide on scope of name, given flags.
378
379 The dicts passed in as arguments are modified as necessary.
380 ste is passed so that flags can be updated.
381*/
382
383static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000384analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385 PyObject *bound, PyObject *local, PyObject *free,
386 PyObject *global)
387{
388 if (flags & DEF_GLOBAL) {
389 if (flags & DEF_PARAM) {
390 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000391 "name '%U' is parameter and global",
392 name);
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000393 PyErr_SyntaxLocation(ste->ste_table->st_filename,
394 ste->ste_lineno);
395
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396 return 0;
397 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000398 if (flags & DEF_NONLOCAL) {
399 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000400 "name '%U' is nonlocal and global",
401 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000402 return 0;
403 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000404 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
405 if (PySet_Add(global, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000406 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000407 if (bound && (PySet_Discard(bound, name) < 0))
408 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409 return 1;
410 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000411 if (flags & DEF_NONLOCAL) {
412 if (flags & DEF_PARAM) {
413 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000414 "name '%U' is parameter and nonlocal",
415 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000416 return 0;
417 }
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000418 if (!bound) {
419 PyErr_Format(PyExc_SyntaxError,
420 "nonlocal declaration not allowed at module level");
421 return 0;
422 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000423 if (!PySet_Contains(bound, name)) {
Jeremy Hylton81e95022007-02-27 06:50:52 +0000424 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald4ddbdad2007-06-11 16:06:26 +0000425 "no binding for nonlocal '%U' found",
426 name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000427
428 return 0;
429 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000430 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000431 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000432 return PySet_Add(free, name) >= 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000433 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434 if (flags & DEF_BOUND) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000435 SET_SCOPE(scopes, name, LOCAL);
436 if (PySet_Add(local, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000438 if (PySet_Discard(global, name) < 0)
439 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440 return 1;
441 }
442 /* If an enclosing block has a binding for this name, it
443 is a free variable rather than a global variable.
444 Note that having a non-NULL bound implies that the block
445 is nested.
446 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000447 if (bound && PySet_Contains(bound, name)) {
448 SET_SCOPE(scopes, name, FREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000450 return PySet_Add(free, name) >= 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451 }
452 /* If a parent has a global statement, then call it global
453 explicit? It could also be global implicit.
454 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000455 if (global && PySet_Contains(global, name)) {
456 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457 return 1;
458 }
Jeremy Hylton81e95022007-02-27 06:50:52 +0000459 if (ste->ste_nested)
460 ste->ste_free = 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000461 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000462 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463}
464
465#undef SET_SCOPE
466
467/* If a name is defined in free and also in locals, then this block
468 provides the binding for the free variable. The name should be
469 marked CELL in this block and removed from the free list.
470
471 Note that the current block's free variables are included in free.
472 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000473
Martin v. Löwis2673a572007-10-29 19:54:24 +0000474 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000475 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476*/
477
478static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000479analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000481 PyObject *name, *v, *v_cell;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000482 int success = 0;
483 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484
Christian Heimes217cfd12007-12-02 14:31:20 +0000485 v_cell = PyLong_FromLong(CELL);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000486 if (!v_cell)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000488 while (PyDict_Next(scopes, &pos, &name, &v)) {
489 long scope;
Christian Heimes217cfd12007-12-02 14:31:20 +0000490 assert(PyLong_Check(v));
491 scope = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000492 if (scope != LOCAL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 continue;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000494 if (!PySet_Contains(free, name))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495 continue;
Martin v. Löwis2673a572007-10-29 19:54:24 +0000496 if (restricted != NULL &&
497 PyUnicode_CompareWithASCIIString(name, restricted))
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000498 continue;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499 /* Replace LOCAL with CELL for this name, and remove
500 from free. It is safe to replace the value of name
501 in the dict, because it will not cause a resize.
502 */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000503 if (PyDict_SetItem(scopes, name, v_cell) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000505 if (PySet_Discard(free, name) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 goto error;
507 }
508 success = 1;
509 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000510 Py_DECREF(v_cell);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511 return success;
512}
513
514/* Check for illegal statements in unoptimized namespaces */
515static int
516check_unoptimized(const PySTEntryObject* ste) {
Armin Rigo31441302005-10-21 12:57:31 +0000517 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518
Neil Schemenauer2dfcef52005-10-23 18:50:36 +0000519 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520 || !(ste->ste_free || ste->ste_child_free))
521 return 1;
522
Armin Rigo31441302005-10-21 12:57:31 +0000523 trailer = (ste->ste_child_free ?
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 "contains a nested function with free variables" :
525 "is a nested function");
526
527 switch (ste->ste_unoptimized) {
Georg Brandl7cae87c2006-09-06 06:51:57 +0000528 case OPT_TOPLEVEL: /* import * at top-level is fine */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 return 1;
530 case OPT_IMPORT_STAR:
Walter Dörwald3ef72bb2007-06-11 16:12:10 +0000531 PyErr_Format(PyExc_SyntaxError,
532 "import * is not allowed in function '%U' because it %s",
Walter Dörwalde42109d2007-06-11 16:08:41 +0000533 ste->ste_name, trailer);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535 }
536
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 PyErr_SyntaxLocation(ste->ste_table->st_filename,
538 ste->ste_opt_lineno);
539 return 0;
540}
541
Nick Coghlan650f0d02007-04-15 12:05:43 +0000542/* Enter the final scope information into the ste_symbols dict.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543 *
544 * All arguments are dicts. Modifies symbols, others are read-only.
545*/
546static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000547update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000548 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000550 PyObject *name = NULL, *itr = NULL;
551 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000552 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553
Nick Coghlan650f0d02007-04-15 12:05:43 +0000554 /* Update scope information for all symbols in this scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555 while (PyDict_Next(symbols, &pos, &name, &v)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000556 long scope, flags;
Christian Heimes217cfd12007-12-02 14:31:20 +0000557 assert(PyLong_Check(v));
558 flags = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000559 v_scope = PyDict_GetItem(scopes, name);
Christian Heimes217cfd12007-12-02 14:31:20 +0000560 assert(v_scope && PyLong_Check(v_scope));
561 scope = PyLong_AS_LONG(v_scope);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000562 flags |= (scope << SCOPE_OFFSET);
Christian Heimes217cfd12007-12-02 14:31:20 +0000563 v_new = PyLong_FromLong(flags);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000564 if (!v_new)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000565 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000566 if (PyDict_SetItem(symbols, name, v_new) < 0) {
567 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 return 0;
569 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000570 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 }
572
Nick Coghlan650f0d02007-04-15 12:05:43 +0000573 /* Record not yet resolved free variables from children (if any) */
Christian Heimes217cfd12007-12-02 14:31:20 +0000574 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000575 if (!v_free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576 return 0;
577
Nick Coghlan650f0d02007-04-15 12:05:43 +0000578 itr = PyObject_GetIter(free);
579 if (!itr)
580 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581
Nick Coghlan650f0d02007-04-15 12:05:43 +0000582 while ((name = PyIter_Next(itr))) {
583 v = PyDict_GetItem(symbols, name);
584
585 /* Handle symbol that already exists in this scope */
586 if (v) {
587 /* Handle a free variable in a method of
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588 the class that has the same name as a local
589 or global in the class scope.
590 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000591 if (classflag &&
Christian Heimes217cfd12007-12-02 14:31:20 +0000592 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
593 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
594 v_new = PyLong_FromLong(flags);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000595 if (!v_new) {
596 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000598 if (PyDict_SetItem(symbols, name, v_new) < 0) {
599 Py_DECREF(v_new);
600 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000602 Py_DECREF(v_new);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000604 /* It's a cell, or already free in this scope */
Nick Coghlan650f0d02007-04-15 12:05:43 +0000605 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606 continue;
607 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000608 /* Handle global symbol */
609 if (!PySet_Contains(bound, name)) {
610 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611 continue; /* it's a global */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000613 /* Propagate new free symbol up the lexical stack */
614 if (PyDict_SetItem(symbols, name, v_free) < 0) {
615 goto error;
616 }
617 Py_DECREF(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000619 Py_DECREF(itr);
620 Py_DECREF(v_free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000622error:
623 Py_XDECREF(v_free);
624 Py_XDECREF(itr);
625 Py_XDECREF(name);
626 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627}
628
629/* Make final symbol table decisions for block of ste.
630 Arguments:
631 ste -- current symtable entry (input/output)
632 bound -- set of variables bound in enclosing scopes (input)
633 free -- set of free variables in enclosed scopes (output)
634 globals -- set of declared global variables in enclosing scopes (input)
635*/
636
637static int
638analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
639 PyObject *global)
640{
Nick Coghlan650f0d02007-04-15 12:05:43 +0000641 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642 PyObject *newglobal = NULL, *newfree = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000643 int i, success = 0;
644 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645
Nick Coghlan650f0d02007-04-15 12:05:43 +0000646 scopes = PyDict_New();
647 if (!scopes)
648 goto error;
649 local = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 if (!local)
651 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000652 newglobal = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653 if (!newglobal)
654 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000655 newfree = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656 if (!newfree)
657 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000658 newbound = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659 if (!newbound)
660 goto error;
661
Nick Coghlan650f0d02007-04-15 12:05:43 +0000662 /* Class namespace has no effect on names visible in
663 nested functions, so populate the global and bound
664 sets to be passed to child blocks before analyzing
665 this one.
666 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667 if (ste->ste_type == ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000668 /* Pass down previously bound symbols */
669 if (bound) {
670 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000672 Py_DECREF(newbound);
673 }
674 /* Pass down known globals */
675 if (!PyNumber_InPlaceOr(newglobal, global))
676 goto error;
677 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 }
679
Nick Coghlan650f0d02007-04-15 12:05:43 +0000680 /* Analyze symbols in current scope */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681 assert(PySTEntry_Check(ste));
682 assert(PyDict_Check(ste->ste_symbols));
683 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000684 long flags = PyLong_AS_LONG(v);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000685 if (!analyze_name(ste, scopes, name, flags, bound, local, free,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 global))
687 goto error;
688 }
689
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000690 /* Populate global and bound sets to be passed to children. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 if (ste->ste_type != ClassBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000692 /* Add function locals to bound set */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693 if (ste->ste_type == FunctionBlock) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000694 if (!PyNumber_InPlaceOr(newbound, local))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000696 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000698 /* Pass down previously bound symbols */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699 if (bound) {
Nick Coghlan650f0d02007-04-15 12:05:43 +0000700 if (!PyNumber_InPlaceOr(newbound, bound))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000702 Py_DECREF(newbound);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703 }
Nick Coghlan650f0d02007-04-15 12:05:43 +0000704 /* Pass down known globals */
705 if (!PyNumber_InPlaceOr(newglobal, global))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000707 Py_DECREF(newglobal);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000709 else {
710 /* Special-case __class__ */
711 if (!GET_IDENTIFIER(__class__))
712 goto error;
713 assert(PySet_Contains(local, __class__) == 1);
714 if (PySet_Add(newbound, __class__) < 0)
715 goto error;
716 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717
718 /* Recursively call analyze_block() on each child block */
719 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
720 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
Armin Rigo31441302005-10-21 12:57:31 +0000721 PySTEntryObject* entry;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 assert(c && PySTEntry_Check(c));
Armin Rigo31441302005-10-21 12:57:31 +0000723 entry = (PySTEntryObject*)c;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724 if (!analyze_block(entry, newbound, newfree, newglobal))
725 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000726 /* Check if any children have free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 if (entry->ste_free || entry->ste_child_free)
728 ste->ste_child_free = 1;
729 }
730
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000731 /* Check if any local variables must be converted to cell variables */
732 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
733 NULL))
734 goto error;
735 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
736 "__class__"))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000738 /* Records the results of the analysis in the symbol table entry */
739 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740 ste->ste_type == ClassBlock))
741 goto error;
742 if (!check_unoptimized(ste))
743 goto error;
744
Nick Coghlan650f0d02007-04-15 12:05:43 +0000745 if (!PyNumber_InPlaceOr(free, newfree))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000747 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000748 success = 1;
749 error:
Nick Coghlan650f0d02007-04-15 12:05:43 +0000750 Py_XDECREF(scopes);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 Py_XDECREF(local);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 Py_XDECREF(newbound);
753 Py_XDECREF(newglobal);
754 Py_XDECREF(newfree);
755 if (!success)
756 assert(PyErr_Occurred());
757 return success;
758}
759
760static int
761symtable_analyze(struct symtable *st)
762{
763 PyObject *free, *global;
764 int r;
765
Nick Coghlan650f0d02007-04-15 12:05:43 +0000766 free = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 if (!free)
768 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000769 global = PySet_New(NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770 if (!global) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000771 Py_DECREF(free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 return 0;
773 }
774 r = analyze_block(st->st_top, NULL, free, global);
775 Py_DECREF(free);
776 Py_DECREF(global);
777 return r;
778}
779
780
781static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000782symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783{
784 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000785 lineno, NULL, NULL) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
787 PyErr_SetString(PyExc_SyntaxError, msg);
788 PyErr_SyntaxLocation(st->st_filename,
789 st->st_cur->ste_lineno);
790 }
791 return 0;
792 }
793 return 1;
794}
795
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000796/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 This reference is released when the block is exited, via the DECREF
798 in symtable_exit_block().
799*/
800
801static int
802symtable_exit_block(struct symtable *st, void *ast)
803{
Martin v. Löwisd96ee902006-02-16 14:37:16 +0000804 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000806 Py_CLEAR(st->st_cur);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807 end = PyList_GET_SIZE(st->st_stack) - 1;
808 if (end >= 0) {
809 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
810 end);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000811 if (st->st_cur == NULL)
812 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 Py_INCREF(st->st_cur);
814 if (PySequence_DelItem(st->st_stack, end) < 0)
815 return 0;
816 }
817 return 1;
818}
819
820static int
Neal Norwitz62c2fac2005-10-24 00:30:44 +0000821symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 void *ast, int lineno)
823{
824 PySTEntryObject *prev = NULL;
825
826 if (st->st_cur) {
827 prev = st->st_cur;
828 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 return 0;
830 }
831 Py_DECREF(st->st_cur);
832 }
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000833 st->st_cur = ste_new(st, name, block, ast, lineno);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000834 if (st->st_cur == NULL)
835 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 if (name == GET_IDENTIFIER(top))
837 st->st_global = st->st_cur->ste_symbols;
838 if (prev) {
839 if (PyList_Append(prev->ste_children,
840 (PyObject *)st->st_cur) < 0) {
841 return 0;
842 }
843 }
844 return 1;
845}
846
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000847static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000848symtable_lookup(struct symtable *st, PyObject *name)
849{
850 PyObject *o;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000851 PyObject *mangled = _Py_Mangle(st->st_private, name);
852 if (!mangled)
853 return 0;
854 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
855 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 if (!o)
857 return 0;
Christian Heimes217cfd12007-12-02 14:31:20 +0000858 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859}
860
861static int
862symtable_add_def(struct symtable *st, PyObject *name, int flag)
863{
864 PyObject *o;
865 PyObject *dict;
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000866 long val;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000867 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868
Jeremy Hylton81e95022007-02-27 06:50:52 +0000869
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000870 if (!mangled)
871 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 dict = st->st_cur->ste_symbols;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000873 if ((o = PyDict_GetItem(dict, mangled))) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000874 val = PyLong_AS_LONG(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000876 /* Is it better to use 'mangled' or 'name' here? */
Neal Norwitza5d16a32007-08-24 22:53:58 +0000877 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878 PyErr_SyntaxLocation(st->st_filename,
879 st->st_cur->ste_lineno);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000880 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 }
882 val |= flag;
883 } else
884 val = flag;
Christian Heimes217cfd12007-12-02 14:31:20 +0000885 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000887 goto error;
888 if (PyDict_SetItem(dict, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000890 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891 }
892 Py_DECREF(o);
893
894 if (flag & DEF_PARAM) {
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000895 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
896 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897 } else if (flag & DEF_GLOBAL) {
898 /* XXX need to update DEF_GLOBAL for other flags too;
899 perhaps only DEF_FREE_GLOBAL */
900 val = flag;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000901 if ((o = PyDict_GetItem(st->st_global, mangled))) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000902 val |= PyLong_AS_LONG(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903 }
Christian Heimes217cfd12007-12-02 14:31:20 +0000904 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905 if (o == NULL)
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000906 goto error;
907 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908 Py_DECREF(o);
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000909 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 }
911 Py_DECREF(o);
912 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000913 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +0000915
916error:
917 Py_DECREF(mangled);
918 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919}
920
921/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
922 They use the ASDL name to synthesize the name of the C type and the visit
923 function.
924
925 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
926 useful if the first node in the sequence requires special treatment.
927*/
928
929#define VISIT(ST, TYPE, V) \
930 if (!symtable_visit_ ## TYPE((ST), (V))) \
931 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000932
933#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
934 if (!symtable_visit_ ## TYPE((ST), (V))) { \
935 symtable_exit_block((ST), (S)); \
936 return 0; \
937 }
938
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939#define VISIT_SEQ(ST, TYPE, SEQ) { \
940 int i; \
941 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
942 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000943 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944 if (!symtable_visit_ ## TYPE((ST), elt)) \
945 return 0; \
946 } \
947}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000948
949#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
950 int i; \
951 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
952 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000953 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000954 if (!symtable_visit_ ## TYPE((ST), elt)) { \
955 symtable_exit_block((ST), (S)); \
956 return 0; \
957 } \
958 } \
959}
960
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
962 int i; \
963 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
964 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000965 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 if (!symtable_visit_ ## TYPE((ST), elt)) \
967 return 0; \
968 } \
969}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000970
971#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
972 int i; \
973 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
974 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000975 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000976 if (!symtable_visit_ ## TYPE((ST), elt)) { \
977 symtable_exit_block((ST), (S)); \
978 return 0; \
979 } \
980 } \
981}
982
Guido van Rossum4f72a782006-10-27 23:31:49 +0000983#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
984 int i = 0; \
985 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
986 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
987 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
988 if (!elt) continue; /* can be NULL */ \
989 if (!symtable_visit_expr((ST), elt)) \
990 return 0; \
991 } \
992}
993
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994static int
Guido van Rossumc2e20742006-02-27 22:32:47 +0000995symtable_new_tmpname(struct symtable *st)
996{
997 char tmpname[256];
998 identifier tmp;
999
1000 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1001 ++st->st_cur->ste_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001002 tmp = PyUnicode_InternFromString(tmpname);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001003 if (!tmp)
1004 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001005 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1006 return 0;
1007 Py_DECREF(tmp);
1008 return 1;
1009}
1010
Guido van Rossum4f72a782006-10-27 23:31:49 +00001011
1012
Guido van Rossumc2e20742006-02-27 22:32:47 +00001013static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014symtable_visit_stmt(struct symtable *st, stmt_ty s)
1015{
1016 switch (s->kind) {
1017 case FunctionDef_kind:
1018 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1019 return 0;
1020 if (s->v.FunctionDef.args->defaults)
1021 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001022 if (s->v.FunctionDef.args->kw_defaults)
1023 VISIT_KWONLYDEFAULTS(st,
1024 s->v.FunctionDef.args->kw_defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001025 if (!symtable_visit_annotations(st, s))
1026 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001027 if (s->v.FunctionDef.decorator_list)
1028 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1030 FunctionBlock, (void *)s, s->lineno))
1031 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001032 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1033 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 if (!symtable_exit_block(st, s))
1035 return 0;
1036 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001037 case ClassDef_kind: {
1038 PyObject *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1040 return 0;
1041 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001042 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1043 if (s->v.ClassDef.starargs)
1044 VISIT(st, expr, s->v.ClassDef.starargs);
1045 if (s->v.ClassDef.kwargs)
1046 VISIT(st, expr, s->v.ClassDef.kwargs);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001047 if (s->v.ClassDef.decorator_list)
1048 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1050 (void *)s, s->lineno))
1051 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001052 if (!GET_IDENTIFIER(__class__) ||
1053 !symtable_add_def(st, __class__, DEF_LOCAL)) {
1054 symtable_exit_block(st, s);
1055 return 0;
1056 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001057 tmp = st->st_private;
1058 st->st_private = s->v.ClassDef.name;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001059 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001060 st->st_private = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 if (!symtable_exit_block(st, s))
1062 return 0;
1063 break;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001064 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 case Return_kind:
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001066 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 VISIT(st, expr, s->v.Return.value);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001068 st->st_cur->ste_returns_value = 1;
1069 if (st->st_cur->ste_generator) {
1070 PyErr_SetString(PyExc_SyntaxError,
1071 RETURN_VAL_IN_GENERATOR);
1072 PyErr_SyntaxLocation(st->st_filename,
1073 s->lineno);
1074 return 0;
1075 }
1076 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 break;
1078 case Delete_kind:
1079 VISIT_SEQ(st, expr, s->v.Delete.targets);
1080 break;
1081 case Assign_kind:
1082 VISIT_SEQ(st, expr, s->v.Assign.targets);
1083 VISIT(st, expr, s->v.Assign.value);
1084 break;
1085 case AugAssign_kind:
1086 VISIT(st, expr, s->v.AugAssign.target);
1087 VISIT(st, expr, s->v.AugAssign.value);
1088 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089 case For_kind:
1090 VISIT(st, expr, s->v.For.target);
1091 VISIT(st, expr, s->v.For.iter);
1092 VISIT_SEQ(st, stmt, s->v.For.body);
1093 if (s->v.For.orelse)
1094 VISIT_SEQ(st, stmt, s->v.For.orelse);
1095 break;
1096 case While_kind:
1097 VISIT(st, expr, s->v.While.test);
1098 VISIT_SEQ(st, stmt, s->v.While.body);
1099 if (s->v.While.orelse)
1100 VISIT_SEQ(st, stmt, s->v.While.orelse);
1101 break;
1102 case If_kind:
1103 /* XXX if 0: and lookup_yield() hacks */
1104 VISIT(st, expr, s->v.If.test);
1105 VISIT_SEQ(st, stmt, s->v.If.body);
1106 if (s->v.If.orelse)
1107 VISIT_SEQ(st, stmt, s->v.If.orelse);
1108 break;
1109 case Raise_kind:
Collin Winter828f04a2007-08-31 00:04:24 +00001110 if (s->v.Raise.exc) {
1111 VISIT(st, expr, s->v.Raise.exc);
1112 if (s->v.Raise.cause) {
1113 VISIT(st, expr, s->v.Raise.cause);
1114 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115 }
1116 break;
1117 case TryExcept_kind:
1118 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1119 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1120 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1121 break;
1122 case TryFinally_kind:
1123 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1124 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1125 break;
1126 case Assert_kind:
1127 VISIT(st, expr, s->v.Assert.test);
1128 if (s->v.Assert.msg)
1129 VISIT(st, expr, s->v.Assert.msg);
1130 break;
1131 case Import_kind:
1132 VISIT_SEQ(st, alias, s->v.Import.names);
1133 /* XXX Don't have the lineno available inside
1134 visit_alias */
1135 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1136 st->st_cur->ste_opt_lineno = s->lineno;
1137 break;
1138 case ImportFrom_kind:
1139 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1140 /* XXX Don't have the lineno available inside
1141 visit_alias */
1142 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1143 st->st_cur->ste_opt_lineno = s->lineno;
1144 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 case Global_kind: {
1146 int i;
1147 asdl_seq *seq = s->v.Global.names;
1148 for (i = 0; i < asdl_seq_LEN(seq); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001149 identifier name = (identifier)asdl_seq_GET(seq, i);
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001150 char *c_name = _PyUnicode_AsString(name);
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001151 long cur = symtable_lookup(st, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 if (cur < 0)
1153 return 0;
1154 if (cur & (DEF_LOCAL | USE)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001155 char buf[256];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 if (cur & DEF_LOCAL)
1157 PyOS_snprintf(buf, sizeof(buf),
1158 GLOBAL_AFTER_ASSIGN,
1159 c_name);
1160 else
1161 PyOS_snprintf(buf, sizeof(buf),
1162 GLOBAL_AFTER_USE,
1163 c_name);
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001164 if (!symtable_warn(st, buf, s->lineno))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 return 0;
1166 }
1167 if (!symtable_add_def(st, name, DEF_GLOBAL))
1168 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170 break;
1171 }
Jeremy Hylton81e95022007-02-27 06:50:52 +00001172 case Nonlocal_kind: {
1173 int i;
1174 asdl_seq *seq = s->v.Nonlocal.names;
1175 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1176 identifier name = (identifier)asdl_seq_GET(seq, i);
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001177 char *c_name = _PyUnicode_AsString(name);
Jeremy Hylton81e95022007-02-27 06:50:52 +00001178 long cur = symtable_lookup(st, name);
1179 if (cur < 0)
1180 return 0;
1181 if (cur & (DEF_LOCAL | USE)) {
1182 char buf[256];
1183 if (cur & DEF_LOCAL)
1184 PyOS_snprintf(buf, sizeof(buf),
1185 NONLOCAL_AFTER_ASSIGN,
1186 c_name);
1187 else
1188 PyOS_snprintf(buf, sizeof(buf),
1189 NONLOCAL_AFTER_USE,
1190 c_name);
1191 if (!symtable_warn(st, buf, s->lineno))
1192 return 0;
1193 }
1194 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1195 return 0;
1196 }
1197 break;
1198 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 case Expr_kind:
1200 VISIT(st, expr, s->v.Expr.value);
1201 break;
1202 case Pass_kind:
1203 case Break_kind:
1204 case Continue_kind:
1205 /* nothing to do here */
1206 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001207 case With_kind:
1208 if (!symtable_new_tmpname(st))
1209 return 0;
1210 VISIT(st, expr, s->v.With.context_expr);
1211 if (s->v.With.optional_vars) {
1212 if (!symtable_new_tmpname(st))
1213 return 0;
1214 VISIT(st, expr, s->v.With.optional_vars);
1215 }
1216 VISIT_SEQ(st, stmt, s->v.With.body);
1217 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 }
1219 return 1;
1220}
1221
1222static int
1223symtable_visit_expr(struct symtable *st, expr_ty e)
1224{
1225 switch (e->kind) {
1226 case BoolOp_kind:
1227 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1228 break;
1229 case BinOp_kind:
1230 VISIT(st, expr, e->v.BinOp.left);
1231 VISIT(st, expr, e->v.BinOp.right);
1232 break;
1233 case UnaryOp_kind:
1234 VISIT(st, expr, e->v.UnaryOp.operand);
1235 break;
1236 case Lambda_kind: {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001237 if (!GET_IDENTIFIER(lambda) ||
1238 !symtable_add_def(st, lambda, DEF_LOCAL))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239 return 0;
1240 if (e->v.Lambda.args->defaults)
1241 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1242 /* XXX how to get line numbers for expressions */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001243 if (!symtable_enter_block(st, lambda,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244 FunctionBlock, (void *)e, 0))
1245 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001246 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1247 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 if (!symtable_exit_block(st, (void *)e))
1249 return 0;
1250 break;
1251 }
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001252 case IfExp_kind:
1253 VISIT(st, expr, e->v.IfExp.test);
1254 VISIT(st, expr, e->v.IfExp.body);
1255 VISIT(st, expr, e->v.IfExp.orelse);
1256 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 case Dict_kind:
1258 VISIT_SEQ(st, expr, e->v.Dict.keys);
1259 VISIT_SEQ(st, expr, e->v.Dict.values);
1260 break;
Georg Brandl17ab9a02006-08-28 16:38:22 +00001261 case Set_kind:
1262 VISIT_SEQ(st, expr, e->v.Set.elts);
1263 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001264 case GeneratorExp_kind:
1265 if (!symtable_visit_genexp(st, e))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001268 case ListComp_kind:
1269 if (!symtable_visit_listcomp(st, e))
1270 return 0;
1271 break;
1272 case SetComp_kind:
1273 if (!symtable_visit_setcomp(st, e))
1274 return 0;
1275 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001276 case DictComp_kind:
1277 if (!symtable_visit_dictcomp(st, e))
1278 return 0;
1279 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 case Yield_kind:
1281 if (e->v.Yield.value)
1282 VISIT(st, expr, e->v.Yield.value);
1283 st->st_cur->ste_generator = 1;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001284 if (st->st_cur->ste_returns_value) {
1285 PyErr_SetString(PyExc_SyntaxError,
1286 RETURN_VAL_IN_GENERATOR);
1287 PyErr_SyntaxLocation(st->st_filename,
1288 e->lineno);
1289 return 0;
1290 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 break;
1292 case Compare_kind:
1293 VISIT(st, expr, e->v.Compare.left);
1294 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1295 break;
1296 case Call_kind:
1297 VISIT(st, expr, e->v.Call.func);
1298 VISIT_SEQ(st, expr, e->v.Call.args);
1299 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1300 if (e->v.Call.starargs)
1301 VISIT(st, expr, e->v.Call.starargs);
1302 if (e->v.Call.kwargs)
1303 VISIT(st, expr, e->v.Call.kwargs);
1304 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 case Num_kind:
1306 case Str_kind:
Georg Brandlee91be42007-02-24 19:41:35 +00001307 case Bytes_kind:
1308 case Ellipsis_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 /* Nothing to do here. */
1310 break;
1311 /* The following exprs can be assignment targets. */
1312 case Attribute_kind:
1313 VISIT(st, expr, e->v.Attribute.value);
1314 break;
1315 case Subscript_kind:
1316 VISIT(st, expr, e->v.Subscript.value);
1317 VISIT(st, slice, e->v.Subscript.slice);
1318 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00001319 case Starred_kind:
1320 VISIT(st, expr, e->v.Starred.value);
1321 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322 case Name_kind:
1323 if (!symtable_add_def(st, e->v.Name.id,
1324 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1325 return 0;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001326 /* Special-case super: it counts as a use of __class__ */
1327 if (e->v.Name.ctx == Load &&
1328 st->st_cur->ste_type == FunctionBlock &&
1329 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1330 if (!GET_IDENTIFIER(__class__) ||
1331 !symtable_add_def(st, __class__, USE))
1332 return 0;
1333 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 break;
1335 /* child nodes of List and Tuple will have expr_context set */
1336 case List_kind:
1337 VISIT_SEQ(st, expr, e->v.List.elts);
1338 break;
1339 case Tuple_kind:
1340 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1341 break;
1342 }
1343 return 1;
1344}
1345
1346static int
1347symtable_implicit_arg(struct symtable *st, int pos)
1348{
Martin v. Löwis5b222132007-06-10 09:51:05 +00001349 PyObject *id = PyUnicode_FromFormat(".%d", pos);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 if (id == NULL)
1351 return 0;
1352 if (!symtable_add_def(st, id, DEF_PARAM)) {
1353 Py_DECREF(id);
1354 return 0;
1355 }
1356 Py_DECREF(id);
1357 return 1;
1358}
1359
1360static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001361symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362{
Neal Norwitzdaf595f2006-01-07 21:24:54 +00001363 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001364
1365 if (!args)
1366 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001369 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001370 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 return 0;
1372 }
1373
1374 return 1;
1375}
1376
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001377static int
1378symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379{
1380 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001381
1382 if (!args)
1383 return -1;
1384
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 for (i = 0; i < asdl_seq_LEN(args); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001386 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001387 if (arg->annotation)
1388 VISIT(st, expr, arg->annotation);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001390
1391 return 1;
1392}
1393
Neal Norwitzc1505362006-12-28 06:47:50 +00001394static int
1395symtable_visit_annotations(struct symtable *st, stmt_ty s)
1396{
1397 arguments_ty a = s->v.FunctionDef.args;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001399 if (a->args && !symtable_visit_argannotations(st, a->args))
Neal Norwitzc1505362006-12-28 06:47:50 +00001400 return 0;
1401 if (a->varargannotation)
1402 VISIT(st, expr, a->varargannotation);
1403 if (a->kwargannotation)
1404 VISIT(st, expr, a->kwargannotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001405 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
Neal Norwitzc1505362006-12-28 06:47:50 +00001406 return 0;
1407 if (s->v.FunctionDef.returns)
1408 VISIT(st, expr, s->v.FunctionDef.returns);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 return 1;
1410}
1411
1412static int
1413symtable_visit_arguments(struct symtable *st, arguments_ty a)
1414{
1415 /* skip default arguments inside function block
1416 XXX should ast be different?
1417 */
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001418 if (a->args && !symtable_visit_params(st, a->args))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001420 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
Guido van Rossum4f72a782006-10-27 23:31:49 +00001421 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 if (a->vararg) {
1423 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1424 return 0;
1425 st->st_cur->ste_varargs = 1;
1426 }
1427 if (a->kwarg) {
1428 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1429 return 0;
1430 st->st_cur->ste_varkeywords = 1;
1431 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 return 1;
1433}
1434
1435
1436static int
1437symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1438{
Neal Norwitzad74aa82008-03-31 05:14:30 +00001439 if (eh->v.ExceptHandler.type)
1440 VISIT(st, expr, eh->v.ExceptHandler.type);
1441 if (eh->v.ExceptHandler.name)
1442 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
Guido van Rossum16be03e2007-01-10 18:51:35 +00001443 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00001444 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 return 1;
1446}
1447
1448
1449static int
1450symtable_visit_alias(struct symtable *st, alias_ty a)
1451{
1452 /* Compute store_name, the name actually bound by the import
1453 operation. It is diferent than a->name when a->name is a
1454 dotted package name (e.g. spam.eggs)
1455 */
1456 PyObject *store_name;
1457 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwis5b222132007-06-10 09:51:05 +00001458 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1459 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001460 if (dot) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001461 store_name = PyUnicode_FromUnicode(base, dot - base);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001462 if (!store_name)
1463 return 0;
1464 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 else {
1466 store_name = name;
1467 Py_INCREF(store_name);
1468 }
Martin v. Löwis5b222132007-06-10 09:51:05 +00001469 if (PyUnicode_CompareWithASCIIString(name, "*")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1471 Py_DECREF(store_name);
1472 return r;
1473 }
1474 else {
1475 if (st->st_cur->ste_type != ModuleBlock) {
Neal Norwitz5d0ad502005-12-19 04:27:42 +00001476 int lineno = st->st_cur->ste_lineno;
Guido van Rossum33d26892007-08-05 15:29:28 +00001477 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1478 PyErr_SyntaxLocation(st->st_filename, lineno);
1479 Py_DECREF(store_name);
1480 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 }
1482 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
Neal Norwitz4737b232005-11-19 23:58:29 +00001483 Py_DECREF(store_name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 return 1;
1485 }
1486}
1487
1488
1489static int
1490symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1491{
1492 VISIT(st, expr, lc->target);
1493 VISIT(st, expr, lc->iter);
1494 VISIT_SEQ(st, expr, lc->ifs);
1495 return 1;
1496}
1497
1498
1499static int
1500symtable_visit_keyword(struct symtable *st, keyword_ty k)
1501{
1502 VISIT(st, expr, k->value);
1503 return 1;
1504}
1505
1506
1507static int
1508symtable_visit_slice(struct symtable *st, slice_ty s)
1509{
1510 switch (s->kind) {
1511 case Slice_kind:
1512 if (s->v.Slice.lower)
1513 VISIT(st, expr, s->v.Slice.lower)
1514 if (s->v.Slice.upper)
1515 VISIT(st, expr, s->v.Slice.upper)
1516 if (s->v.Slice.step)
1517 VISIT(st, expr, s->v.Slice.step)
1518 break;
1519 case ExtSlice_kind:
1520 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1521 break;
1522 case Index_kind:
1523 VISIT(st, expr, s->v.Index.value)
1524 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 }
1526 return 1;
1527}
1528
1529static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001530symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001531 identifier scope_name, asdl_seq *generators,
1532 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533{
Nick Coghlan650f0d02007-04-15 12:05:43 +00001534 int is_generator = (e->kind == GeneratorExp_kind);
1535 int needs_tmp = !is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 comprehension_ty outermost = ((comprehension_ty)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001537 asdl_seq_GET(generators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 /* Outermost iterator is evaluated in current scope */
1539 VISIT(st, expr, outermost->iter);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001540 /* Create comprehension scope for the rest */
1541 if (!scope_name ||
1542 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 return 0;
1544 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001545 st->st_cur->ste_generator = is_generator;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 /* Outermost iter is received as an argument */
1547 if (!symtable_implicit_arg(st, 0)) {
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001548 symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 return 0;
1550 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00001551 /* Allocate temporary name if needed */
1552 if (needs_tmp && !symtable_new_tmpname(st)) {
1553 symtable_exit_block(st, (void *)e);
1554 return 0;
1555 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001556 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1557 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1558 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
Nick Coghlan650f0d02007-04-15 12:05:43 +00001559 generators, 1, (void*)e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001560 if (value)
1561 VISIT_IN_BLOCK(st, expr, value, (void*)e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001562 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001563 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001565
1566static int
1567symtable_visit_genexp(struct symtable *st, expr_ty e)
1568{
1569 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1570 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001571 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001572}
1573
1574static int
1575symtable_visit_listcomp(struct symtable *st, expr_ty e)
1576{
1577 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1578 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001579 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001580}
1581
1582static int
1583symtable_visit_setcomp(struct symtable *st, expr_ty e)
1584{
1585 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1586 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001587 e->v.SetComp.elt, NULL);
1588}
1589
1590static int
1591symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1592{
1593 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1594 e->v.DictComp.generators,
1595 e->v.DictComp.key,
1596 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001597}