blob: b9ca615721bec52cf4a9c3c8f14d49b2c7f5c72f [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
Neal Norwitz090b3dd2006-02-28 22:36:46 +000022static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000023ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000024 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020027 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 k = PyLong_FromVoidPtr(key);
30 if (k == NULL)
31 goto fail;
32 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020033 if (ste == NULL) {
34 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020036 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020038 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 ste->ste_name = name;
41 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 ste->ste_symbols = NULL;
44 ste->ste_varnames = NULL;
45 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 ste->ste_symbols = PyDict_New();
48 if (ste->ste_symbols == NULL)
49 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 ste->ste_varnames = PyList_New(0);
52 if (ste->ste_varnames == NULL)
53 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 ste->ste_children = PyList_New(0);
56 if (ste->ste_children == NULL)
57 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000058
Benjamin Petersond9c87022012-10-31 20:26:20 -040059 ste->ste_directives = NULL;
60
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 ste->ste_type = block;
62 ste->ste_unoptimized = 0;
63 ste->ste_nested = 0;
64 ste->ste_free = 0;
65 ste->ste_varargs = 0;
66 ste->ste_varkeywords = 0;
67 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000068 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000069 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000071 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (st->st_cur != NULL &&
74 (st->st_cur->ste_nested ||
75 st->st_cur->ste_type == FunctionBlock))
76 ste->ste_nested = 1;
77 ste->ste_child_free = 0;
78 ste->ste_generator = 0;
79 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
82 goto fail;
83
84 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 Py_XDECREF(ste);
87 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088}
89
90static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
94 ste->ste_name,
95 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096}
97
98static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 ste->ste_table = NULL;
102 Py_XDECREF(ste->ste_id);
103 Py_XDECREF(ste->ste_name);
104 Py_XDECREF(ste->ste_symbols);
105 Py_XDECREF(ste->ste_varnames);
106 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400107 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109}
110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000112
Guido van Rossum6f799372001-09-20 20:46:19 +0000113static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 {"id", T_OBJECT, OFF(ste_id), READONLY},
115 {"name", T_OBJECT, OFF(ste_name), READONLY},
116 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
117 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
118 {"children", T_OBJECT, OFF(ste_children), READONLY},
119 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
120 {"nested", T_INT, OFF(ste_nested), READONLY},
121 {"type", T_INT, OFF(ste_type), READONLY},
122 {"lineno", T_INT, OFF(ste_lineno), READONLY},
123 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124};
125
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000126PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 PyVarObject_HEAD_INIT(&PyType_Type, 0)
128 "symtable entry",
129 sizeof(PySTEntryObject),
130 0,
131 (destructor)ste_dealloc, /* tp_dealloc */
132 0, /* tp_print */
133 0, /* tp_getattr */
134 0, /* tp_setattr */
135 0, /* tp_reserved */
136 (reprfunc)ste_repr, /* tp_repr */
137 0, /* tp_as_number */
138 0, /* tp_as_sequence */
139 0, /* tp_as_mapping */
140 0, /* tp_hash */
141 0, /* tp_call */
142 0, /* tp_str */
143 PyObject_GenericGetAttr, /* tp_getattro */
144 0, /* tp_setattro */
145 0, /* tp_as_buffer */
146 Py_TPFLAGS_DEFAULT, /* tp_flags */
147 0, /* tp_doc */
148 0, /* tp_traverse */
149 0, /* tp_clear */
150 0, /* tp_richcompare */
151 0, /* tp_weaklistoffset */
152 0, /* tp_iter */
153 0, /* tp_iternext */
154 0, /* tp_methods */
155 ste_memberlist, /* tp_members */
156 0, /* tp_getset */
157 0, /* tp_base */
158 0, /* tp_dict */
159 0, /* tp_descr_get */
160 0, /* tp_descr_set */
161 0, /* tp_dictoffset */
162 0, /* tp_init */
163 0, /* tp_alloc */
164 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000165};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166
167static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000168static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000170 _Py_block_ty block, void *ast, int lineno,
171 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000172static int symtable_exit_block(struct symtable *st, void *ast);
173static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
174static int symtable_visit_expr(struct symtable *st, expr_ty s);
175static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000176static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
177static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000178static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179static int symtable_visit_arguments(struct symtable *st, arguments_ty);
180static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
181static int symtable_visit_alias(struct symtable *st, alias_ty);
182static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
183static int symtable_visit_keyword(struct symtable *st, keyword_ty);
184static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000185static int symtable_visit_params(struct symtable *st, asdl_seq *args);
186static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000188static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500189static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191
Nick Coghlan650f0d02007-04-15 12:05:43 +0000192static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
194 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
199#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000200"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
202static struct symtable *
203symtable_new(void)
204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
208 if (st == NULL)
209 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 st->st_filename = NULL;
212 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if ((st->st_stack = PyList_New(0)) == NULL)
215 goto fail;
216 if ((st->st_blocks = PyDict_New()) == NULL)
217 goto fail;
218 st->st_cur = NULL;
219 st->st_private = NULL;
220 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 PySymtable_Free(st);
223 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224}
225
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000226/* When compiling the use of C stack is probably going to be a lot
227 lighter than when executing Python code but still can overflow
228 and causing a Python crash if not checked (e.g. eval("()"*300000)).
229 Using the current recursion limit for the compiler seems too
230 restrictive (it caused at least one test to fail) so a factor is
231 used to allow deeper recursion when compiling an expression.
232
233 Using a scaling factor means this should automatically adjust when
234 the recursion limit is adjusted for small or large C stack allocations.
235*/
236#define COMPILER_STACK_FRAME_SCALE 3
237
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238struct symtable *
239PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
240{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000241 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 asdl_seq *seq;
243 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000244 PyThreadState *tstate;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 if (st == NULL)
247 return st;
248 st->st_filename = filename;
249 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000250
251 /* Setup recursion depth check counters */
252 tstate = PyThreadState_GET();
253 if (!tstate) {
254 PySymtable_Free(st);
255 return NULL;
256 }
257 st->recursion_depth = tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE;
258 st->recursion_limit = Py_GetRecursionLimit() * COMPILER_STACK_FRAME_SCALE;
259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 /* Make the initial symbol information gathering pass */
261 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000262 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 PySymtable_Free(st);
264 return NULL;
265 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 st->st_top = st->st_cur;
268 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
269 switch (mod->kind) {
270 case Module_kind:
271 seq = mod->v.Module.body;
272 for (i = 0; i < asdl_seq_LEN(seq); i++)
273 if (!symtable_visit_stmt(st,
274 (stmt_ty)asdl_seq_GET(seq, i)))
275 goto error;
276 break;
277 case Expression_kind:
278 if (!symtable_visit_expr(st, mod->v.Expression.body))
279 goto error;
280 break;
281 case Interactive_kind:
282 seq = mod->v.Interactive.body;
283 for (i = 0; i < asdl_seq_LEN(seq); i++)
284 if (!symtable_visit_stmt(st,
285 (stmt_ty)asdl_seq_GET(seq, i)))
286 goto error;
287 break;
288 case Suite_kind:
289 PyErr_SetString(PyExc_RuntimeError,
290 "this compiler does not handle Suites");
291 goto error;
292 }
293 if (!symtable_exit_block(st, (void *)mod)) {
294 PySymtable_Free(st);
295 return NULL;
296 }
297 /* Make the second symbol analysis pass */
298 if (symtable_analyze(st))
299 return st;
300 PySymtable_Free(st);
301 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 (void) symtable_exit_block(st, (void *)mod);
304 PySymtable_Free(st);
305 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306}
307
308void
309PySymtable_Free(struct symtable *st)
310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_XDECREF(st->st_blocks);
312 Py_XDECREF(st->st_stack);
313 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314}
315
316PySTEntryObject *
317PySymtable_Lookup(struct symtable *st, void *key)
318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 k = PyLong_FromVoidPtr(key);
322 if (k == NULL)
323 return NULL;
324 v = PyDict_GetItem(st->st_blocks, k);
325 if (v) {
326 assert(PySTEntry_Check(v));
327 Py_INCREF(v);
328 }
329 else {
330 PyErr_SetString(PyExc_KeyError,
331 "unknown symbol table entry");
332 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 Py_DECREF(k);
335 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336}
337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339PyST_GetScope(PySTEntryObject *ste, PyObject *name)
340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
342 if (!v)
343 return 0;
344 assert(PyLong_Check(v));
345 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346}
347
Benjamin Petersond9c87022012-10-31 20:26:20 -0400348static int
349error_at_directive(PySTEntryObject *ste, PyObject *name)
350{
351 Py_ssize_t i;
352 PyObject *data;
353 assert(ste->ste_directives);
354 for (i = 0; ; i++) {
355 data = PyList_GET_ITEM(ste->ste_directives, i);
356 assert(PyTuple_CheckExact(data));
357 if (PyTuple_GET_ITEM(data, 0) == name)
358 break;
359 }
360 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
361 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
362 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
363 return 0;
364}
365
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366
367/* Analyze raw symbol information to determine scope of each name.
368
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000369 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375 explicit global is declared with the global statement. An implicit
376 global is a free variable for which the compiler has found no binding
377 in an enclosing function scope. The implicit global is either a global
378 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
379 to handle these names to implement slightly odd semantics. In such a
380 block, the name is treated as global until it is assigned to; then it
381 is treated as a local.
382
383 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000384 The first pass collects raw facts from the AST via the symtable_visit_*
385 functions: the name is a parameter here, the name is used but not defined
386 here, etc. The second pass analyzes these facts during a pass over the
387 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388
389 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000391 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000392 Names which are explicitly declared nonlocal must exist in this set of
393 visible names - if they do not, a syntax error is raised. After doing
394 the local analysis, it analyzes each of its child blocks using an
395 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396
Nick Coghlan650f0d02007-04-15 12:05:43 +0000397 The children update the free variable set. If a local variable is added to
398 the free variable set by the child, the variable is marked as a cell. The
399 function object being defined must provide runtime storage for the variable
400 that may outlive the function's frame. Cell variables are removed from the
401 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000402
Nick Coghlan650f0d02007-04-15 12:05:43 +0000403 During analysis, the names are:
404 symbols: dict mapping from symbol names to flag values (including offset scope values)
405 scopes: dict mapping from symbol names to scope values (no offset)
406 local: set of all symbol names local to the current scope
407 bound: set of all symbol names local to a containing function scope
408 free: set of all symbol names referenced but not bound in child scopes
409 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410*/
411
412#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 PyObject *o = PyLong_FromLong(I); \
414 if (!o) \
415 return 0; \
416 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
417 Py_DECREF(o); \
418 return 0; \
419 } \
420 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000421}
422
423/* Decide on scope of name, given flags.
424
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000425 The namespace dictionaries may be modified to record information
426 about the new name. For example, a new global will add an entry to
427 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428*/
429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000431analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 PyObject *bound, PyObject *local, PyObject *free,
433 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 if (flags & DEF_GLOBAL) {
436 if (flags & DEF_PARAM) {
437 PyErr_Format(PyExc_SyntaxError,
438 "name '%U' is parameter and global",
439 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400440 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000441 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 if (flags & DEF_NONLOCAL) {
443 PyErr_Format(PyExc_SyntaxError,
444 "name '%U' is nonlocal and global",
445 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400446 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 }
448 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
449 if (PySet_Add(global, name) < 0)
450 return 0;
451 if (bound && (PySet_Discard(bound, name) < 0))
452 return 0;
453 return 1;
454 }
455 if (flags & DEF_NONLOCAL) {
456 if (flags & DEF_PARAM) {
457 PyErr_Format(PyExc_SyntaxError,
458 "name '%U' is parameter and nonlocal",
459 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400460 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 }
462 if (!bound) {
463 PyErr_Format(PyExc_SyntaxError,
464 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400465 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 }
467 if (!PySet_Contains(bound, name)) {
468 PyErr_Format(PyExc_SyntaxError,
469 "no binding for nonlocal '%U' found",
470 name);
471
Benjamin Petersond9c87022012-10-31 20:26:20 -0400472 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 }
474 SET_SCOPE(scopes, name, FREE);
475 ste->ste_free = 1;
476 return PySet_Add(free, name) >= 0;
477 }
478 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000479 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 if (PySet_Add(local, name) < 0)
481 return 0;
482 if (PySet_Discard(global, name) < 0)
483 return 0;
484 return 1;
485 }
486 /* If an enclosing block has a binding for this name, it
487 is a free variable rather than a global variable.
488 Note that having a non-NULL bound implies that the block
489 is nested.
490 */
491 if (bound && PySet_Contains(bound, name)) {
492 SET_SCOPE(scopes, name, FREE);
493 ste->ste_free = 1;
494 return PySet_Add(free, name) >= 0;
495 }
496 /* If a parent has a global statement, then call it global
497 explicit? It could also be global implicit.
498 */
499 if (global && PySet_Contains(global, name)) {
500 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
501 return 1;
502 }
503 if (ste->ste_nested)
504 ste->ste_free = 1;
505 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
506 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000507}
508
509#undef SET_SCOPE
510
511/* If a name is defined in free and also in locals, then this block
512 provides the binding for the free variable. The name should be
513 marked CELL in this block and removed from the free list.
514
515 Note that the current block's free variables are included in free.
516 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000517
Martin v. Löwis2673a572007-10-29 19:54:24 +0000518 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000519 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520*/
521
522static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000523analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 PyObject *name, *v, *v_cell;
526 int success = 0;
527 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 v_cell = PyLong_FromLong(CELL);
530 if (!v_cell)
531 return 0;
532 while (PyDict_Next(scopes, &pos, &name, &v)) {
533 long scope;
534 assert(PyLong_Check(v));
535 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000536 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 continue;
538 if (!PySet_Contains(free, name))
539 continue;
540 if (restricted != NULL &&
541 PyUnicode_CompareWithASCIIString(name, restricted))
542 continue;
543 /* Replace LOCAL with CELL for this name, and remove
544 from free. It is safe to replace the value of name
545 in the dict, because it will not cause a resize.
546 */
547 if (PyDict_SetItem(scopes, name, v_cell) < 0)
548 goto error;
549 if (PySet_Discard(free, name) < 0)
550 goto error;
551 }
552 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 Py_DECREF(v_cell);
555 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556}
557
558/* Check for illegal statements in unoptimized namespaces */
559static int
560check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
564 || !(ste->ste_free || ste->ste_child_free))
565 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 trailer = (ste->ste_child_free ?
568 "contains a nested function with free variables" :
569 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 switch (ste->ste_unoptimized) {
572 case OPT_TOPLEVEL: /* import * at top-level is fine */
573 return 1;
574 case OPT_IMPORT_STAR:
575 PyErr_Format(PyExc_SyntaxError,
576 "import * is not allowed in function '%U' because it %s",
577 ste->ste_name, trailer);
578 break;
579 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000581 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
582 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584}
585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586/* Enter the final scope information into the ste_symbols dict.
587 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588 * All arguments are dicts. Modifies symbols, others are read-only.
589*/
590static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 PyObject *name = NULL, *itr = NULL;
595 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
596 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* Update scope information for all symbols in this scope */
599 while (PyDict_Next(symbols, &pos, &name, &v)) {
600 long scope, flags;
601 assert(PyLong_Check(v));
602 flags = PyLong_AS_LONG(v);
603 v_scope = PyDict_GetItem(scopes, name);
604 assert(v_scope && PyLong_Check(v_scope));
605 scope = PyLong_AS_LONG(v_scope);
606 flags |= (scope << SCOPE_OFFSET);
607 v_new = PyLong_FromLong(flags);
608 if (!v_new)
609 return 0;
610 if (PyDict_SetItem(symbols, name, v_new) < 0) {
611 Py_DECREF(v_new);
612 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 Py_DECREF(v_new);
615 }
616
617 /* Record not yet resolved free variables from children (if any) */
618 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
619 if (!v_free)
620 return 0;
621
622 itr = PyObject_GetIter(free);
623 if (!itr)
624 goto error;
625
626 while ((name = PyIter_Next(itr))) {
627 v = PyDict_GetItem(symbols, name);
628
629 /* Handle symbol that already exists in this scope */
630 if (v) {
631 /* Handle a free variable in a method of
632 the class that has the same name as a local
633 or global in the class scope.
634 */
635 if (classflag &&
636 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
637 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
638 v_new = PyLong_FromLong(flags);
639 if (!v_new) {
640 goto error;
641 }
642 if (PyDict_SetItem(symbols, name, v_new) < 0) {
643 Py_DECREF(v_new);
644 goto error;
645 }
646 Py_DECREF(v_new);
647 }
648 /* It's a cell, or already free in this scope */
649 Py_DECREF(name);
650 continue;
651 }
652 /* Handle global symbol */
653 if (!PySet_Contains(bound, name)) {
654 Py_DECREF(name);
655 continue; /* it's a global */
656 }
657 /* Propagate new free symbol up the lexical stack */
658 if (PyDict_SetItem(symbols, name, v_free) < 0) {
659 goto error;
660 }
661 Py_DECREF(name);
662 }
663 Py_DECREF(itr);
664 Py_DECREF(v_free);
665 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000666error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 Py_XDECREF(v_free);
668 Py_XDECREF(itr);
669 Py_XDECREF(name);
670 return 0;
671}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672
673/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000674
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 Arguments:
676 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000677 bound -- set of variables bound in enclosing scopes (input). bound
678 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 free -- set of free variables in enclosed scopes (output)
680 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000681
682 The implementation uses two mutually recursive functions,
683 analyze_block() and analyze_child_block(). analyze_block() is
684 responsible for analyzing the individual names defined in a block.
685 analyze_child_block() prepares temporary namespace dictionaries
686 used to evaluated nested blocks.
687
688 The two functions exist because a child block should see the name
689 bindings of its enclosing blocks, but those bindings should not
690 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691*/
692
693static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
695 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000696
697static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
699 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
702 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
703 PyObject *temp;
704 int i, success = 0;
705 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 local = PySet_New(NULL); /* collect new names bound in block */
708 if (!local)
709 goto error;
710 scopes = PyDict_New(); /* collect scopes defined for each name */
711 if (!scopes)
712 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* Allocate new global and bound variable dictionaries. These
715 dictionaries hold the names visible in nested blocks. For
716 ClassBlocks, the bound and global names are initialized
717 before analyzing names, because class bindings aren't
718 visible in methods. For other blocks, they are initialized
719 after names are analyzed.
720 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 /* TODO(jhylton): Package these dicts in a struct so that we
723 can write reasonable helper functions?
724 */
725 newglobal = PySet_New(NULL);
726 if (!newglobal)
727 goto error;
728 newfree = PySet_New(NULL);
729 if (!newfree)
730 goto error;
731 newbound = PySet_New(NULL);
732 if (!newbound)
733 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 /* Class namespace has no effect on names visible in
736 nested functions, so populate the global and bound
737 sets to be passed to child blocks before analyzing
738 this one.
739 */
740 if (ste->ste_type == ClassBlock) {
741 /* Pass down known globals */
742 temp = PyNumber_InPlaceOr(newglobal, global);
743 if (!temp)
744 goto error;
745 Py_DECREF(temp);
746 /* Pass down previously bound symbols */
747 if (bound) {
748 temp = PyNumber_InPlaceOr(newbound, bound);
749 if (!temp)
750 goto error;
751 Py_DECREF(temp);
752 }
753 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
756 long flags = PyLong_AS_LONG(v);
757 if (!analyze_name(ste, scopes, name, flags,
758 bound, local, free, global))
759 goto error;
760 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 /* Populate global and bound sets to be passed to children. */
763 if (ste->ste_type != ClassBlock) {
764 /* Add function locals to bound set */
765 if (ste->ste_type == FunctionBlock) {
766 temp = PyNumber_InPlaceOr(newbound, local);
767 if (!temp)
768 goto error;
769 Py_DECREF(temp);
770 }
771 /* Pass down previously bound symbols */
772 if (bound) {
773 temp = PyNumber_InPlaceOr(newbound, bound);
774 if (!temp)
775 goto error;
776 Py_DECREF(temp);
777 }
778 /* Pass down known globals */
779 temp = PyNumber_InPlaceOr(newglobal, global);
780 if (!temp)
781 goto error;
782 Py_DECREF(temp);
783 }
784 else {
785 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000786 if (!GET_IDENTIFIER(__class__))
787 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 assert(PySet_Contains(local, __class__) == 1);
789 if (PySet_Add(newbound, __class__) < 0)
790 goto error;
791 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300793 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 newbound, newglobal now contain the names visible in
796 nested blocks. The free variables in the children will
797 be collected in allfree.
798 */
799 allfree = PySet_New(NULL);
800 if (!allfree)
801 goto error;
802 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
803 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
804 PySTEntryObject* entry;
805 assert(c && PySTEntry_Check(c));
806 entry = (PySTEntryObject*)c;
807 if (!analyze_child_block(entry, newbound, newfree, newglobal,
808 allfree))
809 goto error;
810 /* Check if any children have free variables */
811 if (entry->ste_free || entry->ste_child_free)
812 ste->ste_child_free = 1;
813 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 temp = PyNumber_InPlaceOr(newfree, allfree);
816 if (!temp)
817 goto error;
818 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 /* Check if any local variables must be converted to cell variables */
821 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
822 NULL))
823 goto error;
824 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000825 "__class__"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 goto error;
827 /* Records the results of the analysis in the symbol table entry */
828 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
829 ste->ste_type == ClassBlock))
830 goto error;
831 if (!check_unoptimized(ste))
832 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 temp = PyNumber_InPlaceOr(free, newfree);
835 if (!temp)
836 goto error;
837 Py_DECREF(temp);
838 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 Py_XDECREF(scopes);
841 Py_XDECREF(local);
842 Py_XDECREF(newbound);
843 Py_XDECREF(newglobal);
844 Py_XDECREF(newfree);
845 Py_XDECREF(allfree);
846 if (!success)
847 assert(PyErr_Occurred());
848 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849}
850
851static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
853 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
856 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 These dictionary are used by all blocks enclosed by the
861 current block. The analyze_block() call modifies these
862 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 */
865 temp_bound = PySet_New(bound);
866 if (!temp_bound)
867 goto error;
868 temp_free = PySet_New(free);
869 if (!temp_free)
870 goto error;
871 temp_global = PySet_New(global);
872 if (!temp_global)
873 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
876 goto error;
877 temp = PyNumber_InPlaceOr(child_free, temp_free);
878 if (!temp)
879 goto error;
880 Py_DECREF(temp);
881 Py_DECREF(temp_bound);
882 Py_DECREF(temp_free);
883 Py_DECREF(temp_global);
884 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000885 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 Py_XDECREF(temp_bound);
887 Py_XDECREF(temp_free);
888 Py_XDECREF(temp_global);
889 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000890}
891
892static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893symtable_analyze(struct symtable *st)
894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 PyObject *free, *global;
896 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 free = PySet_New(NULL);
899 if (!free)
900 return 0;
901 global = PySet_New(NULL);
902 if (!global) {
903 Py_DECREF(free);
904 return 0;
905 }
906 r = analyze_block(st->st_top, NULL, free, global);
907 Py_DECREF(free);
908 Py_DECREF(global);
909 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910}
911
912
913static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000914symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
917 lineno, NULL, NULL) < 0) {
918 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
919 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000920 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
921 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 }
923 return 0;
924 }
925 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926}
927
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000928/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 This reference is released when the block is exited, via the DECREF
930 in symtable_exit_block().
931*/
932
933static int
934symtable_exit_block(struct symtable *st, void *ast)
935{
Benjamin Peterson609da582011-06-29 22:52:39 -0500936 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937
Benjamin Peterson609da582011-06-29 22:52:39 -0500938 st->st_cur = NULL;
939 size = PyList_GET_SIZE(st->st_stack);
940 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500941 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500943 if (--size)
944 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 }
946 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947}
948
949static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000951 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952{
Benjamin Peterson609da582011-06-29 22:52:39 -0500953 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954
Benjamin Peterson609da582011-06-29 22:52:39 -0500955 ste = ste_new(st, name, block, ast, lineno, col_offset);
956 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500958 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
959 Py_DECREF(ste);
960 return 0;
961 }
962 prev = st->st_cur;
963 /* The entry is owned by the stack. Borrow it for st_cur. */
964 Py_DECREF(ste);
965 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000966 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 st->st_global = st->st_cur->ste_symbols;
968 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500969 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 return 0;
971 }
972 }
973 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974}
975
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000976static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977symtable_lookup(struct symtable *st, PyObject *name)
978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyObject *o;
980 PyObject *mangled = _Py_Mangle(st->st_private, name);
981 if (!mangled)
982 return 0;
983 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
984 Py_DECREF(mangled);
985 if (!o)
986 return 0;
987 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988}
989
990static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyObject *o;
994 PyObject *dict;
995 long val;
996 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997
Jeremy Hylton81e95022007-02-27 06:50:52 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (!mangled)
1000 return 0;
1001 dict = st->st_cur->ste_symbols;
1002 if ((o = PyDict_GetItem(dict, mangled))) {
1003 val = PyLong_AS_LONG(o);
1004 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1005 /* Is it better to use 'mangled' or 'name' here? */
1006 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001007 PyErr_SyntaxLocationEx(st->st_filename,
1008 st->st_cur->ste_lineno,
1009 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 goto error;
1011 }
1012 val |= flag;
1013 } else
1014 val = flag;
1015 o = PyLong_FromLong(val);
1016 if (o == NULL)
1017 goto error;
1018 if (PyDict_SetItem(dict, mangled, o) < 0) {
1019 Py_DECREF(o);
1020 goto error;
1021 }
1022 Py_DECREF(o);
1023
1024 if (flag & DEF_PARAM) {
1025 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1026 goto error;
1027 } else if (flag & DEF_GLOBAL) {
1028 /* XXX need to update DEF_GLOBAL for other flags too;
1029 perhaps only DEF_FREE_GLOBAL */
1030 val = flag;
1031 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1032 val |= PyLong_AS_LONG(o);
1033 }
1034 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 goto error;
1037 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1038 Py_DECREF(o);
1039 goto error;
1040 }
1041 Py_DECREF(o);
1042 }
1043 Py_DECREF(mangled);
1044 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001045
1046error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 Py_DECREF(mangled);
1048 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049}
1050
1051/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1052 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 function.
1054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1056 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001057
1058 VISIT_QUIT macro returns the specified value exiting from the function but
1059 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060*/
1061
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001062#define VISIT_QUIT(ST, X) \
1063 return --(ST)->recursion_depth,(X)
1064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001067 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001068
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 int i; \
1071 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1072 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1073 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1074 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001075 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001078
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 int i; \
1081 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1082 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1083 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1084 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001085 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001088
Guido van Rossum4f72a782006-10-27 23:31:49 +00001089#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 int i = 0; \
1091 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1092 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1093 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1094 if (!elt) continue; /* can be NULL */ \
1095 if (!symtable_visit_expr((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001096 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001098}
1099
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001101symtable_new_tmpname(struct symtable *st)
1102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 char tmpname[256];
1104 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1107 ++st->st_cur->ste_tmpname);
1108 tmp = PyUnicode_InternFromString(tmpname);
1109 if (!tmp)
1110 return 0;
1111 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1112 return 0;
1113 Py_DECREF(tmp);
1114 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001115}
1116
Guido van Rossum4f72a782006-10-27 23:31:49 +00001117
Guido van Rossumc2e20742006-02-27 22:32:47 +00001118static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001119symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1120{
1121 PyObject *data;
1122 int res;
1123 if (!st->st_cur->ste_directives) {
1124 st->st_cur->ste_directives = PyList_New(0);
1125 if (!st->st_cur->ste_directives)
1126 return 0;
1127 }
1128 data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset);
1129 if (!data)
1130 return 0;
1131 res = PyList_Append(st->st_cur->ste_directives, data);
1132 Py_DECREF(data);
1133 return res == 0;
1134}
1135
1136
1137static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138symtable_visit_stmt(struct symtable *st, stmt_ty s)
1139{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001140 if (++st->recursion_depth > st->recursion_limit) {
1141 PyErr_SetString(PyExc_RuntimeError,
1142 "maximum recursion depth exceeded during compilation");
1143 VISIT_QUIT(st, 0);
1144 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 switch (s->kind) {
1146 case FunctionDef_kind:
1147 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001148 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (s->v.FunctionDef.args->defaults)
1150 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1151 if (s->v.FunctionDef.args->kw_defaults)
1152 VISIT_KWONLYDEFAULTS(st,
1153 s->v.FunctionDef.args->kw_defaults);
1154 if (!symtable_visit_annotations(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001155 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (s->v.FunctionDef.decorator_list)
1157 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1158 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001159 FunctionBlock, (void *)s, s->lineno,
1160 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001161 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001162 VISIT(st, arguments, s->v.FunctionDef.args);
1163 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001165 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 break;
1167 case ClassDef_kind: {
1168 PyObject *tmp;
1169 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001170 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1172 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1173 if (s->v.ClassDef.starargs)
1174 VISIT(st, expr, s->v.ClassDef.starargs);
1175 if (s->v.ClassDef.kwargs)
1176 VISIT(st, expr, s->v.ClassDef.kwargs);
1177 if (s->v.ClassDef.decorator_list)
1178 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1179 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001180 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001181 VISIT_QUIT(st, 0);
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001182 if (!GET_IDENTIFIER(__class__) ||
1183 !symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 !GET_IDENTIFIER(__locals__) ||
1185 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1186 symtable_exit_block(st, s);
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001187 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 }
1189 tmp = st->st_private;
1190 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001191 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 st->st_private = tmp;
1193 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001194 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 break;
1196 }
1197 case Return_kind:
1198 if (s->v.Return.value) {
1199 VISIT(st, expr, s->v.Return.value);
1200 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 }
1202 break;
1203 case Delete_kind:
1204 VISIT_SEQ(st, expr, s->v.Delete.targets);
1205 break;
1206 case Assign_kind:
1207 VISIT_SEQ(st, expr, s->v.Assign.targets);
1208 VISIT(st, expr, s->v.Assign.value);
1209 break;
1210 case AugAssign_kind:
1211 VISIT(st, expr, s->v.AugAssign.target);
1212 VISIT(st, expr, s->v.AugAssign.value);
1213 break;
1214 case For_kind:
1215 VISIT(st, expr, s->v.For.target);
1216 VISIT(st, expr, s->v.For.iter);
1217 VISIT_SEQ(st, stmt, s->v.For.body);
1218 if (s->v.For.orelse)
1219 VISIT_SEQ(st, stmt, s->v.For.orelse);
1220 break;
1221 case While_kind:
1222 VISIT(st, expr, s->v.While.test);
1223 VISIT_SEQ(st, stmt, s->v.While.body);
1224 if (s->v.While.orelse)
1225 VISIT_SEQ(st, stmt, s->v.While.orelse);
1226 break;
1227 case If_kind:
1228 /* XXX if 0: and lookup_yield() hacks */
1229 VISIT(st, expr, s->v.If.test);
1230 VISIT_SEQ(st, stmt, s->v.If.body);
1231 if (s->v.If.orelse)
1232 VISIT_SEQ(st, stmt, s->v.If.orelse);
1233 break;
1234 case Raise_kind:
1235 if (s->v.Raise.exc) {
1236 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001237 if (s->v.Raise.cause) {
1238 VISIT(st, expr, s->v.Raise.cause);
1239 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 }
1241 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001242 case Try_kind:
1243 VISIT_SEQ(st, stmt, s->v.Try.body);
1244 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1245 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1246 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 break;
1248 case Assert_kind:
1249 VISIT(st, expr, s->v.Assert.test);
1250 if (s->v.Assert.msg)
1251 VISIT(st, expr, s->v.Assert.msg);
1252 break;
1253 case Import_kind:
1254 VISIT_SEQ(st, alias, s->v.Import.names);
1255 /* XXX Don't have the lineno available inside
1256 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001257 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001259 st->st_cur->ste_opt_col_offset = s->col_offset;
1260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 break;
1262 case ImportFrom_kind:
1263 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1264 /* XXX Don't have the lineno available inside
1265 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001266 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001268 st->st_cur->ste_opt_col_offset = s->col_offset;
1269 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 break;
1271 case Global_kind: {
1272 int i;
1273 asdl_seq *seq = s->v.Global.names;
1274 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1275 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 long cur = symtable_lookup(st, name);
1277 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001278 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (cur & (DEF_LOCAL | USE)) {
1280 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001281 char *c_name = _PyUnicode_AsString(name);
1282 if (!c_name)
1283 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 if (cur & DEF_LOCAL)
1285 PyOS_snprintf(buf, sizeof(buf),
1286 GLOBAL_AFTER_ASSIGN,
1287 c_name);
1288 else
1289 PyOS_snprintf(buf, sizeof(buf),
1290 GLOBAL_AFTER_USE,
1291 c_name);
1292 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001293 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 }
1295 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001296 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001297 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001298 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 }
1300 break;
1301 }
1302 case Nonlocal_kind: {
1303 int i;
1304 asdl_seq *seq = s->v.Nonlocal.names;
1305 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1306 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 long cur = symtable_lookup(st, name);
1308 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001309 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (cur & (DEF_LOCAL | USE)) {
1311 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001312 char *c_name = _PyUnicode_AsString(name);
1313 if (!c_name)
1314 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 if (cur & DEF_LOCAL)
1316 PyOS_snprintf(buf, sizeof(buf),
1317 NONLOCAL_AFTER_ASSIGN,
1318 c_name);
1319 else
1320 PyOS_snprintf(buf, sizeof(buf),
1321 NONLOCAL_AFTER_USE,
1322 c_name);
1323 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001324 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 }
1326 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001327 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001328 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001329 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 }
1331 break;
1332 }
1333 case Expr_kind:
1334 VISIT(st, expr, s->v.Expr.value);
1335 break;
1336 case Pass_kind:
1337 case Break_kind:
1338 case Continue_kind:
1339 /* nothing to do here */
1340 break;
1341 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001342 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 VISIT_SEQ(st, stmt, s->v.With.body);
1344 break;
1345 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001346 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347}
1348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350symtable_visit_expr(struct symtable *st, expr_ty e)
1351{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001352 if (++st->recursion_depth > st->recursion_limit) {
1353 PyErr_SetString(PyExc_RuntimeError,
1354 "maximum recursion depth exceeded during compilation");
1355 VISIT_QUIT(st, 0);
1356 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 switch (e->kind) {
1358 case BoolOp_kind:
1359 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1360 break;
1361 case BinOp_kind:
1362 VISIT(st, expr, e->v.BinOp.left);
1363 VISIT(st, expr, e->v.BinOp.right);
1364 break;
1365 case UnaryOp_kind:
1366 VISIT(st, expr, e->v.UnaryOp.operand);
1367 break;
1368 case Lambda_kind: {
1369 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001370 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 if (e->v.Lambda.args->defaults)
1372 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001373 if (e->v.Lambda.args->kw_defaults)
1374 VISIT_KWONLYDEFAULTS(st,
1375 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001377 FunctionBlock, (void *)e, e->lineno,
1378 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001379 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001380 VISIT(st, arguments, e->v.Lambda.args);
1381 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001383 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 break;
1385 }
1386 case IfExp_kind:
1387 VISIT(st, expr, e->v.IfExp.test);
1388 VISIT(st, expr, e->v.IfExp.body);
1389 VISIT(st, expr, e->v.IfExp.orelse);
1390 break;
1391 case Dict_kind:
1392 VISIT_SEQ(st, expr, e->v.Dict.keys);
1393 VISIT_SEQ(st, expr, e->v.Dict.values);
1394 break;
1395 case Set_kind:
1396 VISIT_SEQ(st, expr, e->v.Set.elts);
1397 break;
1398 case GeneratorExp_kind:
1399 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001400 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 break;
1402 case ListComp_kind:
1403 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001404 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 break;
1406 case SetComp_kind:
1407 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001408 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 break;
1410 case DictComp_kind:
1411 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001412 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 break;
1414 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001415 if (e->v.Yield.value)
1416 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001419 case YieldFrom_kind:
1420 VISIT(st, expr, e->v.YieldFrom.value);
1421 st->st_cur->ste_generator = 1;
1422 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 case Compare_kind:
1424 VISIT(st, expr, e->v.Compare.left);
1425 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1426 break;
1427 case Call_kind:
1428 VISIT(st, expr, e->v.Call.func);
1429 VISIT_SEQ(st, expr, e->v.Call.args);
1430 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1431 if (e->v.Call.starargs)
1432 VISIT(st, expr, e->v.Call.starargs);
1433 if (e->v.Call.kwargs)
1434 VISIT(st, expr, e->v.Call.kwargs);
1435 break;
1436 case Num_kind:
1437 case Str_kind:
1438 case Bytes_kind:
1439 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001440 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 /* Nothing to do here. */
1442 break;
1443 /* The following exprs can be assignment targets. */
1444 case Attribute_kind:
1445 VISIT(st, expr, e->v.Attribute.value);
1446 break;
1447 case Subscript_kind:
1448 VISIT(st, expr, e->v.Subscript.value);
1449 VISIT(st, slice, e->v.Subscript.slice);
1450 break;
1451 case Starred_kind:
1452 VISIT(st, expr, e->v.Starred.value);
1453 break;
1454 case Name_kind:
1455 if (!symtable_add_def(st, e->v.Name.id,
1456 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001457 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 /* Special-case super: it counts as a use of __class__ */
1459 if (e->v.Name.ctx == Load &&
1460 st->st_cur->ste_type == FunctionBlock &&
1461 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001462 if (!GET_IDENTIFIER(__class__) ||
1463 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001464 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 }
1466 break;
1467 /* child nodes of List and Tuple will have expr_context set */
1468 case List_kind:
1469 VISIT_SEQ(st, expr, e->v.List.elts);
1470 break;
1471 case Tuple_kind:
1472 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1473 break;
1474 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001475 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476}
1477
1478static int
1479symtable_implicit_arg(struct symtable *st, int pos)
1480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1482 if (id == NULL)
1483 return 0;
1484 if (!symtable_add_def(st, id, DEF_PARAM)) {
1485 Py_DECREF(id);
1486 return 0;
1487 }
1488 Py_DECREF(id);
1489 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490}
1491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001493symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 if (!args)
1498 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 for (i = 0; i < asdl_seq_LEN(args); i++) {
1501 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1502 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1503 return 0;
1504 }
1505
1506 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507}
1508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001510symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 if (!args)
1515 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 for (i = 0; i < asdl_seq_LEN(args); i++) {
1518 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1519 if (arg->annotation)
1520 VISIT(st, expr, arg->annotation);
1521 }
1522
1523 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001524}
1525
Neal Norwitzc1505362006-12-28 06:47:50 +00001526static int
1527symtable_visit_annotations(struct symtable *st, stmt_ty s)
1528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 arguments_ty a = s->v.FunctionDef.args;
1530
1531 if (a->args && !symtable_visit_argannotations(st, a->args))
1532 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001533 if (a->vararg && a->vararg->annotation)
1534 VISIT(st, expr, a->vararg->annotation);
1535 if (a->kwarg && a->kwarg->annotation)
1536 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1538 return 0;
1539 if (s->v.FunctionDef.returns)
1540 VISIT(st, expr, s->v.FunctionDef.returns);
1541 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542}
1543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545symtable_visit_arguments(struct symtable *st, arguments_ty a)
1546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 /* skip default arguments inside function block
1548 XXX should ast be different?
1549 */
1550 if (a->args && !symtable_visit_params(st, a->args))
1551 return 0;
1552 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1553 return 0;
1554 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001555 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 return 0;
1557 st->st_cur->ste_varargs = 1;
1558 }
1559 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001560 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 return 0;
1562 st->st_cur->ste_varkeywords = 1;
1563 }
1564 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565}
1566
1567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 if (eh->v.ExceptHandler.type)
1572 VISIT(st, expr, eh->v.ExceptHandler.type);
1573 if (eh->v.ExceptHandler.name)
1574 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1575 return 0;
1576 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1577 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578}
1579
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001580static int
1581symtable_visit_withitem(struct symtable *st, withitem_ty item)
1582{
1583 VISIT(st, expr, item->context_expr);
1584 if (item->optional_vars) {
1585 VISIT(st, expr, item->optional_vars);
1586 }
1587 return 1;
1588}
1589
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592symtable_visit_alias(struct symtable *st, alias_ty a)
1593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001595 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 dotted package name (e.g. spam.eggs)
1597 */
1598 PyObject *store_name;
1599 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001600 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1601 PyUnicode_GET_LENGTH(name), 1);
1602 if (dot != -1) {
1603 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 if (!store_name)
1605 return 0;
1606 }
1607 else {
1608 store_name = name;
1609 Py_INCREF(store_name);
1610 }
1611 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1612 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1613 Py_DECREF(store_name);
1614 return r;
1615 }
1616 else {
1617 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001618 int lineno = st->st_cur->ste_lineno;
1619 int col_offset = st->st_cur->ste_col_offset;
1620 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1621 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1622 Py_DECREF(store_name);
1623 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 }
1625 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1626 Py_DECREF(store_name);
1627 return 1;
1628 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629}
1630
1631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 VISIT(st, expr, lc->target);
1636 VISIT(st, expr, lc->iter);
1637 VISIT_SEQ(st, expr, lc->ifs);
1638 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639}
1640
1641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643symtable_visit_keyword(struct symtable *st, keyword_ty k)
1644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 VISIT(st, expr, k->value);
1646 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647}
1648
1649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651symtable_visit_slice(struct symtable *st, slice_ty s)
1652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 switch (s->kind) {
1654 case Slice_kind:
1655 if (s->v.Slice.lower)
1656 VISIT(st, expr, s->v.Slice.lower)
1657 if (s->v.Slice.upper)
1658 VISIT(st, expr, s->v.Slice.upper)
1659 if (s->v.Slice.step)
1660 VISIT(st, expr, s->v.Slice.step)
1661 break;
1662 case ExtSlice_kind:
1663 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1664 break;
1665 case Index_kind:
1666 VISIT(st, expr, s->v.Index.value)
1667 break;
1668 }
1669 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670}
1671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001673symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001674 identifier scope_name, asdl_seq *generators,
1675 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 int is_generator = (e->kind == GeneratorExp_kind);
1678 int needs_tmp = !is_generator;
1679 comprehension_ty outermost = ((comprehension_ty)
1680 asdl_seq_GET(generators, 0));
1681 /* Outermost iterator is evaluated in current scope */
1682 VISIT(st, expr, outermost->iter);
1683 /* Create comprehension scope for the rest */
1684 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001685 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1686 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 return 0;
1688 }
1689 st->st_cur->ste_generator = is_generator;
1690 /* Outermost iter is received as an argument */
1691 if (!symtable_implicit_arg(st, 0)) {
1692 symtable_exit_block(st, (void *)e);
1693 return 0;
1694 }
1695 /* Allocate temporary name if needed */
1696 if (needs_tmp && !symtable_new_tmpname(st)) {
1697 symtable_exit_block(st, (void *)e);
1698 return 0;
1699 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001700 VISIT(st, expr, outermost->target);
1701 VISIT_SEQ(st, expr, outermost->ifs);
1702 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001704 VISIT(st, expr, value);
1705 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001710symtable_visit_genexp(struct symtable *st, expr_ty e)
1711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1713 e->v.GeneratorExp.generators,
1714 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001715}
1716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001718symtable_visit_listcomp(struct symtable *st, expr_ty e)
1719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1721 e->v.ListComp.generators,
1722 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001723}
1724
1725static int
1726symtable_visit_setcomp(struct symtable *st, expr_ty e)
1727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1729 e->v.SetComp.generators,
1730 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001731}
1732
1733static int
1734symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1737 e->v.DictComp.generators,
1738 e->v.DictComp.key,
1739 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001740}