blob: 758aefb90390def2b113be33f1c86953ea4445aa [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;
Benjamin Peterson312595c2013-05-15 15:26:42 -050080 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
83 goto fail;
84
85 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000086 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 Py_XDECREF(ste);
88 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000089}
90
91static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
95 ste->ste_name,
96 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097}
98
99static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 ste->ste_table = NULL;
103 Py_XDECREF(ste->ste_id);
104 Py_XDECREF(ste->ste_name);
105 Py_XDECREF(ste->ste_symbols);
106 Py_XDECREF(ste->ste_varnames);
107 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400108 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000110}
111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000113
Guido van Rossum6f799372001-09-20 20:46:19 +0000114static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 {"id", T_OBJECT, OFF(ste_id), READONLY},
116 {"name", T_OBJECT, OFF(ste_name), READONLY},
117 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
118 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
119 {"children", T_OBJECT, OFF(ste_children), READONLY},
120 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
121 {"nested", T_INT, OFF(ste_nested), READONLY},
122 {"type", T_INT, OFF(ste_type), READONLY},
123 {"lineno", T_INT, OFF(ste_lineno), READONLY},
124 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000125};
126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 PyVarObject_HEAD_INIT(&PyType_Type, 0)
129 "symtable entry",
130 sizeof(PySTEntryObject),
131 0,
132 (destructor)ste_dealloc, /* tp_dealloc */
133 0, /* tp_print */
134 0, /* tp_getattr */
135 0, /* tp_setattr */
136 0, /* tp_reserved */
137 (reprfunc)ste_repr, /* tp_repr */
138 0, /* tp_as_number */
139 0, /* tp_as_sequence */
140 0, /* tp_as_mapping */
141 0, /* tp_hash */
142 0, /* tp_call */
143 0, /* tp_str */
144 PyObject_GenericGetAttr, /* tp_getattro */
145 0, /* tp_setattro */
146 0, /* tp_as_buffer */
147 Py_TPFLAGS_DEFAULT, /* tp_flags */
148 0, /* tp_doc */
149 0, /* tp_traverse */
150 0, /* tp_clear */
151 0, /* tp_richcompare */
152 0, /* tp_weaklistoffset */
153 0, /* tp_iter */
154 0, /* tp_iternext */
155 0, /* tp_methods */
156 ste_memberlist, /* tp_members */
157 0, /* tp_getset */
158 0, /* tp_base */
159 0, /* tp_dict */
160 0, /* tp_descr_get */
161 0, /* tp_descr_set */
162 0, /* tp_dictoffset */
163 0, /* tp_init */
164 0, /* tp_alloc */
165 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000166};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167
168static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000169static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000171 _Py_block_ty block, void *ast, int lineno,
172 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173static int symtable_exit_block(struct symtable *st, void *ast);
174static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
175static int symtable_visit_expr(struct symtable *st, expr_ty s);
176static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000177static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
178static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000179static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180static int symtable_visit_arguments(struct symtable *st, arguments_ty);
181static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
182static int symtable_visit_alias(struct symtable *st, alias_ty);
183static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
184static int symtable_visit_keyword(struct symtable *st, keyword_ty);
185static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000186static int symtable_visit_params(struct symtable *st, asdl_seq *args);
187static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000189static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500190static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
192
Nick Coghlan650f0d02007-04-15 12:05:43 +0000193static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500195 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
197#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
200#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000201"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202
203static struct symtable *
204symtable_new(void)
205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
209 if (st == NULL)
210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 st->st_filename = NULL;
213 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 if ((st->st_stack = PyList_New(0)) == NULL)
216 goto fail;
217 if ((st->st_blocks = PyDict_New()) == NULL)
218 goto fail;
219 st->st_cur = NULL;
220 st->st_private = NULL;
221 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 PySymtable_Free(st);
224 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225}
226
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000227/* When compiling the use of C stack is probably going to be a lot
228 lighter than when executing Python code but still can overflow
229 and causing a Python crash if not checked (e.g. eval("()"*300000)).
230 Using the current recursion limit for the compiler seems too
231 restrictive (it caused at least one test to fail) so a factor is
232 used to allow deeper recursion when compiling an expression.
233
234 Using a scaling factor means this should automatically adjust when
235 the recursion limit is adjusted for small or large C stack allocations.
236*/
237#define COMPILER_STACK_FRAME_SCALE 3
238
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239struct symtable *
240PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
241{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000242 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 asdl_seq *seq;
244 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000245 PyThreadState *tstate;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 if (st == NULL)
248 return st;
249 st->st_filename = filename;
250 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000251
252 /* Setup recursion depth check counters */
253 tstate = PyThreadState_GET();
254 if (!tstate) {
255 PySymtable_Free(st);
256 return NULL;
257 }
258 st->recursion_depth = tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE;
259 st->recursion_limit = Py_GetRecursionLimit() * COMPILER_STACK_FRAME_SCALE;
260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 /* Make the initial symbol information gathering pass */
262 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000263 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 PySymtable_Free(st);
265 return NULL;
266 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 st->st_top = st->st_cur;
269 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
270 switch (mod->kind) {
271 case Module_kind:
272 seq = mod->v.Module.body;
273 for (i = 0; i < asdl_seq_LEN(seq); i++)
274 if (!symtable_visit_stmt(st,
275 (stmt_ty)asdl_seq_GET(seq, i)))
276 goto error;
277 break;
278 case Expression_kind:
279 if (!symtable_visit_expr(st, mod->v.Expression.body))
280 goto error;
281 break;
282 case Interactive_kind:
283 seq = mod->v.Interactive.body;
284 for (i = 0; i < asdl_seq_LEN(seq); i++)
285 if (!symtable_visit_stmt(st,
286 (stmt_ty)asdl_seq_GET(seq, i)))
287 goto error;
288 break;
289 case Suite_kind:
290 PyErr_SetString(PyExc_RuntimeError,
291 "this compiler does not handle Suites");
292 goto error;
293 }
294 if (!symtable_exit_block(st, (void *)mod)) {
295 PySymtable_Free(st);
296 return NULL;
297 }
298 /* Make the second symbol analysis pass */
299 if (symtable_analyze(st))
300 return st;
301 PySymtable_Free(st);
302 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 (void) symtable_exit_block(st, (void *)mod);
305 PySymtable_Free(st);
306 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307}
308
309void
310PySymtable_Free(struct symtable *st)
311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 Py_XDECREF(st->st_blocks);
313 Py_XDECREF(st->st_stack);
314 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315}
316
317PySTEntryObject *
318PySymtable_Lookup(struct symtable *st, void *key)
319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 k = PyLong_FromVoidPtr(key);
323 if (k == NULL)
324 return NULL;
325 v = PyDict_GetItem(st->st_blocks, k);
326 if (v) {
327 assert(PySTEntry_Check(v));
328 Py_INCREF(v);
329 }
330 else {
331 PyErr_SetString(PyExc_KeyError,
332 "unknown symbol table entry");
333 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 Py_DECREF(k);
336 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337}
338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340PyST_GetScope(PySTEntryObject *ste, PyObject *name)
341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
343 if (!v)
344 return 0;
345 assert(PyLong_Check(v));
346 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347}
348
Benjamin Petersond9c87022012-10-31 20:26:20 -0400349static int
350error_at_directive(PySTEntryObject *ste, PyObject *name)
351{
352 Py_ssize_t i;
353 PyObject *data;
354 assert(ste->ste_directives);
355 for (i = 0; ; i++) {
356 data = PyList_GET_ITEM(ste->ste_directives, i);
357 assert(PyTuple_CheckExact(data));
358 if (PyTuple_GET_ITEM(data, 0) == name)
359 break;
360 }
361 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
362 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
363 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
364 return 0;
365}
366
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367
368/* Analyze raw symbol information to determine scope of each name.
369
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000370 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376 explicit global is declared with the global statement. An implicit
377 global is a free variable for which the compiler has found no binding
378 in an enclosing function scope. The implicit global is either a global
379 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
380 to handle these names to implement slightly odd semantics. In such a
381 block, the name is treated as global until it is assigned to; then it
382 is treated as a local.
383
384 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000385 The first pass collects raw facts from the AST via the symtable_visit_*
386 functions: the name is a parameter here, the name is used but not defined
387 here, etc. The second pass analyzes these facts during a pass over the
388 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389
390 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000392 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000393 Names which are explicitly declared nonlocal must exist in this set of
394 visible names - if they do not, a syntax error is raised. After doing
395 the local analysis, it analyzes each of its child blocks using an
396 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397
Nick Coghlan650f0d02007-04-15 12:05:43 +0000398 The children update the free variable set. If a local variable is added to
399 the free variable set by the child, the variable is marked as a cell. The
400 function object being defined must provide runtime storage for the variable
401 that may outlive the function's frame. Cell variables are removed from the
402 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000403
Nick Coghlan650f0d02007-04-15 12:05:43 +0000404 During analysis, the names are:
405 symbols: dict mapping from symbol names to flag values (including offset scope values)
406 scopes: dict mapping from symbol names to scope values (no offset)
407 local: set of all symbol names local to the current scope
408 bound: set of all symbol names local to a containing function scope
409 free: set of all symbol names referenced but not bound in child scopes
410 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000411*/
412
413#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 PyObject *o = PyLong_FromLong(I); \
415 if (!o) \
416 return 0; \
417 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
418 Py_DECREF(o); \
419 return 0; \
420 } \
421 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422}
423
424/* Decide on scope of name, given flags.
425
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000426 The namespace dictionaries may be modified to record information
427 about the new name. For example, a new global will add an entry to
428 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429*/
430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000432analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyObject *bound, PyObject *local, PyObject *free,
434 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (flags & DEF_GLOBAL) {
437 if (flags & DEF_PARAM) {
438 PyErr_Format(PyExc_SyntaxError,
439 "name '%U' is parameter and global",
440 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400441 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000442 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (flags & DEF_NONLOCAL) {
444 PyErr_Format(PyExc_SyntaxError,
445 "name '%U' is nonlocal and global",
446 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400447 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 }
449 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
450 if (PySet_Add(global, name) < 0)
451 return 0;
452 if (bound && (PySet_Discard(bound, name) < 0))
453 return 0;
454 return 1;
455 }
456 if (flags & DEF_NONLOCAL) {
457 if (flags & DEF_PARAM) {
458 PyErr_Format(PyExc_SyntaxError,
459 "name '%U' is parameter and nonlocal",
460 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400461 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 }
463 if (!bound) {
464 PyErr_Format(PyExc_SyntaxError,
465 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400466 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 }
468 if (!PySet_Contains(bound, name)) {
469 PyErr_Format(PyExc_SyntaxError,
470 "no binding for nonlocal '%U' found",
471 name);
472
Benjamin Petersond9c87022012-10-31 20:26:20 -0400473 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 }
475 SET_SCOPE(scopes, name, FREE);
476 ste->ste_free = 1;
477 return PySet_Add(free, name) >= 0;
478 }
479 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000480 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (PySet_Add(local, name) < 0)
482 return 0;
483 if (PySet_Discard(global, name) < 0)
484 return 0;
485 return 1;
486 }
487 /* If an enclosing block has a binding for this name, it
488 is a free variable rather than a global variable.
489 Note that having a non-NULL bound implies that the block
490 is nested.
491 */
492 if (bound && PySet_Contains(bound, name)) {
493 SET_SCOPE(scopes, name, FREE);
494 ste->ste_free = 1;
495 return PySet_Add(free, name) >= 0;
496 }
497 /* If a parent has a global statement, then call it global
498 explicit? It could also be global implicit.
499 */
500 if (global && PySet_Contains(global, name)) {
501 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
502 return 1;
503 }
504 if (ste->ste_nested)
505 ste->ste_free = 1;
506 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
507 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508}
509
510#undef SET_SCOPE
511
512/* If a name is defined in free and also in locals, then this block
513 provides the binding for the free variable. The name should be
514 marked CELL in this block and removed from the free list.
515
516 Note that the current block's free variables are included in free.
517 That's safe because no name can be free and local in the same scope.
518*/
519
520static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500521analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 PyObject *name, *v, *v_cell;
524 int success = 0;
525 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 v_cell = PyLong_FromLong(CELL);
528 if (!v_cell)
529 return 0;
530 while (PyDict_Next(scopes, &pos, &name, &v)) {
531 long scope;
532 assert(PyLong_Check(v));
533 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000534 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 continue;
536 if (!PySet_Contains(free, name))
537 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 /* Replace LOCAL with CELL for this name, and remove
539 from free. It is safe to replace the value of name
540 in the dict, because it will not cause a resize.
541 */
542 if (PyDict_SetItem(scopes, name, v_cell) < 0)
543 goto error;
544 if (PySet_Discard(free, name) < 0)
545 goto error;
546 }
547 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 Py_DECREF(v_cell);
550 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551}
552
Benjamin Peterson312595c2013-05-15 15:26:42 -0500553static int
554drop_class_free(PySTEntryObject *ste, PyObject *free)
555{
556 int res;
557 if (!GET_IDENTIFIER(__class__))
558 return 0;
559 res = PySet_Discard(free, __class__);
560 if (res < 0)
561 return 0;
562 if (res)
563 ste->ste_needs_class_closure = 1;
564 return 1;
565}
566
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567/* Check for illegal statements in unoptimized namespaces */
568static int
569check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
573 || !(ste->ste_free || ste->ste_child_free))
574 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 trailer = (ste->ste_child_free ?
577 "contains a nested function with free variables" :
578 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 switch (ste->ste_unoptimized) {
581 case OPT_TOPLEVEL: /* import * at top-level is fine */
582 return 1;
583 case OPT_IMPORT_STAR:
584 PyErr_Format(PyExc_SyntaxError,
585 "import * is not allowed in function '%U' because it %s",
586 ste->ste_name, trailer);
587 break;
588 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000590 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
591 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593}
594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595/* Enter the final scope information into the ste_symbols dict.
596 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 * All arguments are dicts. Modifies symbols, others are read-only.
598*/
599static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000601 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 PyObject *name = NULL, *itr = NULL;
604 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
605 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 /* Update scope information for all symbols in this scope */
608 while (PyDict_Next(symbols, &pos, &name, &v)) {
609 long scope, flags;
610 assert(PyLong_Check(v));
611 flags = PyLong_AS_LONG(v);
612 v_scope = PyDict_GetItem(scopes, name);
613 assert(v_scope && PyLong_Check(v_scope));
614 scope = PyLong_AS_LONG(v_scope);
615 flags |= (scope << SCOPE_OFFSET);
616 v_new = PyLong_FromLong(flags);
617 if (!v_new)
618 return 0;
619 if (PyDict_SetItem(symbols, name, v_new) < 0) {
620 Py_DECREF(v_new);
621 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 Py_DECREF(v_new);
624 }
625
626 /* Record not yet resolved free variables from children (if any) */
627 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
628 if (!v_free)
629 return 0;
630
631 itr = PyObject_GetIter(free);
632 if (!itr)
633 goto error;
634
635 while ((name = PyIter_Next(itr))) {
636 v = PyDict_GetItem(symbols, name);
637
638 /* Handle symbol that already exists in this scope */
639 if (v) {
640 /* Handle a free variable in a method of
641 the class that has the same name as a local
642 or global in the class scope.
643 */
644 if (classflag &&
645 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
646 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
647 v_new = PyLong_FromLong(flags);
648 if (!v_new) {
649 goto error;
650 }
651 if (PyDict_SetItem(symbols, name, v_new) < 0) {
652 Py_DECREF(v_new);
653 goto error;
654 }
655 Py_DECREF(v_new);
656 }
657 /* It's a cell, or already free in this scope */
658 Py_DECREF(name);
659 continue;
660 }
661 /* Handle global symbol */
662 if (!PySet_Contains(bound, name)) {
663 Py_DECREF(name);
664 continue; /* it's a global */
665 }
666 /* Propagate new free symbol up the lexical stack */
667 if (PyDict_SetItem(symbols, name, v_free) < 0) {
668 goto error;
669 }
670 Py_DECREF(name);
671 }
672 Py_DECREF(itr);
673 Py_DECREF(v_free);
674 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000675error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 Py_XDECREF(v_free);
677 Py_XDECREF(itr);
678 Py_XDECREF(name);
679 return 0;
680}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681
682/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000683
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684 Arguments:
685 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000686 bound -- set of variables bound in enclosing scopes (input). bound
687 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 free -- set of free variables in enclosed scopes (output)
689 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000690
691 The implementation uses two mutually recursive functions,
692 analyze_block() and analyze_child_block(). analyze_block() is
693 responsible for analyzing the individual names defined in a block.
694 analyze_child_block() prepares temporary namespace dictionaries
695 used to evaluated nested blocks.
696
697 The two functions exist because a child block should see the name
698 bindings of its enclosing blocks, but those bindings should not
699 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700*/
701
702static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
704 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000705
706static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
708 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
711 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
712 PyObject *temp;
713 int i, success = 0;
714 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 local = PySet_New(NULL); /* collect new names bound in block */
717 if (!local)
718 goto error;
719 scopes = PyDict_New(); /* collect scopes defined for each name */
720 if (!scopes)
721 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 /* Allocate new global and bound variable dictionaries. These
724 dictionaries hold the names visible in nested blocks. For
725 ClassBlocks, the bound and global names are initialized
726 before analyzing names, because class bindings aren't
727 visible in methods. For other blocks, they are initialized
728 after names are analyzed.
729 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 /* TODO(jhylton): Package these dicts in a struct so that we
732 can write reasonable helper functions?
733 */
734 newglobal = PySet_New(NULL);
735 if (!newglobal)
736 goto error;
737 newfree = PySet_New(NULL);
738 if (!newfree)
739 goto error;
740 newbound = PySet_New(NULL);
741 if (!newbound)
742 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 /* Class namespace has no effect on names visible in
745 nested functions, so populate the global and bound
746 sets to be passed to child blocks before analyzing
747 this one.
748 */
749 if (ste->ste_type == ClassBlock) {
750 /* Pass down known globals */
751 temp = PyNumber_InPlaceOr(newglobal, global);
752 if (!temp)
753 goto error;
754 Py_DECREF(temp);
755 /* Pass down previously bound symbols */
756 if (bound) {
757 temp = PyNumber_InPlaceOr(newbound, bound);
758 if (!temp)
759 goto error;
760 Py_DECREF(temp);
761 }
762 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
765 long flags = PyLong_AS_LONG(v);
766 if (!analyze_name(ste, scopes, name, flags,
767 bound, local, free, global))
768 goto error;
769 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 /* Populate global and bound sets to be passed to children. */
772 if (ste->ste_type != ClassBlock) {
773 /* Add function locals to bound set */
774 if (ste->ste_type == FunctionBlock) {
775 temp = PyNumber_InPlaceOr(newbound, local);
776 if (!temp)
777 goto error;
778 Py_DECREF(temp);
779 }
780 /* Pass down previously bound symbols */
781 if (bound) {
782 temp = PyNumber_InPlaceOr(newbound, bound);
783 if (!temp)
784 goto error;
785 Py_DECREF(temp);
786 }
787 /* Pass down known globals */
788 temp = PyNumber_InPlaceOr(newglobal, global);
789 if (!temp)
790 goto error;
791 Py_DECREF(temp);
792 }
793 else {
794 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000795 if (!GET_IDENTIFIER(__class__))
796 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 if (PySet_Add(newbound, __class__) < 0)
798 goto error;
799 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300801 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 newbound, newglobal now contain the names visible in
804 nested blocks. The free variables in the children will
805 be collected in allfree.
806 */
807 allfree = PySet_New(NULL);
808 if (!allfree)
809 goto error;
810 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
811 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
812 PySTEntryObject* entry;
813 assert(c && PySTEntry_Check(c));
814 entry = (PySTEntryObject*)c;
815 if (!analyze_child_block(entry, newbound, newfree, newglobal,
816 allfree))
817 goto error;
818 /* Check if any children have free variables */
819 if (entry->ste_free || entry->ste_child_free)
820 ste->ste_child_free = 1;
821 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 temp = PyNumber_InPlaceOr(newfree, allfree);
824 if (!temp)
825 goto error;
826 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500829 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500831 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 goto error;
833 /* Records the results of the analysis in the symbol table entry */
834 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
835 ste->ste_type == ClassBlock))
836 goto error;
837 if (!check_unoptimized(ste))
838 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 temp = PyNumber_InPlaceOr(free, newfree);
841 if (!temp)
842 goto error;
843 Py_DECREF(temp);
844 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 Py_XDECREF(scopes);
847 Py_XDECREF(local);
848 Py_XDECREF(newbound);
849 Py_XDECREF(newglobal);
850 Py_XDECREF(newfree);
851 Py_XDECREF(allfree);
852 if (!success)
853 assert(PyErr_Occurred());
854 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855}
856
857static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
859 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
862 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 These dictionary are used by all blocks enclosed by the
867 current block. The analyze_block() call modifies these
868 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 */
871 temp_bound = PySet_New(bound);
872 if (!temp_bound)
873 goto error;
874 temp_free = PySet_New(free);
875 if (!temp_free)
876 goto error;
877 temp_global = PySet_New(global);
878 if (!temp_global)
879 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
882 goto error;
883 temp = PyNumber_InPlaceOr(child_free, temp_free);
884 if (!temp)
885 goto error;
886 Py_DECREF(temp);
887 Py_DECREF(temp_bound);
888 Py_DECREF(temp_free);
889 Py_DECREF(temp_global);
890 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000891 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 Py_XDECREF(temp_bound);
893 Py_XDECREF(temp_free);
894 Py_XDECREF(temp_global);
895 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000896}
897
898static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899symtable_analyze(struct symtable *st)
900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 PyObject *free, *global;
902 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 free = PySet_New(NULL);
905 if (!free)
906 return 0;
907 global = PySet_New(NULL);
908 if (!global) {
909 Py_DECREF(free);
910 return 0;
911 }
912 r = analyze_block(st->st_top, NULL, free, global);
913 Py_DECREF(free);
914 Py_DECREF(global);
915 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916}
917
918
919static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000920symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
923 lineno, NULL, NULL) < 0) {
924 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
925 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000926 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
927 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 }
929 return 0;
930 }
931 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932}
933
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000934/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935 This reference is released when the block is exited, via the DECREF
936 in symtable_exit_block().
937*/
938
939static int
940symtable_exit_block(struct symtable *st, void *ast)
941{
Benjamin Peterson609da582011-06-29 22:52:39 -0500942 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943
Benjamin Peterson609da582011-06-29 22:52:39 -0500944 st->st_cur = NULL;
945 size = PyList_GET_SIZE(st->st_stack);
946 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500947 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500949 if (--size)
950 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 }
952 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953}
954
955static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000957 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958{
Benjamin Peterson609da582011-06-29 22:52:39 -0500959 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960
Benjamin Peterson609da582011-06-29 22:52:39 -0500961 ste = ste_new(st, name, block, ast, lineno, col_offset);
962 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500964 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
965 Py_DECREF(ste);
966 return 0;
967 }
968 prev = st->st_cur;
969 /* The entry is owned by the stack. Borrow it for st_cur. */
970 Py_DECREF(ste);
971 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000972 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 st->st_global = st->st_cur->ste_symbols;
974 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500975 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 return 0;
977 }
978 }
979 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980}
981
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000982static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983symtable_lookup(struct symtable *st, PyObject *name)
984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 PyObject *o;
986 PyObject *mangled = _Py_Mangle(st->st_private, name);
987 if (!mangled)
988 return 0;
989 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
990 Py_DECREF(mangled);
991 if (!o)
992 return 0;
993 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994}
995
996static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyObject *o;
1000 PyObject *dict;
1001 long val;
1002 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003
Jeremy Hylton81e95022007-02-27 06:50:52 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 if (!mangled)
1006 return 0;
1007 dict = st->st_cur->ste_symbols;
1008 if ((o = PyDict_GetItem(dict, mangled))) {
1009 val = PyLong_AS_LONG(o);
1010 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1011 /* Is it better to use 'mangled' or 'name' here? */
1012 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001013 PyErr_SyntaxLocationEx(st->st_filename,
1014 st->st_cur->ste_lineno,
1015 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 goto error;
1017 }
1018 val |= flag;
1019 } else
1020 val = flag;
1021 o = PyLong_FromLong(val);
1022 if (o == NULL)
1023 goto error;
1024 if (PyDict_SetItem(dict, mangled, o) < 0) {
1025 Py_DECREF(o);
1026 goto error;
1027 }
1028 Py_DECREF(o);
1029
1030 if (flag & DEF_PARAM) {
1031 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1032 goto error;
1033 } else if (flag & DEF_GLOBAL) {
1034 /* XXX need to update DEF_GLOBAL for other flags too;
1035 perhaps only DEF_FREE_GLOBAL */
1036 val = flag;
1037 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1038 val |= PyLong_AS_LONG(o);
1039 }
1040 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 goto error;
1043 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1044 Py_DECREF(o);
1045 goto error;
1046 }
1047 Py_DECREF(o);
1048 }
1049 Py_DECREF(mangled);
1050 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001051
1052error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 Py_DECREF(mangled);
1054 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055}
1056
1057/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1058 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 function.
1060
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1062 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001063
1064 VISIT_QUIT macro returns the specified value exiting from the function but
1065 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066*/
1067
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001068#define VISIT_QUIT(ST, X) \
1069 return --(ST)->recursion_depth,(X)
1070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001073 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001074
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 int i; \
1077 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1078 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1079 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1080 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001081 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001084
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 int i; \
1087 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1088 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1089 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1090 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001091 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001094
Guido van Rossum4f72a782006-10-27 23:31:49 +00001095#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 int i = 0; \
1097 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1098 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1099 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1100 if (!elt) continue; /* can be NULL */ \
1101 if (!symtable_visit_expr((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001102 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001104}
1105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001107symtable_new_tmpname(struct symtable *st)
1108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 char tmpname[256];
1110 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1113 ++st->st_cur->ste_tmpname);
1114 tmp = PyUnicode_InternFromString(tmpname);
1115 if (!tmp)
1116 return 0;
1117 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1118 return 0;
1119 Py_DECREF(tmp);
1120 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001121}
1122
Guido van Rossum4f72a782006-10-27 23:31:49 +00001123
Guido van Rossumc2e20742006-02-27 22:32:47 +00001124static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001125symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1126{
1127 PyObject *data;
1128 int res;
1129 if (!st->st_cur->ste_directives) {
1130 st->st_cur->ste_directives = PyList_New(0);
1131 if (!st->st_cur->ste_directives)
1132 return 0;
1133 }
1134 data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset);
1135 if (!data)
1136 return 0;
1137 res = PyList_Append(st->st_cur->ste_directives, data);
1138 Py_DECREF(data);
1139 return res == 0;
1140}
1141
1142
1143static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144symtable_visit_stmt(struct symtable *st, stmt_ty s)
1145{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001146 if (++st->recursion_depth > st->recursion_limit) {
1147 PyErr_SetString(PyExc_RuntimeError,
1148 "maximum recursion depth exceeded during compilation");
1149 VISIT_QUIT(st, 0);
1150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 switch (s->kind) {
1152 case FunctionDef_kind:
1153 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001154 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (s->v.FunctionDef.args->defaults)
1156 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1157 if (s->v.FunctionDef.args->kw_defaults)
1158 VISIT_KWONLYDEFAULTS(st,
1159 s->v.FunctionDef.args->kw_defaults);
1160 if (!symtable_visit_annotations(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001161 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (s->v.FunctionDef.decorator_list)
1163 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1164 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001165 FunctionBlock, (void *)s, s->lineno,
1166 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001167 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001168 VISIT(st, arguments, s->v.FunctionDef.args);
1169 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001171 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 break;
1173 case ClassDef_kind: {
1174 PyObject *tmp;
1175 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001176 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1178 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1179 if (s->v.ClassDef.starargs)
1180 VISIT(st, expr, s->v.ClassDef.starargs);
1181 if (s->v.ClassDef.kwargs)
1182 VISIT(st, expr, s->v.ClassDef.kwargs);
1183 if (s->v.ClassDef.decorator_list)
1184 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1185 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001186 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001187 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 tmp = st->st_private;
1189 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001190 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 st->st_private = tmp;
1192 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001193 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 break;
1195 }
1196 case Return_kind:
1197 if (s->v.Return.value) {
1198 VISIT(st, expr, s->v.Return.value);
1199 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 }
1201 break;
1202 case Delete_kind:
1203 VISIT_SEQ(st, expr, s->v.Delete.targets);
1204 break;
1205 case Assign_kind:
1206 VISIT_SEQ(st, expr, s->v.Assign.targets);
1207 VISIT(st, expr, s->v.Assign.value);
1208 break;
1209 case AugAssign_kind:
1210 VISIT(st, expr, s->v.AugAssign.target);
1211 VISIT(st, expr, s->v.AugAssign.value);
1212 break;
1213 case For_kind:
1214 VISIT(st, expr, s->v.For.target);
1215 VISIT(st, expr, s->v.For.iter);
1216 VISIT_SEQ(st, stmt, s->v.For.body);
1217 if (s->v.For.orelse)
1218 VISIT_SEQ(st, stmt, s->v.For.orelse);
1219 break;
1220 case While_kind:
1221 VISIT(st, expr, s->v.While.test);
1222 VISIT_SEQ(st, stmt, s->v.While.body);
1223 if (s->v.While.orelse)
1224 VISIT_SEQ(st, stmt, s->v.While.orelse);
1225 break;
1226 case If_kind:
1227 /* XXX if 0: and lookup_yield() hacks */
1228 VISIT(st, expr, s->v.If.test);
1229 VISIT_SEQ(st, stmt, s->v.If.body);
1230 if (s->v.If.orelse)
1231 VISIT_SEQ(st, stmt, s->v.If.orelse);
1232 break;
1233 case Raise_kind:
1234 if (s->v.Raise.exc) {
1235 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001236 if (s->v.Raise.cause) {
1237 VISIT(st, expr, s->v.Raise.cause);
1238 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 }
1240 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001241 case Try_kind:
1242 VISIT_SEQ(st, stmt, s->v.Try.body);
1243 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1244 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1245 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 break;
1247 case Assert_kind:
1248 VISIT(st, expr, s->v.Assert.test);
1249 if (s->v.Assert.msg)
1250 VISIT(st, expr, s->v.Assert.msg);
1251 break;
1252 case Import_kind:
1253 VISIT_SEQ(st, alias, s->v.Import.names);
1254 /* XXX Don't have the lineno available inside
1255 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001256 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001258 st->st_cur->ste_opt_col_offset = s->col_offset;
1259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 break;
1261 case ImportFrom_kind:
1262 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1263 /* XXX Don't have the lineno available inside
1264 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001265 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001267 st->st_cur->ste_opt_col_offset = s->col_offset;
1268 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 break;
1270 case Global_kind: {
1271 int i;
1272 asdl_seq *seq = s->v.Global.names;
1273 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1274 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 long cur = symtable_lookup(st, name);
1276 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001277 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 if (cur & (DEF_LOCAL | USE)) {
1279 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001280 char *c_name = _PyUnicode_AsString(name);
1281 if (!c_name)
1282 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (cur & DEF_LOCAL)
1284 PyOS_snprintf(buf, sizeof(buf),
1285 GLOBAL_AFTER_ASSIGN,
1286 c_name);
1287 else
1288 PyOS_snprintf(buf, sizeof(buf),
1289 GLOBAL_AFTER_USE,
1290 c_name);
1291 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001292 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 }
1294 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001295 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001296 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001297 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 }
1299 break;
1300 }
1301 case Nonlocal_kind: {
1302 int i;
1303 asdl_seq *seq = s->v.Nonlocal.names;
1304 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1305 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 long cur = symtable_lookup(st, name);
1307 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001308 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 if (cur & (DEF_LOCAL | USE)) {
1310 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001311 char *c_name = _PyUnicode_AsString(name);
1312 if (!c_name)
1313 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 if (cur & DEF_LOCAL)
1315 PyOS_snprintf(buf, sizeof(buf),
1316 NONLOCAL_AFTER_ASSIGN,
1317 c_name);
1318 else
1319 PyOS_snprintf(buf, sizeof(buf),
1320 NONLOCAL_AFTER_USE,
1321 c_name);
1322 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001323 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 }
1325 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001326 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001327 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001328 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 }
1330 break;
1331 }
1332 case Expr_kind:
1333 VISIT(st, expr, s->v.Expr.value);
1334 break;
1335 case Pass_kind:
1336 case Break_kind:
1337 case Continue_kind:
1338 /* nothing to do here */
1339 break;
1340 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001341 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 VISIT_SEQ(st, stmt, s->v.With.body);
1343 break;
1344 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001345 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346}
1347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349symtable_visit_expr(struct symtable *st, expr_ty e)
1350{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001351 if (++st->recursion_depth > st->recursion_limit) {
1352 PyErr_SetString(PyExc_RuntimeError,
1353 "maximum recursion depth exceeded during compilation");
1354 VISIT_QUIT(st, 0);
1355 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 switch (e->kind) {
1357 case BoolOp_kind:
1358 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1359 break;
1360 case BinOp_kind:
1361 VISIT(st, expr, e->v.BinOp.left);
1362 VISIT(st, expr, e->v.BinOp.right);
1363 break;
1364 case UnaryOp_kind:
1365 VISIT(st, expr, e->v.UnaryOp.operand);
1366 break;
1367 case Lambda_kind: {
1368 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001369 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if (e->v.Lambda.args->defaults)
1371 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001372 if (e->v.Lambda.args->kw_defaults)
1373 VISIT_KWONLYDEFAULTS(st,
1374 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001376 FunctionBlock, (void *)e, e->lineno,
1377 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001378 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001379 VISIT(st, arguments, e->v.Lambda.args);
1380 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001382 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 break;
1384 }
1385 case IfExp_kind:
1386 VISIT(st, expr, e->v.IfExp.test);
1387 VISIT(st, expr, e->v.IfExp.body);
1388 VISIT(st, expr, e->v.IfExp.orelse);
1389 break;
1390 case Dict_kind:
1391 VISIT_SEQ(st, expr, e->v.Dict.keys);
1392 VISIT_SEQ(st, expr, e->v.Dict.values);
1393 break;
1394 case Set_kind:
1395 VISIT_SEQ(st, expr, e->v.Set.elts);
1396 break;
1397 case GeneratorExp_kind:
1398 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001399 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 break;
1401 case ListComp_kind:
1402 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001403 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 break;
1405 case SetComp_kind:
1406 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001407 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 break;
1409 case DictComp_kind:
1410 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001411 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 break;
1413 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001414 if (e->v.Yield.value)
1415 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001418 case YieldFrom_kind:
1419 VISIT(st, expr, e->v.YieldFrom.value);
1420 st->st_cur->ste_generator = 1;
1421 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 case Compare_kind:
1423 VISIT(st, expr, e->v.Compare.left);
1424 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1425 break;
1426 case Call_kind:
1427 VISIT(st, expr, e->v.Call.func);
1428 VISIT_SEQ(st, expr, e->v.Call.args);
1429 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1430 if (e->v.Call.starargs)
1431 VISIT(st, expr, e->v.Call.starargs);
1432 if (e->v.Call.kwargs)
1433 VISIT(st, expr, e->v.Call.kwargs);
1434 break;
1435 case Num_kind:
1436 case Str_kind:
1437 case Bytes_kind:
1438 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001439 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 /* Nothing to do here. */
1441 break;
1442 /* The following exprs can be assignment targets. */
1443 case Attribute_kind:
1444 VISIT(st, expr, e->v.Attribute.value);
1445 break;
1446 case Subscript_kind:
1447 VISIT(st, expr, e->v.Subscript.value);
1448 VISIT(st, slice, e->v.Subscript.slice);
1449 break;
1450 case Starred_kind:
1451 VISIT(st, expr, e->v.Starred.value);
1452 break;
1453 case Name_kind:
1454 if (!symtable_add_def(st, e->v.Name.id,
1455 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001456 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 /* Special-case super: it counts as a use of __class__ */
1458 if (e->v.Name.ctx == Load &&
1459 st->st_cur->ste_type == FunctionBlock &&
1460 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001461 if (!GET_IDENTIFIER(__class__) ||
1462 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001463 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 }
1465 break;
1466 /* child nodes of List and Tuple will have expr_context set */
1467 case List_kind:
1468 VISIT_SEQ(st, expr, e->v.List.elts);
1469 break;
1470 case Tuple_kind:
1471 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1472 break;
1473 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001474 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475}
1476
1477static int
1478symtable_implicit_arg(struct symtable *st, int pos)
1479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1481 if (id == NULL)
1482 return 0;
1483 if (!symtable_add_def(st, id, DEF_PARAM)) {
1484 Py_DECREF(id);
1485 return 0;
1486 }
1487 Py_DECREF(id);
1488 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489}
1490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001492symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 if (!args)
1497 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 for (i = 0; i < asdl_seq_LEN(args); i++) {
1500 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1501 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1502 return 0;
1503 }
1504
1505 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506}
1507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001509symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 if (!args)
1514 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 for (i = 0; i < asdl_seq_LEN(args); i++) {
1517 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1518 if (arg->annotation)
1519 VISIT(st, expr, arg->annotation);
1520 }
1521
1522 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001523}
1524
Neal Norwitzc1505362006-12-28 06:47:50 +00001525static int
1526symtable_visit_annotations(struct symtable *st, stmt_ty s)
1527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 arguments_ty a = s->v.FunctionDef.args;
1529
1530 if (a->args && !symtable_visit_argannotations(st, a->args))
1531 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001532 if (a->vararg && a->vararg->annotation)
1533 VISIT(st, expr, a->vararg->annotation);
1534 if (a->kwarg && a->kwarg->annotation)
1535 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1537 return 0;
1538 if (s->v.FunctionDef.returns)
1539 VISIT(st, expr, s->v.FunctionDef.returns);
1540 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541}
1542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544symtable_visit_arguments(struct symtable *st, arguments_ty a)
1545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 /* skip default arguments inside function block
1547 XXX should ast be different?
1548 */
1549 if (a->args && !symtable_visit_params(st, a->args))
1550 return 0;
1551 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1552 return 0;
1553 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001554 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 return 0;
1556 st->st_cur->ste_varargs = 1;
1557 }
1558 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001559 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 return 0;
1561 st->st_cur->ste_varkeywords = 1;
1562 }
1563 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564}
1565
1566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (eh->v.ExceptHandler.type)
1571 VISIT(st, expr, eh->v.ExceptHandler.type);
1572 if (eh->v.ExceptHandler.name)
1573 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1574 return 0;
1575 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1576 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577}
1578
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001579static int
1580symtable_visit_withitem(struct symtable *st, withitem_ty item)
1581{
1582 VISIT(st, expr, item->context_expr);
1583 if (item->optional_vars) {
1584 VISIT(st, expr, item->optional_vars);
1585 }
1586 return 1;
1587}
1588
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591symtable_visit_alias(struct symtable *st, alias_ty a)
1592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001594 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 dotted package name (e.g. spam.eggs)
1596 */
1597 PyObject *store_name;
1598 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001599 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1600 PyUnicode_GET_LENGTH(name), 1);
1601 if (dot != -1) {
1602 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 if (!store_name)
1604 return 0;
1605 }
1606 else {
1607 store_name = name;
1608 Py_INCREF(store_name);
1609 }
1610 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1611 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1612 Py_DECREF(store_name);
1613 return r;
1614 }
1615 else {
1616 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001617 int lineno = st->st_cur->ste_lineno;
1618 int col_offset = st->st_cur->ste_col_offset;
1619 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1620 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1621 Py_DECREF(store_name);
1622 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 }
1624 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1625 Py_DECREF(store_name);
1626 return 1;
1627 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628}
1629
1630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 VISIT(st, expr, lc->target);
1635 VISIT(st, expr, lc->iter);
1636 VISIT_SEQ(st, expr, lc->ifs);
1637 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638}
1639
1640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642symtable_visit_keyword(struct symtable *st, keyword_ty k)
1643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 VISIT(st, expr, k->value);
1645 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646}
1647
1648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650symtable_visit_slice(struct symtable *st, slice_ty s)
1651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 switch (s->kind) {
1653 case Slice_kind:
1654 if (s->v.Slice.lower)
1655 VISIT(st, expr, s->v.Slice.lower)
1656 if (s->v.Slice.upper)
1657 VISIT(st, expr, s->v.Slice.upper)
1658 if (s->v.Slice.step)
1659 VISIT(st, expr, s->v.Slice.step)
1660 break;
1661 case ExtSlice_kind:
1662 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1663 break;
1664 case Index_kind:
1665 VISIT(st, expr, s->v.Index.value)
1666 break;
1667 }
1668 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669}
1670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001672symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001673 identifier scope_name, asdl_seq *generators,
1674 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 int is_generator = (e->kind == GeneratorExp_kind);
1677 int needs_tmp = !is_generator;
1678 comprehension_ty outermost = ((comprehension_ty)
1679 asdl_seq_GET(generators, 0));
1680 /* Outermost iterator is evaluated in current scope */
1681 VISIT(st, expr, outermost->iter);
1682 /* Create comprehension scope for the rest */
1683 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001684 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1685 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 return 0;
1687 }
1688 st->st_cur->ste_generator = is_generator;
1689 /* Outermost iter is received as an argument */
1690 if (!symtable_implicit_arg(st, 0)) {
1691 symtable_exit_block(st, (void *)e);
1692 return 0;
1693 }
1694 /* Allocate temporary name if needed */
1695 if (needs_tmp && !symtable_new_tmpname(st)) {
1696 symtable_exit_block(st, (void *)e);
1697 return 0;
1698 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001699 VISIT(st, expr, outermost->target);
1700 VISIT_SEQ(st, expr, outermost->ifs);
1701 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001703 VISIT(st, expr, value);
1704 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001709symtable_visit_genexp(struct symtable *st, expr_ty e)
1710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1712 e->v.GeneratorExp.generators,
1713 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001714}
1715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001717symtable_visit_listcomp(struct symtable *st, expr_ty e)
1718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1720 e->v.ListComp.generators,
1721 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001722}
1723
1724static int
1725symtable_visit_setcomp(struct symtable *st, expr_ty e)
1726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1728 e->v.SetComp.generators,
1729 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001730}
1731
1732static int
1733symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1736 e->v.DictComp.generators,
1737 e->v.DictComp.key,
1738 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001739}