blob: fbbe77f62508f0980cd4cc7f363749262e6079b0 [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 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020041 ste->ste_name = 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
Benjamin Petersond9c87022012-10-31 20:26:20 -040047 ste->ste_directives = NULL;
48
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 ste->ste_nested = 0;
51 ste->ste_free = 0;
52 ste->ste_varargs = 0;
53 ste->ste_varkeywords = 0;
54 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000055 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000056 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000058 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 if (st->st_cur != NULL &&
61 (st->st_cur->ste_nested ||
62 st->st_cur->ste_type == FunctionBlock))
63 ste->ste_nested = 1;
64 ste->ste_child_free = 0;
65 ste->ste_generator = 0;
66 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050067 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000068
Victor Stinner9a4fb662013-07-11 22:49:00 +020069 ste->ste_symbols = PyDict_New();
70 ste->ste_varnames = PyList_New(0);
71 ste->ste_children = PyList_New(0);
72 if (ste->ste_symbols == NULL
73 || ste->ste_varnames == NULL
74 || ste->ste_children == NULL)
75 goto fail;
76
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
78 goto fail;
79
80 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000081 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 Py_XDECREF(ste);
83 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084}
85
86static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
90 ste->ste_name,
91 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092}
93
94static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 ste->ste_table = NULL;
98 Py_XDECREF(ste->ste_id);
99 Py_XDECREF(ste->ste_name);
100 Py_XDECREF(ste->ste_symbols);
101 Py_XDECREF(ste->ste_varnames);
102 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400103 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000105}
106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108
Guido van Rossum6f799372001-09-20 20:46:19 +0000109static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 {"id", T_OBJECT, OFF(ste_id), READONLY},
111 {"name", T_OBJECT, OFF(ste_name), READONLY},
112 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
113 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
114 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 {"nested", T_INT, OFF(ste_nested), READONLY},
116 {"type", T_INT, OFF(ste_type), READONLY},
117 {"lineno", T_INT, OFF(ste_lineno), READONLY},
118 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119};
120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 PyVarObject_HEAD_INIT(&PyType_Type, 0)
123 "symtable entry",
124 sizeof(PySTEntryObject),
125 0,
126 (destructor)ste_dealloc, /* tp_dealloc */
127 0, /* tp_print */
128 0, /* tp_getattr */
129 0, /* tp_setattr */
130 0, /* tp_reserved */
131 (reprfunc)ste_repr, /* tp_repr */
132 0, /* tp_as_number */
133 0, /* tp_as_sequence */
134 0, /* tp_as_mapping */
135 0, /* tp_hash */
136 0, /* tp_call */
137 0, /* tp_str */
138 PyObject_GenericGetAttr, /* tp_getattro */
139 0, /* tp_setattro */
140 0, /* tp_as_buffer */
141 Py_TPFLAGS_DEFAULT, /* tp_flags */
142 0, /* tp_doc */
143 0, /* tp_traverse */
144 0, /* tp_clear */
145 0, /* tp_richcompare */
146 0, /* tp_weaklistoffset */
147 0, /* tp_iter */
148 0, /* tp_iternext */
149 0, /* tp_methods */
150 ste_memberlist, /* tp_members */
151 0, /* tp_getset */
152 0, /* tp_base */
153 0, /* tp_dict */
154 0, /* tp_descr_get */
155 0, /* tp_descr_set */
156 0, /* tp_dictoffset */
157 0, /* tp_init */
158 0, /* tp_alloc */
159 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000160};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161
162static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000163static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000165 _Py_block_ty block, void *ast, int lineno,
166 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_exit_block(struct symtable *st, void *ast);
168static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
169static int symtable_visit_expr(struct symtable *st, expr_ty s);
170static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000171static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
172static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000173static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static int symtable_visit_arguments(struct symtable *st, arguments_ty);
175static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
176static int symtable_visit_alias(struct symtable *st, alias_ty);
177static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
178static int symtable_visit_keyword(struct symtable *st, keyword_ty);
179static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000180static int symtable_visit_params(struct symtable *st, asdl_seq *args);
181static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000183static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500184static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185
186
Nick Coghlan650f0d02007-04-15 12:05:43 +0000187static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500189 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
194#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000195"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
197static struct symtable *
198symtable_new(void)
199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
203 if (st == NULL)
204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 st->st_filename = NULL;
207 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if ((st->st_stack = PyList_New(0)) == NULL)
210 goto fail;
211 if ((st->st_blocks = PyDict_New()) == NULL)
212 goto fail;
213 st->st_cur = NULL;
214 st->st_private = NULL;
215 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 PySymtable_Free(st);
218 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219}
220
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000221/* When compiling the use of C stack is probably going to be a lot
222 lighter than when executing Python code but still can overflow
223 and causing a Python crash if not checked (e.g. eval("()"*300000)).
224 Using the current recursion limit for the compiler seems too
225 restrictive (it caused at least one test to fail) so a factor is
226 used to allow deeper recursion when compiling an expression.
227
228 Using a scaling factor means this should automatically adjust when
229 the recursion limit is adjusted for small or large C stack allocations.
230*/
231#define COMPILER_STACK_FRAME_SCALE 3
232
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200234PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000236 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 asdl_seq *seq;
238 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000239 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400240 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200243 return NULL;
244 if (filename == NULL) {
245 PySymtable_Free(st);
246 return NULL;
247 }
248 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 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 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400258 /* Be careful here to prevent overflow. */
259 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
260 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
261 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
262 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* Make the initial symbol information gathering pass */
265 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000266 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 PySymtable_Free(st);
268 return NULL;
269 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 switch (mod->kind) {
273 case Module_kind:
274 seq = mod->v.Module.body;
275 for (i = 0; i < asdl_seq_LEN(seq); i++)
276 if (!symtable_visit_stmt(st,
277 (stmt_ty)asdl_seq_GET(seq, i)))
278 goto error;
279 break;
280 case Expression_kind:
281 if (!symtable_visit_expr(st, mod->v.Expression.body))
282 goto error;
283 break;
284 case Interactive_kind:
285 seq = mod->v.Interactive.body;
286 for (i = 0; i < asdl_seq_LEN(seq); i++)
287 if (!symtable_visit_stmt(st,
288 (stmt_ty)asdl_seq_GET(seq, i)))
289 goto error;
290 break;
291 case Suite_kind:
292 PyErr_SetString(PyExc_RuntimeError,
293 "this compiler does not handle Suites");
294 goto error;
295 }
296 if (!symtable_exit_block(st, (void *)mod)) {
297 PySymtable_Free(st);
298 return NULL;
299 }
300 /* Make the second symbol analysis pass */
301 if (symtable_analyze(st))
302 return st;
303 PySymtable_Free(st);
304 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 (void) symtable_exit_block(st, (void *)mod);
307 PySymtable_Free(st);
308 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309}
310
Victor Stinner14e461d2013-08-26 22:28:21 +0200311struct symtable *
312PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
313{
314 PyObject *filename;
315 struct symtable *st;
316 filename = PyUnicode_DecodeFSDefault(filename_str);
317 if (filename == NULL)
318 return NULL;
319 st = PySymtable_BuildObject(mod, filename, future);
320 Py_DECREF(filename);
321 return st;
322}
323
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324void
325PySymtable_Free(struct symtable *st)
326{
Victor Stinner14e461d2013-08-26 22:28:21 +0200327 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 Py_XDECREF(st->st_blocks);
329 Py_XDECREF(st->st_stack);
330 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000331}
332
333PySTEntryObject *
334PySymtable_Lookup(struct symtable *st, void *key)
335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 k = PyLong_FromVoidPtr(key);
339 if (k == NULL)
340 return NULL;
341 v = PyDict_GetItem(st->st_blocks, k);
342 if (v) {
343 assert(PySTEntry_Check(v));
344 Py_INCREF(v);
345 }
346 else {
347 PyErr_SetString(PyExc_KeyError,
348 "unknown symbol table entry");
349 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 Py_DECREF(k);
352 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353}
354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356PyST_GetScope(PySTEntryObject *ste, PyObject *name)
357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
359 if (!v)
360 return 0;
361 assert(PyLong_Check(v));
362 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363}
364
Benjamin Petersond9c87022012-10-31 20:26:20 -0400365static int
366error_at_directive(PySTEntryObject *ste, PyObject *name)
367{
368 Py_ssize_t i;
369 PyObject *data;
370 assert(ste->ste_directives);
371 for (i = 0; ; i++) {
372 data = PyList_GET_ITEM(ste->ste_directives, i);
373 assert(PyTuple_CheckExact(data));
374 if (PyTuple_GET_ITEM(data, 0) == name)
375 break;
376 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200377 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
378 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
379 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
Benjamin Petersond9c87022012-10-31 20:26:20 -0400380 return 0;
381}
382
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383
384/* Analyze raw symbol information to determine scope of each name.
385
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000386 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000392 explicit global is declared with the global statement. An implicit
393 global is a free variable for which the compiler has found no binding
394 in an enclosing function scope. The implicit global is either a global
395 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
396 to handle these names to implement slightly odd semantics. In such a
397 block, the name is treated as global until it is assigned to; then it
398 is treated as a local.
399
400 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000401 The first pass collects raw facts from the AST via the symtable_visit_*
402 functions: the name is a parameter here, the name is used but not defined
403 here, etc. The second pass analyzes these facts during a pass over the
404 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405
406 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000408 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000409 Names which are explicitly declared nonlocal must exist in this set of
410 visible names - if they do not, a syntax error is raised. After doing
411 the local analysis, it analyzes each of its child blocks using an
412 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000413
Nick Coghlan650f0d02007-04-15 12:05:43 +0000414 The children update the free variable set. If a local variable is added to
415 the free variable set by the child, the variable is marked as a cell. The
416 function object being defined must provide runtime storage for the variable
417 that may outlive the function's frame. Cell variables are removed from the
418 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000419
Nick Coghlan650f0d02007-04-15 12:05:43 +0000420 During analysis, the names are:
421 symbols: dict mapping from symbol names to flag values (including offset scope values)
422 scopes: dict mapping from symbol names to scope values (no offset)
423 local: set of all symbol names local to the current scope
424 bound: set of all symbol names local to a containing function scope
425 free: set of all symbol names referenced but not bound in child scopes
426 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427*/
428
429#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 PyObject *o = PyLong_FromLong(I); \
431 if (!o) \
432 return 0; \
433 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
434 Py_DECREF(o); \
435 return 0; \
436 } \
437 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000438}
439
440/* Decide on scope of name, given flags.
441
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000442 The namespace dictionaries may be modified to record information
443 about the new name. For example, a new global will add an entry to
444 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445*/
446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000448analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 PyObject *bound, PyObject *local, PyObject *free,
450 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 if (flags & DEF_GLOBAL) {
453 if (flags & DEF_PARAM) {
454 PyErr_Format(PyExc_SyntaxError,
455 "name '%U' is parameter and global",
456 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400457 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000458 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (flags & DEF_NONLOCAL) {
460 PyErr_Format(PyExc_SyntaxError,
461 "name '%U' is nonlocal and global",
462 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400463 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 }
465 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
466 if (PySet_Add(global, name) < 0)
467 return 0;
468 if (bound && (PySet_Discard(bound, name) < 0))
469 return 0;
470 return 1;
471 }
472 if (flags & DEF_NONLOCAL) {
473 if (flags & DEF_PARAM) {
474 PyErr_Format(PyExc_SyntaxError,
475 "name '%U' is parameter and nonlocal",
476 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400477 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 }
479 if (!bound) {
480 PyErr_Format(PyExc_SyntaxError,
481 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400482 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 }
484 if (!PySet_Contains(bound, name)) {
485 PyErr_Format(PyExc_SyntaxError,
486 "no binding for nonlocal '%U' found",
487 name);
488
Benjamin Petersond9c87022012-10-31 20:26:20 -0400489 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 }
491 SET_SCOPE(scopes, name, FREE);
492 ste->ste_free = 1;
493 return PySet_Add(free, name) >= 0;
494 }
495 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000496 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (PySet_Add(local, name) < 0)
498 return 0;
499 if (PySet_Discard(global, name) < 0)
500 return 0;
501 return 1;
502 }
503 /* If an enclosing block has a binding for this name, it
504 is a free variable rather than a global variable.
505 Note that having a non-NULL bound implies that the block
506 is nested.
507 */
508 if (bound && PySet_Contains(bound, name)) {
509 SET_SCOPE(scopes, name, FREE);
510 ste->ste_free = 1;
511 return PySet_Add(free, name) >= 0;
512 }
513 /* If a parent has a global statement, then call it global
514 explicit? It could also be global implicit.
515 */
516 if (global && PySet_Contains(global, name)) {
517 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
518 return 1;
519 }
520 if (ste->ste_nested)
521 ste->ste_free = 1;
522 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
523 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524}
525
526#undef SET_SCOPE
527
528/* If a name is defined in free and also in locals, then this block
529 provides the binding for the free variable. The name should be
530 marked CELL in this block and removed from the free list.
531
532 Note that the current block's free variables are included in free.
533 That's safe because no name can be free and local in the same scope.
534*/
535
536static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500537analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 PyObject *name, *v, *v_cell;
540 int success = 0;
541 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 v_cell = PyLong_FromLong(CELL);
544 if (!v_cell)
545 return 0;
546 while (PyDict_Next(scopes, &pos, &name, &v)) {
547 long scope;
548 assert(PyLong_Check(v));
549 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000550 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 continue;
552 if (!PySet_Contains(free, name))
553 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /* Replace LOCAL with CELL for this name, and remove
555 from free. It is safe to replace the value of name
556 in the dict, because it will not cause a resize.
557 */
558 if (PyDict_SetItem(scopes, name, v_cell) < 0)
559 goto error;
560 if (PySet_Discard(free, name) < 0)
561 goto error;
562 }
563 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_DECREF(v_cell);
566 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567}
568
Benjamin Peterson312595c2013-05-15 15:26:42 -0500569static int
570drop_class_free(PySTEntryObject *ste, PyObject *free)
571{
572 int res;
573 if (!GET_IDENTIFIER(__class__))
574 return 0;
575 res = PySet_Discard(free, __class__);
576 if (res < 0)
577 return 0;
578 if (res)
579 ste->ste_needs_class_closure = 1;
580 return 1;
581}
582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583/* Enter the final scope information into the ste_symbols dict.
584 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585 * All arguments are dicts. Modifies symbols, others are read-only.
586*/
587static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PyObject *name = NULL, *itr = NULL;
592 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
593 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 /* Update scope information for all symbols in this scope */
596 while (PyDict_Next(symbols, &pos, &name, &v)) {
597 long scope, flags;
598 assert(PyLong_Check(v));
599 flags = PyLong_AS_LONG(v);
600 v_scope = PyDict_GetItem(scopes, name);
601 assert(v_scope && PyLong_Check(v_scope));
602 scope = PyLong_AS_LONG(v_scope);
603 flags |= (scope << SCOPE_OFFSET);
604 v_new = PyLong_FromLong(flags);
605 if (!v_new)
606 return 0;
607 if (PyDict_SetItem(symbols, name, v_new) < 0) {
608 Py_DECREF(v_new);
609 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 Py_DECREF(v_new);
612 }
613
614 /* Record not yet resolved free variables from children (if any) */
615 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
616 if (!v_free)
617 return 0;
618
619 itr = PyObject_GetIter(free);
620 if (!itr)
621 goto error;
622
623 while ((name = PyIter_Next(itr))) {
624 v = PyDict_GetItem(symbols, name);
625
626 /* Handle symbol that already exists in this scope */
627 if (v) {
628 /* Handle a free variable in a method of
629 the class that has the same name as a local
630 or global in the class scope.
631 */
632 if (classflag &&
633 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
634 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
635 v_new = PyLong_FromLong(flags);
636 if (!v_new) {
637 goto error;
638 }
639 if (PyDict_SetItem(symbols, name, v_new) < 0) {
640 Py_DECREF(v_new);
641 goto error;
642 }
643 Py_DECREF(v_new);
644 }
645 /* It's a cell, or already free in this scope */
646 Py_DECREF(name);
647 continue;
648 }
649 /* Handle global symbol */
650 if (!PySet_Contains(bound, name)) {
651 Py_DECREF(name);
652 continue; /* it's a global */
653 }
654 /* Propagate new free symbol up the lexical stack */
655 if (PyDict_SetItem(symbols, name, v_free) < 0) {
656 goto error;
657 }
658 Py_DECREF(name);
659 }
660 Py_DECREF(itr);
661 Py_DECREF(v_free);
662 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000663error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 Py_XDECREF(v_free);
665 Py_XDECREF(itr);
666 Py_XDECREF(name);
667 return 0;
668}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669
670/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000671
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 Arguments:
673 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000674 bound -- set of variables bound in enclosing scopes (input). bound
675 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 free -- set of free variables in enclosed scopes (output)
677 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000678
679 The implementation uses two mutually recursive functions,
680 analyze_block() and analyze_child_block(). analyze_block() is
681 responsible for analyzing the individual names defined in a block.
682 analyze_child_block() prepares temporary namespace dictionaries
683 used to evaluated nested blocks.
684
685 The two functions exist because a child block should see the name
686 bindings of its enclosing blocks, but those bindings should not
687 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688*/
689
690static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
692 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000693
694static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
696 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
699 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
700 PyObject *temp;
701 int i, success = 0;
702 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 local = PySet_New(NULL); /* collect new names bound in block */
705 if (!local)
706 goto error;
707 scopes = PyDict_New(); /* collect scopes defined for each name */
708 if (!scopes)
709 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 /* Allocate new global and bound variable dictionaries. These
712 dictionaries hold the names visible in nested blocks. For
713 ClassBlocks, the bound and global names are initialized
714 before analyzing names, because class bindings aren't
715 visible in methods. For other blocks, they are initialized
716 after names are analyzed.
717 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 /* TODO(jhylton): Package these dicts in a struct so that we
720 can write reasonable helper functions?
721 */
722 newglobal = PySet_New(NULL);
723 if (!newglobal)
724 goto error;
725 newfree = PySet_New(NULL);
726 if (!newfree)
727 goto error;
728 newbound = PySet_New(NULL);
729 if (!newbound)
730 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 /* Class namespace has no effect on names visible in
733 nested functions, so populate the global and bound
734 sets to be passed to child blocks before analyzing
735 this one.
736 */
737 if (ste->ste_type == ClassBlock) {
738 /* Pass down known globals */
739 temp = PyNumber_InPlaceOr(newglobal, global);
740 if (!temp)
741 goto error;
742 Py_DECREF(temp);
743 /* Pass down previously bound symbols */
744 if (bound) {
745 temp = PyNumber_InPlaceOr(newbound, bound);
746 if (!temp)
747 goto error;
748 Py_DECREF(temp);
749 }
750 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
753 long flags = PyLong_AS_LONG(v);
754 if (!analyze_name(ste, scopes, name, flags,
755 bound, local, free, global))
756 goto error;
757 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 /* Populate global and bound sets to be passed to children. */
760 if (ste->ste_type != ClassBlock) {
761 /* Add function locals to bound set */
762 if (ste->ste_type == FunctionBlock) {
763 temp = PyNumber_InPlaceOr(newbound, local);
764 if (!temp)
765 goto error;
766 Py_DECREF(temp);
767 }
768 /* Pass down previously bound symbols */
769 if (bound) {
770 temp = PyNumber_InPlaceOr(newbound, bound);
771 if (!temp)
772 goto error;
773 Py_DECREF(temp);
774 }
775 /* Pass down known globals */
776 temp = PyNumber_InPlaceOr(newglobal, global);
777 if (!temp)
778 goto error;
779 Py_DECREF(temp);
780 }
781 else {
782 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000783 if (!GET_IDENTIFIER(__class__))
784 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 if (PySet_Add(newbound, __class__) < 0)
786 goto error;
787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300789 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 newbound, newglobal now contain the names visible in
792 nested blocks. The free variables in the children will
793 be collected in allfree.
794 */
795 allfree = PySet_New(NULL);
796 if (!allfree)
797 goto error;
798 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
799 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
800 PySTEntryObject* entry;
801 assert(c && PySTEntry_Check(c));
802 entry = (PySTEntryObject*)c;
803 if (!analyze_child_block(entry, newbound, newfree, newglobal,
804 allfree))
805 goto error;
806 /* Check if any children have free variables */
807 if (entry->ste_free || entry->ste_child_free)
808 ste->ste_child_free = 1;
809 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 temp = PyNumber_InPlaceOr(newfree, allfree);
812 if (!temp)
813 goto error;
814 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500817 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500819 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 goto error;
821 /* Records the results of the analysis in the symbol table entry */
822 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
823 ste->ste_type == ClassBlock))
824 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 temp = PyNumber_InPlaceOr(free, newfree);
827 if (!temp)
828 goto error;
829 Py_DECREF(temp);
830 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 Py_XDECREF(scopes);
833 Py_XDECREF(local);
834 Py_XDECREF(newbound);
835 Py_XDECREF(newglobal);
836 Py_XDECREF(newfree);
837 Py_XDECREF(allfree);
838 if (!success)
839 assert(PyErr_Occurred());
840 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841}
842
843static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
845 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
848 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 These dictionary are used by all blocks enclosed by the
853 current block. The analyze_block() call modifies these
854 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 */
857 temp_bound = PySet_New(bound);
858 if (!temp_bound)
859 goto error;
860 temp_free = PySet_New(free);
861 if (!temp_free)
862 goto error;
863 temp_global = PySet_New(global);
864 if (!temp_global)
865 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
868 goto error;
869 temp = PyNumber_InPlaceOr(child_free, temp_free);
870 if (!temp)
871 goto error;
872 Py_DECREF(temp);
873 Py_DECREF(temp_bound);
874 Py_DECREF(temp_free);
875 Py_DECREF(temp_global);
876 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000877 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 Py_XDECREF(temp_bound);
879 Py_XDECREF(temp_free);
880 Py_XDECREF(temp_global);
881 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000882}
883
884static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885symtable_analyze(struct symtable *st)
886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 PyObject *free, *global;
888 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 free = PySet_New(NULL);
891 if (!free)
892 return 0;
893 global = PySet_New(NULL);
894 if (!global) {
895 Py_DECREF(free);
896 return 0;
897 }
898 r = analyze_block(st->st_top, NULL, free, global);
899 Py_DECREF(free);
900 Py_DECREF(global);
901 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902}
903
904
905static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000906symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907{
Victor Stinner14e461d2013-08-26 22:28:21 +0200908 PyObject *message = PyUnicode_FromString(msg);
909 if (message == NULL)
910 return 0;
911 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, message, st->st_filename,
912 lineno, NULL, NULL) < 0) {
913 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
915 PyErr_SetString(PyExc_SyntaxError, msg);
Victor Stinner14e461d2013-08-26 22:28:21 +0200916 PyErr_SyntaxLocationObject(st->st_filename, st->st_cur->ste_lineno,
917 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 }
919 return 0;
920 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200921 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923}
924
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000925/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 This reference is released when the block is exited, via the DECREF
927 in symtable_exit_block().
928*/
929
930static int
931symtable_exit_block(struct symtable *st, void *ast)
932{
Benjamin Peterson609da582011-06-29 22:52:39 -0500933 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934
Benjamin Peterson609da582011-06-29 22:52:39 -0500935 st->st_cur = NULL;
936 size = PyList_GET_SIZE(st->st_stack);
937 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500938 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500940 if (--size)
941 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 }
943 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944}
945
946static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000948 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949{
Benjamin Peterson609da582011-06-29 22:52:39 -0500950 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951
Benjamin Peterson609da582011-06-29 22:52:39 -0500952 ste = ste_new(st, name, block, ast, lineno, col_offset);
953 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500955 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
956 Py_DECREF(ste);
957 return 0;
958 }
959 prev = st->st_cur;
960 /* The entry is owned by the stack. Borrow it for st_cur. */
961 Py_DECREF(ste);
962 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000963 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 st->st_global = st->st_cur->ste_symbols;
965 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500966 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 return 0;
968 }
969 }
970 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971}
972
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000973static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974symtable_lookup(struct symtable *st, PyObject *name)
975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 PyObject *o;
977 PyObject *mangled = _Py_Mangle(st->st_private, name);
978 if (!mangled)
979 return 0;
980 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
981 Py_DECREF(mangled);
982 if (!o)
983 return 0;
984 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985}
986
987static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 PyObject *o;
991 PyObject *dict;
992 long val;
993 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994
Jeremy Hylton81e95022007-02-27 06:50:52 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 if (!mangled)
997 return 0;
998 dict = st->st_cur->ste_symbols;
999 if ((o = PyDict_GetItem(dict, mangled))) {
1000 val = PyLong_AS_LONG(o);
1001 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1002 /* Is it better to use 'mangled' or 'name' here? */
1003 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001004 PyErr_SyntaxLocationObject(st->st_filename,
1005 st->st_cur->ste_lineno,
1006 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 goto error;
1008 }
1009 val |= flag;
1010 } else
1011 val = flag;
1012 o = PyLong_FromLong(val);
1013 if (o == NULL)
1014 goto error;
1015 if (PyDict_SetItem(dict, mangled, o) < 0) {
1016 Py_DECREF(o);
1017 goto error;
1018 }
1019 Py_DECREF(o);
1020
1021 if (flag & DEF_PARAM) {
1022 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1023 goto error;
1024 } else if (flag & DEF_GLOBAL) {
1025 /* XXX need to update DEF_GLOBAL for other flags too;
1026 perhaps only DEF_FREE_GLOBAL */
1027 val = flag;
1028 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1029 val |= PyLong_AS_LONG(o);
1030 }
1031 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 goto error;
1034 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1035 Py_DECREF(o);
1036 goto error;
1037 }
1038 Py_DECREF(o);
1039 }
1040 Py_DECREF(mangled);
1041 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001042
1043error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 Py_DECREF(mangled);
1045 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046}
1047
1048/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1049 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 function.
1051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1053 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001054
1055 VISIT_QUIT macro returns the specified value exiting from the function but
1056 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057*/
1058
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001059#define VISIT_QUIT(ST, X) \
1060 return --(ST)->recursion_depth,(X)
1061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001064 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 int i; \
1068 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1069 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1070 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1071 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001072 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001075
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 int i; \
1078 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1079 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1080 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1081 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001082 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001085
Guido van Rossum4f72a782006-10-27 23:31:49 +00001086#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 int i = 0; \
1088 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1089 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1090 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1091 if (!elt) continue; /* can be NULL */ \
1092 if (!symtable_visit_expr((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001093 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001095}
1096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001098symtable_new_tmpname(struct symtable *st)
1099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 char tmpname[256];
1101 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1104 ++st->st_cur->ste_tmpname);
1105 tmp = PyUnicode_InternFromString(tmpname);
1106 if (!tmp)
1107 return 0;
1108 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1109 return 0;
1110 Py_DECREF(tmp);
1111 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001112}
1113
Guido van Rossum4f72a782006-10-27 23:31:49 +00001114
Guido van Rossumc2e20742006-02-27 22:32:47 +00001115static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001116symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1117{
1118 PyObject *data;
1119 int res;
1120 if (!st->st_cur->ste_directives) {
1121 st->st_cur->ste_directives = PyList_New(0);
1122 if (!st->st_cur->ste_directives)
1123 return 0;
1124 }
1125 data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset);
1126 if (!data)
1127 return 0;
1128 res = PyList_Append(st->st_cur->ste_directives, data);
1129 Py_DECREF(data);
1130 return res == 0;
1131}
1132
1133
1134static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135symtable_visit_stmt(struct symtable *st, stmt_ty s)
1136{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001137 if (++st->recursion_depth > st->recursion_limit) {
1138 PyErr_SetString(PyExc_RuntimeError,
1139 "maximum recursion depth exceeded during compilation");
1140 VISIT_QUIT(st, 0);
1141 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 switch (s->kind) {
1143 case FunctionDef_kind:
1144 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001145 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (s->v.FunctionDef.args->defaults)
1147 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1148 if (s->v.FunctionDef.args->kw_defaults)
1149 VISIT_KWONLYDEFAULTS(st,
1150 s->v.FunctionDef.args->kw_defaults);
1151 if (!symtable_visit_annotations(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001152 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (s->v.FunctionDef.decorator_list)
1154 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1155 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001156 FunctionBlock, (void *)s, s->lineno,
1157 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001158 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001159 VISIT(st, arguments, s->v.FunctionDef.args);
1160 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001162 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 break;
1164 case ClassDef_kind: {
1165 PyObject *tmp;
1166 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001167 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1169 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1170 if (s->v.ClassDef.starargs)
1171 VISIT(st, expr, s->v.ClassDef.starargs);
1172 if (s->v.ClassDef.kwargs)
1173 VISIT(st, expr, s->v.ClassDef.kwargs);
1174 if (s->v.ClassDef.decorator_list)
1175 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1176 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001177 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001178 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 tmp = st->st_private;
1180 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001181 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 st->st_private = tmp;
1183 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001184 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 break;
1186 }
1187 case Return_kind:
1188 if (s->v.Return.value) {
1189 VISIT(st, expr, s->v.Return.value);
1190 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 }
1192 break;
1193 case Delete_kind:
1194 VISIT_SEQ(st, expr, s->v.Delete.targets);
1195 break;
1196 case Assign_kind:
1197 VISIT_SEQ(st, expr, s->v.Assign.targets);
1198 VISIT(st, expr, s->v.Assign.value);
1199 break;
1200 case AugAssign_kind:
1201 VISIT(st, expr, s->v.AugAssign.target);
1202 VISIT(st, expr, s->v.AugAssign.value);
1203 break;
1204 case For_kind:
1205 VISIT(st, expr, s->v.For.target);
1206 VISIT(st, expr, s->v.For.iter);
1207 VISIT_SEQ(st, stmt, s->v.For.body);
1208 if (s->v.For.orelse)
1209 VISIT_SEQ(st, stmt, s->v.For.orelse);
1210 break;
1211 case While_kind:
1212 VISIT(st, expr, s->v.While.test);
1213 VISIT_SEQ(st, stmt, s->v.While.body);
1214 if (s->v.While.orelse)
1215 VISIT_SEQ(st, stmt, s->v.While.orelse);
1216 break;
1217 case If_kind:
1218 /* XXX if 0: and lookup_yield() hacks */
1219 VISIT(st, expr, s->v.If.test);
1220 VISIT_SEQ(st, stmt, s->v.If.body);
1221 if (s->v.If.orelse)
1222 VISIT_SEQ(st, stmt, s->v.If.orelse);
1223 break;
1224 case Raise_kind:
1225 if (s->v.Raise.exc) {
1226 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001227 if (s->v.Raise.cause) {
1228 VISIT(st, expr, s->v.Raise.cause);
1229 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 }
1231 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001232 case Try_kind:
1233 VISIT_SEQ(st, stmt, s->v.Try.body);
1234 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1235 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1236 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 break;
1238 case Assert_kind:
1239 VISIT(st, expr, s->v.Assert.test);
1240 if (s->v.Assert.msg)
1241 VISIT(st, expr, s->v.Assert.msg);
1242 break;
1243 case Import_kind:
1244 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 break;
1246 case ImportFrom_kind:
1247 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 break;
1249 case Global_kind: {
1250 int i;
1251 asdl_seq *seq = s->v.Global.names;
1252 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1253 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 long cur = symtable_lookup(st, name);
1255 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001256 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (cur & (DEF_LOCAL | USE)) {
1258 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001259 char *c_name = _PyUnicode_AsString(name);
1260 if (!c_name)
1261 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (cur & DEF_LOCAL)
1263 PyOS_snprintf(buf, sizeof(buf),
1264 GLOBAL_AFTER_ASSIGN,
1265 c_name);
1266 else
1267 PyOS_snprintf(buf, sizeof(buf),
1268 GLOBAL_AFTER_USE,
1269 c_name);
1270 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001271 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 }
1273 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001274 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001275 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001276 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 }
1278 break;
1279 }
1280 case Nonlocal_kind: {
1281 int i;
1282 asdl_seq *seq = s->v.Nonlocal.names;
1283 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1284 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 long cur = symtable_lookup(st, name);
1286 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001287 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (cur & (DEF_LOCAL | USE)) {
1289 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001290 char *c_name = _PyUnicode_AsString(name);
1291 if (!c_name)
1292 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 if (cur & DEF_LOCAL)
1294 PyOS_snprintf(buf, sizeof(buf),
1295 NONLOCAL_AFTER_ASSIGN,
1296 c_name);
1297 else
1298 PyOS_snprintf(buf, sizeof(buf),
1299 NONLOCAL_AFTER_USE,
1300 c_name);
1301 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001302 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 }
1304 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001305 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001306 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001307 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 }
1309 break;
1310 }
1311 case Expr_kind:
1312 VISIT(st, expr, s->v.Expr.value);
1313 break;
1314 case Pass_kind:
1315 case Break_kind:
1316 case Continue_kind:
1317 /* nothing to do here */
1318 break;
1319 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001320 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 VISIT_SEQ(st, stmt, s->v.With.body);
1322 break;
1323 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001324 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325}
1326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001328symtable_visit_expr(struct symtable *st, expr_ty e)
1329{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001330 if (++st->recursion_depth > st->recursion_limit) {
1331 PyErr_SetString(PyExc_RuntimeError,
1332 "maximum recursion depth exceeded during compilation");
1333 VISIT_QUIT(st, 0);
1334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 switch (e->kind) {
1336 case BoolOp_kind:
1337 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1338 break;
1339 case BinOp_kind:
1340 VISIT(st, expr, e->v.BinOp.left);
1341 VISIT(st, expr, e->v.BinOp.right);
1342 break;
1343 case UnaryOp_kind:
1344 VISIT(st, expr, e->v.UnaryOp.operand);
1345 break;
1346 case Lambda_kind: {
1347 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001348 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 if (e->v.Lambda.args->defaults)
1350 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001351 if (e->v.Lambda.args->kw_defaults)
1352 VISIT_KWONLYDEFAULTS(st,
1353 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001355 FunctionBlock, (void *)e, e->lineno,
1356 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001357 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001358 VISIT(st, arguments, e->v.Lambda.args);
1359 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001361 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 break;
1363 }
1364 case IfExp_kind:
1365 VISIT(st, expr, e->v.IfExp.test);
1366 VISIT(st, expr, e->v.IfExp.body);
1367 VISIT(st, expr, e->v.IfExp.orelse);
1368 break;
1369 case Dict_kind:
1370 VISIT_SEQ(st, expr, e->v.Dict.keys);
1371 VISIT_SEQ(st, expr, e->v.Dict.values);
1372 break;
1373 case Set_kind:
1374 VISIT_SEQ(st, expr, e->v.Set.elts);
1375 break;
1376 case GeneratorExp_kind:
1377 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001378 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 break;
1380 case ListComp_kind:
1381 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001382 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 break;
1384 case SetComp_kind:
1385 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001386 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 break;
1388 case DictComp_kind:
1389 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001390 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 break;
1392 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001393 if (e->v.Yield.value)
1394 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001397 case YieldFrom_kind:
1398 VISIT(st, expr, e->v.YieldFrom.value);
1399 st->st_cur->ste_generator = 1;
1400 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 case Compare_kind:
1402 VISIT(st, expr, e->v.Compare.left);
1403 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1404 break;
1405 case Call_kind:
1406 VISIT(st, expr, e->v.Call.func);
1407 VISIT_SEQ(st, expr, e->v.Call.args);
1408 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1409 if (e->v.Call.starargs)
1410 VISIT(st, expr, e->v.Call.starargs);
1411 if (e->v.Call.kwargs)
1412 VISIT(st, expr, e->v.Call.kwargs);
1413 break;
1414 case Num_kind:
1415 case Str_kind:
1416 case Bytes_kind:
1417 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001418 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 /* Nothing to do here. */
1420 break;
1421 /* The following exprs can be assignment targets. */
1422 case Attribute_kind:
1423 VISIT(st, expr, e->v.Attribute.value);
1424 break;
1425 case Subscript_kind:
1426 VISIT(st, expr, e->v.Subscript.value);
1427 VISIT(st, slice, e->v.Subscript.slice);
1428 break;
1429 case Starred_kind:
1430 VISIT(st, expr, e->v.Starred.value);
1431 break;
1432 case Name_kind:
1433 if (!symtable_add_def(st, e->v.Name.id,
1434 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001435 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 /* Special-case super: it counts as a use of __class__ */
1437 if (e->v.Name.ctx == Load &&
1438 st->st_cur->ste_type == FunctionBlock &&
1439 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001440 if (!GET_IDENTIFIER(__class__) ||
1441 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001442 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 }
1444 break;
1445 /* child nodes of List and Tuple will have expr_context set */
1446 case List_kind:
1447 VISIT_SEQ(st, expr, e->v.List.elts);
1448 break;
1449 case Tuple_kind:
1450 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1451 break;
1452 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001453 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454}
1455
1456static int
1457symtable_implicit_arg(struct symtable *st, int pos)
1458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1460 if (id == NULL)
1461 return 0;
1462 if (!symtable_add_def(st, id, DEF_PARAM)) {
1463 Py_DECREF(id);
1464 return 0;
1465 }
1466 Py_DECREF(id);
1467 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468}
1469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001471symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 if (!args)
1476 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 for (i = 0; i < asdl_seq_LEN(args); i++) {
1479 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1480 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1481 return 0;
1482 }
1483
1484 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485}
1486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001488symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 if (!args)
1493 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 for (i = 0; i < asdl_seq_LEN(args); i++) {
1496 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1497 if (arg->annotation)
1498 VISIT(st, expr, arg->annotation);
1499 }
1500
1501 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001502}
1503
Neal Norwitzc1505362006-12-28 06:47:50 +00001504static int
1505symtable_visit_annotations(struct symtable *st, stmt_ty s)
1506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 arguments_ty a = s->v.FunctionDef.args;
1508
1509 if (a->args && !symtable_visit_argannotations(st, a->args))
1510 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001511 if (a->vararg && a->vararg->annotation)
1512 VISIT(st, expr, a->vararg->annotation);
1513 if (a->kwarg && a->kwarg->annotation)
1514 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1516 return 0;
1517 if (s->v.FunctionDef.returns)
1518 VISIT(st, expr, s->v.FunctionDef.returns);
1519 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520}
1521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523symtable_visit_arguments(struct symtable *st, arguments_ty a)
1524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 /* skip default arguments inside function block
1526 XXX should ast be different?
1527 */
1528 if (a->args && !symtable_visit_params(st, a->args))
1529 return 0;
1530 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1531 return 0;
1532 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001533 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 return 0;
1535 st->st_cur->ste_varargs = 1;
1536 }
1537 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001538 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 return 0;
1540 st->st_cur->ste_varkeywords = 1;
1541 }
1542 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543}
1544
1545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 if (eh->v.ExceptHandler.type)
1550 VISIT(st, expr, eh->v.ExceptHandler.type);
1551 if (eh->v.ExceptHandler.name)
1552 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1553 return 0;
1554 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1555 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556}
1557
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001558static int
1559symtable_visit_withitem(struct symtable *st, withitem_ty item)
1560{
1561 VISIT(st, expr, item->context_expr);
1562 if (item->optional_vars) {
1563 VISIT(st, expr, item->optional_vars);
1564 }
1565 return 1;
1566}
1567
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570symtable_visit_alias(struct symtable *st, alias_ty a)
1571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001573 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 dotted package name (e.g. spam.eggs)
1575 */
1576 PyObject *store_name;
1577 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001578 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1579 PyUnicode_GET_LENGTH(name), 1);
1580 if (dot != -1) {
1581 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (!store_name)
1583 return 0;
1584 }
1585 else {
1586 store_name = name;
1587 Py_INCREF(store_name);
1588 }
1589 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1590 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1591 Py_DECREF(store_name);
1592 return r;
1593 }
1594 else {
1595 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001596 int lineno = st->st_cur->ste_lineno;
1597 int col_offset = st->st_cur->ste_col_offset;
1598 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001599 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001600 Py_DECREF(store_name);
1601 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 Py_DECREF(store_name);
1604 return 1;
1605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606}
1607
1608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 VISIT(st, expr, lc->target);
1613 VISIT(st, expr, lc->iter);
1614 VISIT_SEQ(st, expr, lc->ifs);
1615 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616}
1617
1618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620symtable_visit_keyword(struct symtable *st, keyword_ty k)
1621{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 VISIT(st, expr, k->value);
1623 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624}
1625
1626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628symtable_visit_slice(struct symtable *st, slice_ty s)
1629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 switch (s->kind) {
1631 case Slice_kind:
1632 if (s->v.Slice.lower)
1633 VISIT(st, expr, s->v.Slice.lower)
1634 if (s->v.Slice.upper)
1635 VISIT(st, expr, s->v.Slice.upper)
1636 if (s->v.Slice.step)
1637 VISIT(st, expr, s->v.Slice.step)
1638 break;
1639 case ExtSlice_kind:
1640 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1641 break;
1642 case Index_kind:
1643 VISIT(st, expr, s->v.Index.value)
1644 break;
1645 }
1646 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647}
1648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001650symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001651 identifier scope_name, asdl_seq *generators,
1652 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 int is_generator = (e->kind == GeneratorExp_kind);
1655 int needs_tmp = !is_generator;
1656 comprehension_ty outermost = ((comprehension_ty)
1657 asdl_seq_GET(generators, 0));
1658 /* Outermost iterator is evaluated in current scope */
1659 VISIT(st, expr, outermost->iter);
1660 /* Create comprehension scope for the rest */
1661 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001662 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1663 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 return 0;
1665 }
1666 st->st_cur->ste_generator = is_generator;
1667 /* Outermost iter is received as an argument */
1668 if (!symtable_implicit_arg(st, 0)) {
1669 symtable_exit_block(st, (void *)e);
1670 return 0;
1671 }
1672 /* Allocate temporary name if needed */
1673 if (needs_tmp && !symtable_new_tmpname(st)) {
1674 symtable_exit_block(st, (void *)e);
1675 return 0;
1676 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001677 VISIT(st, expr, outermost->target);
1678 VISIT_SEQ(st, expr, outermost->ifs);
1679 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001681 VISIT(st, expr, value);
1682 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001687symtable_visit_genexp(struct symtable *st, expr_ty e)
1688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1690 e->v.GeneratorExp.generators,
1691 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001692}
1693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001695symtable_visit_listcomp(struct symtable *st, expr_ty e)
1696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1698 e->v.ListComp.generators,
1699 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001700}
1701
1702static int
1703symtable_visit_setcomp(struct symtable *st, expr_ty e)
1704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1706 e->v.SetComp.generators,
1707 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001708}
1709
1710static int
1711symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1714 e->v.DictComp.generators,
1715 e->v.DictComp.key,
1716 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001717}