blob: 812c052a51735fd5ef261c62f76d298aca1c94de [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
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001086#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001088 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001090 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001092 if (!symtable_visit_ ## TYPE((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)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001149 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 if (!symtable_visit_annotations(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001151 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (s->v.FunctionDef.decorator_list)
1153 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1154 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001155 FunctionBlock, (void *)s, s->lineno,
1156 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001157 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001158 VISIT(st, arguments, s->v.FunctionDef.args);
1159 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001161 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 break;
1163 case ClassDef_kind: {
1164 PyObject *tmp;
1165 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001166 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1168 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 if (s->v.ClassDef.decorator_list)
1170 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1171 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001172 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001173 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 tmp = st->st_private;
1175 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001176 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 st->st_private = tmp;
1178 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001179 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 break;
1181 }
1182 case Return_kind:
1183 if (s->v.Return.value) {
1184 VISIT(st, expr, s->v.Return.value);
1185 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 }
1187 break;
1188 case Delete_kind:
1189 VISIT_SEQ(st, expr, s->v.Delete.targets);
1190 break;
1191 case Assign_kind:
1192 VISIT_SEQ(st, expr, s->v.Assign.targets);
1193 VISIT(st, expr, s->v.Assign.value);
1194 break;
1195 case AugAssign_kind:
1196 VISIT(st, expr, s->v.AugAssign.target);
1197 VISIT(st, expr, s->v.AugAssign.value);
1198 break;
1199 case For_kind:
1200 VISIT(st, expr, s->v.For.target);
1201 VISIT(st, expr, s->v.For.iter);
1202 VISIT_SEQ(st, stmt, s->v.For.body);
1203 if (s->v.For.orelse)
1204 VISIT_SEQ(st, stmt, s->v.For.orelse);
1205 break;
1206 case While_kind:
1207 VISIT(st, expr, s->v.While.test);
1208 VISIT_SEQ(st, stmt, s->v.While.body);
1209 if (s->v.While.orelse)
1210 VISIT_SEQ(st, stmt, s->v.While.orelse);
1211 break;
1212 case If_kind:
1213 /* XXX if 0: and lookup_yield() hacks */
1214 VISIT(st, expr, s->v.If.test);
1215 VISIT_SEQ(st, stmt, s->v.If.body);
1216 if (s->v.If.orelse)
1217 VISIT_SEQ(st, stmt, s->v.If.orelse);
1218 break;
1219 case Raise_kind:
1220 if (s->v.Raise.exc) {
1221 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001222 if (s->v.Raise.cause) {
1223 VISIT(st, expr, s->v.Raise.cause);
1224 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 }
1226 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001227 case Try_kind:
1228 VISIT_SEQ(st, stmt, s->v.Try.body);
1229 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1230 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1231 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 break;
1233 case Assert_kind:
1234 VISIT(st, expr, s->v.Assert.test);
1235 if (s->v.Assert.msg)
1236 VISIT(st, expr, s->v.Assert.msg);
1237 break;
1238 case Import_kind:
1239 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 break;
1241 case ImportFrom_kind:
1242 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 break;
1244 case Global_kind: {
1245 int i;
1246 asdl_seq *seq = s->v.Global.names;
1247 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1248 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 long cur = symtable_lookup(st, name);
1250 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001251 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 if (cur & (DEF_LOCAL | USE)) {
1253 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001254 char *c_name = _PyUnicode_AsString(name);
1255 if (!c_name)
1256 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (cur & DEF_LOCAL)
1258 PyOS_snprintf(buf, sizeof(buf),
1259 GLOBAL_AFTER_ASSIGN,
1260 c_name);
1261 else
1262 PyOS_snprintf(buf, sizeof(buf),
1263 GLOBAL_AFTER_USE,
1264 c_name);
1265 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001266 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 }
1268 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001269 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001270 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001271 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 }
1273 break;
1274 }
1275 case Nonlocal_kind: {
1276 int i;
1277 asdl_seq *seq = s->v.Nonlocal.names;
1278 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1279 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 long cur = symtable_lookup(st, name);
1281 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001282 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (cur & (DEF_LOCAL | USE)) {
1284 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001285 char *c_name = _PyUnicode_AsString(name);
1286 if (!c_name)
1287 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (cur & DEF_LOCAL)
1289 PyOS_snprintf(buf, sizeof(buf),
1290 NONLOCAL_AFTER_ASSIGN,
1291 c_name);
1292 else
1293 PyOS_snprintf(buf, sizeof(buf),
1294 NONLOCAL_AFTER_USE,
1295 c_name);
1296 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001297 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 }
1299 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001300 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001301 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001302 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 }
1304 break;
1305 }
1306 case Expr_kind:
1307 VISIT(st, expr, s->v.Expr.value);
1308 break;
1309 case Pass_kind:
1310 case Break_kind:
1311 case Continue_kind:
1312 /* nothing to do here */
1313 break;
1314 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001315 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 VISIT_SEQ(st, stmt, s->v.With.body);
1317 break;
1318 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001319 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320}
1321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323symtable_visit_expr(struct symtable *st, expr_ty e)
1324{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001325 if (++st->recursion_depth > st->recursion_limit) {
1326 PyErr_SetString(PyExc_RuntimeError,
1327 "maximum recursion depth exceeded during compilation");
1328 VISIT_QUIT(st, 0);
1329 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 switch (e->kind) {
1331 case BoolOp_kind:
1332 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1333 break;
1334 case BinOp_kind:
1335 VISIT(st, expr, e->v.BinOp.left);
1336 VISIT(st, expr, e->v.BinOp.right);
1337 break;
1338 case UnaryOp_kind:
1339 VISIT(st, expr, e->v.UnaryOp.operand);
1340 break;
1341 case Lambda_kind: {
1342 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001343 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (e->v.Lambda.args->defaults)
1345 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001346 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001347 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001349 FunctionBlock, (void *)e, e->lineno,
1350 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001351 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001352 VISIT(st, arguments, e->v.Lambda.args);
1353 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001355 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 break;
1357 }
1358 case IfExp_kind:
1359 VISIT(st, expr, e->v.IfExp.test);
1360 VISIT(st, expr, e->v.IfExp.body);
1361 VISIT(st, expr, e->v.IfExp.orelse);
1362 break;
1363 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001364 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 VISIT_SEQ(st, expr, e->v.Dict.values);
1366 break;
1367 case Set_kind:
1368 VISIT_SEQ(st, expr, e->v.Set.elts);
1369 break;
1370 case GeneratorExp_kind:
1371 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001372 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 break;
1374 case ListComp_kind:
1375 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001376 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 break;
1378 case SetComp_kind:
1379 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001380 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 break;
1382 case DictComp_kind:
1383 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001384 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 break;
1386 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001387 if (e->v.Yield.value)
1388 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001391 case YieldFrom_kind:
1392 VISIT(st, expr, e->v.YieldFrom.value);
1393 st->st_cur->ste_generator = 1;
1394 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 case Compare_kind:
1396 VISIT(st, expr, e->v.Compare.left);
1397 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1398 break;
1399 case Call_kind:
1400 VISIT(st, expr, e->v.Call.func);
1401 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001402 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 break;
1404 case Num_kind:
1405 case Str_kind:
1406 case Bytes_kind:
1407 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001408 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 /* Nothing to do here. */
1410 break;
1411 /* The following exprs can be assignment targets. */
1412 case Attribute_kind:
1413 VISIT(st, expr, e->v.Attribute.value);
1414 break;
1415 case Subscript_kind:
1416 VISIT(st, expr, e->v.Subscript.value);
1417 VISIT(st, slice, e->v.Subscript.slice);
1418 break;
1419 case Starred_kind:
1420 VISIT(st, expr, e->v.Starred.value);
1421 break;
1422 case Name_kind:
1423 if (!symtable_add_def(st, e->v.Name.id,
1424 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001425 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 /* Special-case super: it counts as a use of __class__ */
1427 if (e->v.Name.ctx == Load &&
1428 st->st_cur->ste_type == FunctionBlock &&
1429 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001430 if (!GET_IDENTIFIER(__class__) ||
1431 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001432 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 }
1434 break;
1435 /* child nodes of List and Tuple will have expr_context set */
1436 case List_kind:
1437 VISIT_SEQ(st, expr, e->v.List.elts);
1438 break;
1439 case Tuple_kind:
1440 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1441 break;
1442 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001443 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444}
1445
1446static int
1447symtable_implicit_arg(struct symtable *st, int pos)
1448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1450 if (id == NULL)
1451 return 0;
1452 if (!symtable_add_def(st, id, DEF_PARAM)) {
1453 Py_DECREF(id);
1454 return 0;
1455 }
1456 Py_DECREF(id);
1457 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458}
1459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001461symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 if (!args)
1466 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 for (i = 0; i < asdl_seq_LEN(args); i++) {
1469 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1470 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1471 return 0;
1472 }
1473
1474 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475}
1476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001478symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 if (!args)
1483 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 for (i = 0; i < asdl_seq_LEN(args); i++) {
1486 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1487 if (arg->annotation)
1488 VISIT(st, expr, arg->annotation);
1489 }
1490
1491 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001492}
1493
Neal Norwitzc1505362006-12-28 06:47:50 +00001494static int
1495symtable_visit_annotations(struct symtable *st, stmt_ty s)
1496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 arguments_ty a = s->v.FunctionDef.args;
1498
1499 if (a->args && !symtable_visit_argannotations(st, a->args))
1500 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001501 if (a->vararg && a->vararg->annotation)
1502 VISIT(st, expr, a->vararg->annotation);
1503 if (a->kwarg && a->kwarg->annotation)
1504 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1506 return 0;
1507 if (s->v.FunctionDef.returns)
1508 VISIT(st, expr, s->v.FunctionDef.returns);
1509 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510}
1511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513symtable_visit_arguments(struct symtable *st, arguments_ty a)
1514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 /* skip default arguments inside function block
1516 XXX should ast be different?
1517 */
1518 if (a->args && !symtable_visit_params(st, a->args))
1519 return 0;
1520 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1521 return 0;
1522 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001523 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 return 0;
1525 st->st_cur->ste_varargs = 1;
1526 }
1527 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001528 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 return 0;
1530 st->st_cur->ste_varkeywords = 1;
1531 }
1532 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533}
1534
1535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 if (eh->v.ExceptHandler.type)
1540 VISIT(st, expr, eh->v.ExceptHandler.type);
1541 if (eh->v.ExceptHandler.name)
1542 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1543 return 0;
1544 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1545 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546}
1547
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001548static int
1549symtable_visit_withitem(struct symtable *st, withitem_ty item)
1550{
1551 VISIT(st, expr, item->context_expr);
1552 if (item->optional_vars) {
1553 VISIT(st, expr, item->optional_vars);
1554 }
1555 return 1;
1556}
1557
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560symtable_visit_alias(struct symtable *st, alias_ty a)
1561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001563 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 dotted package name (e.g. spam.eggs)
1565 */
1566 PyObject *store_name;
1567 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001568 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1569 PyUnicode_GET_LENGTH(name), 1);
1570 if (dot != -1) {
1571 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 if (!store_name)
1573 return 0;
1574 }
1575 else {
1576 store_name = name;
1577 Py_INCREF(store_name);
1578 }
1579 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1580 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1581 Py_DECREF(store_name);
1582 return r;
1583 }
1584 else {
1585 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001586 int lineno = st->st_cur->ste_lineno;
1587 int col_offset = st->st_cur->ste_col_offset;
1588 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001589 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001590 Py_DECREF(store_name);
1591 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 Py_DECREF(store_name);
1594 return 1;
1595 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596}
1597
1598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 VISIT(st, expr, lc->target);
1603 VISIT(st, expr, lc->iter);
1604 VISIT_SEQ(st, expr, lc->ifs);
1605 return 1;
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_keyword(struct symtable *st, keyword_ty k)
1611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 VISIT(st, expr, k->value);
1613 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614}
1615
1616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618symtable_visit_slice(struct symtable *st, slice_ty s)
1619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 switch (s->kind) {
1621 case Slice_kind:
1622 if (s->v.Slice.lower)
1623 VISIT(st, expr, s->v.Slice.lower)
1624 if (s->v.Slice.upper)
1625 VISIT(st, expr, s->v.Slice.upper)
1626 if (s->v.Slice.step)
1627 VISIT(st, expr, s->v.Slice.step)
1628 break;
1629 case ExtSlice_kind:
1630 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1631 break;
1632 case Index_kind:
1633 VISIT(st, expr, s->v.Index.value)
1634 break;
1635 }
1636 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637}
1638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001640symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001641 identifier scope_name, asdl_seq *generators,
1642 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 int is_generator = (e->kind == GeneratorExp_kind);
1645 int needs_tmp = !is_generator;
1646 comprehension_ty outermost = ((comprehension_ty)
1647 asdl_seq_GET(generators, 0));
1648 /* Outermost iterator is evaluated in current scope */
1649 VISIT(st, expr, outermost->iter);
1650 /* Create comprehension scope for the rest */
1651 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001652 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1653 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 return 0;
1655 }
1656 st->st_cur->ste_generator = is_generator;
1657 /* Outermost iter is received as an argument */
1658 if (!symtable_implicit_arg(st, 0)) {
1659 symtable_exit_block(st, (void *)e);
1660 return 0;
1661 }
1662 /* Allocate temporary name if needed */
1663 if (needs_tmp && !symtable_new_tmpname(st)) {
1664 symtable_exit_block(st, (void *)e);
1665 return 0;
1666 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001667 VISIT(st, expr, outermost->target);
1668 VISIT_SEQ(st, expr, outermost->ifs);
1669 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001671 VISIT(st, expr, value);
1672 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001677symtable_visit_genexp(struct symtable *st, expr_ty e)
1678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1680 e->v.GeneratorExp.generators,
1681 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001682}
1683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001685symtable_visit_listcomp(struct symtable *st, expr_ty e)
1686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1688 e->v.ListComp.generators,
1689 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001690}
1691
1692static int
1693symtable_visit_setcomp(struct symtable *st, expr_ty e)
1694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1696 e->v.SetComp.generators,
1697 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001698}
1699
1700static int
1701symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1704 e->v.DictComp.generators,
1705 e->v.DictComp.key,
1706 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001707}