blob: 951267342b900b8c6ad17bf4f15b1ac0ffdbbdc5 [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;
50 ste->ste_unoptimized = 0;
51 ste->ste_nested = 0;
52 ste->ste_free = 0;
53 ste->ste_varargs = 0;
54 ste->ste_varkeywords = 0;
55 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000056 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000057 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000059 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 if (st->st_cur != NULL &&
62 (st->st_cur->ste_nested ||
63 st->st_cur->ste_type == FunctionBlock))
64 ste->ste_nested = 1;
65 ste->ste_child_free = 0;
66 ste->ste_generator = 0;
67 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050068 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069
Victor Stinner9a4fb662013-07-11 22:49:00 +020070 ste->ste_symbols = PyDict_New();
71 ste->ste_varnames = PyList_New(0);
72 ste->ste_children = PyList_New(0);
73 if (ste->ste_symbols == NULL
74 || ste->ste_varnames == NULL
75 || ste->ste_children == NULL)
76 goto fail;
77
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
79 goto fail;
80
81 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 Py_XDECREF(ste);
84 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085}
86
87static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
91 ste->ste_name,
92 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093}
94
95static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 ste->ste_table = NULL;
99 Py_XDECREF(ste->ste_id);
100 Py_XDECREF(ste->ste_name);
101 Py_XDECREF(ste->ste_symbols);
102 Py_XDECREF(ste->ste_varnames);
103 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400104 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000106}
107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109
Guido van Rossum6f799372001-09-20 20:46:19 +0000110static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 {"id", T_OBJECT, OFF(ste_id), READONLY},
112 {"name", T_OBJECT, OFF(ste_name), READONLY},
113 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
114 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
115 {"children", T_OBJECT, OFF(ste_children), READONLY},
116 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
117 {"nested", T_INT, OFF(ste_nested), READONLY},
118 {"type", T_INT, OFF(ste_type), READONLY},
119 {"lineno", T_INT, OFF(ste_lineno), READONLY},
120 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121};
122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 PyVarObject_HEAD_INIT(&PyType_Type, 0)
125 "symtable entry",
126 sizeof(PySTEntryObject),
127 0,
128 (destructor)ste_dealloc, /* tp_dealloc */
129 0, /* tp_print */
130 0, /* tp_getattr */
131 0, /* tp_setattr */
132 0, /* tp_reserved */
133 (reprfunc)ste_repr, /* tp_repr */
134 0, /* tp_as_number */
135 0, /* tp_as_sequence */
136 0, /* tp_as_mapping */
137 0, /* tp_hash */
138 0, /* tp_call */
139 0, /* tp_str */
140 PyObject_GenericGetAttr, /* tp_getattro */
141 0, /* tp_setattro */
142 0, /* tp_as_buffer */
143 Py_TPFLAGS_DEFAULT, /* tp_flags */
144 0, /* tp_doc */
145 0, /* tp_traverse */
146 0, /* tp_clear */
147 0, /* tp_richcompare */
148 0, /* tp_weaklistoffset */
149 0, /* tp_iter */
150 0, /* tp_iternext */
151 0, /* tp_methods */
152 ste_memberlist, /* tp_members */
153 0, /* tp_getset */
154 0, /* tp_base */
155 0, /* tp_dict */
156 0, /* tp_descr_get */
157 0, /* tp_descr_set */
158 0, /* tp_dictoffset */
159 0, /* tp_init */
160 0, /* tp_alloc */
161 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000162};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163
164static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000165static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000167 _Py_block_ty block, void *ast, int lineno,
168 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169static int symtable_exit_block(struct symtable *st, void *ast);
170static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
171static int symtable_visit_expr(struct symtable *st, expr_ty s);
172static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000173static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
174static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000175static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000176static int symtable_visit_arguments(struct symtable *st, arguments_ty);
177static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
178static int symtable_visit_alias(struct symtable *st, alias_ty);
179static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
180static int symtable_visit_keyword(struct symtable *st, keyword_ty);
181static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000182static int symtable_visit_params(struct symtable *st, asdl_seq *args);
183static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000185static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500186static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
188
Nick Coghlan650f0d02007-04-15 12:05:43 +0000189static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500191 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
193#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000197"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
199static struct symtable *
200symtable_new(void)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
205 if (st == NULL)
206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 st->st_filename = NULL;
209 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if ((st->st_stack = PyList_New(0)) == NULL)
212 goto fail;
213 if ((st->st_blocks = PyDict_New()) == NULL)
214 goto fail;
215 st->st_cur = NULL;
216 st->st_private = NULL;
217 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 PySymtable_Free(st);
220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221}
222
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000223/* When compiling the use of C stack is probably going to be a lot
224 lighter than when executing Python code but still can overflow
225 and causing a Python crash if not checked (e.g. eval("()"*300000)).
226 Using the current recursion limit for the compiler seems too
227 restrictive (it caused at least one test to fail) so a factor is
228 used to allow deeper recursion when compiling an expression.
229
230 Using a scaling factor means this should automatically adjust when
231 the recursion limit is adjusted for small or large C stack allocations.
232*/
233#define COMPILER_STACK_FRAME_SCALE 3
234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200236PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000238 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 asdl_seq *seq;
240 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000241 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400242 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200245 return NULL;
246 if (filename == NULL) {
247 PySymtable_Free(st);
248 return NULL;
249 }
250 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 st->st_filename = filename;
252 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000253
254 /* Setup recursion depth check counters */
255 tstate = PyThreadState_GET();
256 if (!tstate) {
257 PySymtable_Free(st);
258 return NULL;
259 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400260 /* Be careful here to prevent overflow. */
261 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
262 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
263 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
264 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 /* Make the initial symbol information gathering pass */
267 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000268 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 PySymtable_Free(st);
270 return NULL;
271 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 st->st_top = st->st_cur;
274 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
275 switch (mod->kind) {
276 case Module_kind:
277 seq = mod->v.Module.body;
278 for (i = 0; i < asdl_seq_LEN(seq); i++)
279 if (!symtable_visit_stmt(st,
280 (stmt_ty)asdl_seq_GET(seq, i)))
281 goto error;
282 break;
283 case Expression_kind:
284 if (!symtable_visit_expr(st, mod->v.Expression.body))
285 goto error;
286 break;
287 case Interactive_kind:
288 seq = mod->v.Interactive.body;
289 for (i = 0; i < asdl_seq_LEN(seq); i++)
290 if (!symtable_visit_stmt(st,
291 (stmt_ty)asdl_seq_GET(seq, i)))
292 goto error;
293 break;
294 case Suite_kind:
295 PyErr_SetString(PyExc_RuntimeError,
296 "this compiler does not handle Suites");
297 goto error;
298 }
299 if (!symtable_exit_block(st, (void *)mod)) {
300 PySymtable_Free(st);
301 return NULL;
302 }
303 /* Make the second symbol analysis pass */
304 if (symtable_analyze(st))
305 return st;
306 PySymtable_Free(st);
307 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 (void) symtable_exit_block(st, (void *)mod);
310 PySymtable_Free(st);
311 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312}
313
Victor Stinner14e461d2013-08-26 22:28:21 +0200314struct symtable *
315PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
316{
317 PyObject *filename;
318 struct symtable *st;
319 filename = PyUnicode_DecodeFSDefault(filename_str);
320 if (filename == NULL)
321 return NULL;
322 st = PySymtable_BuildObject(mod, filename, future);
323 Py_DECREF(filename);
324 return st;
325}
326
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327void
328PySymtable_Free(struct symtable *st)
329{
Victor Stinner14e461d2013-08-26 22:28:21 +0200330 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 Py_XDECREF(st->st_blocks);
332 Py_XDECREF(st->st_stack);
333 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334}
335
336PySTEntryObject *
337PySymtable_Lookup(struct symtable *st, void *key)
338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 k = PyLong_FromVoidPtr(key);
342 if (k == NULL)
343 return NULL;
344 v = PyDict_GetItem(st->st_blocks, k);
345 if (v) {
346 assert(PySTEntry_Check(v));
347 Py_INCREF(v);
348 }
349 else {
350 PyErr_SetString(PyExc_KeyError,
351 "unknown symbol table entry");
352 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 Py_DECREF(k);
355 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356}
357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359PyST_GetScope(PySTEntryObject *ste, PyObject *name)
360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
362 if (!v)
363 return 0;
364 assert(PyLong_Check(v));
365 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366}
367
Benjamin Petersond9c87022012-10-31 20:26:20 -0400368static int
369error_at_directive(PySTEntryObject *ste, PyObject *name)
370{
371 Py_ssize_t i;
372 PyObject *data;
373 assert(ste->ste_directives);
374 for (i = 0; ; i++) {
375 data = PyList_GET_ITEM(ste->ste_directives, i);
376 assert(PyTuple_CheckExact(data));
377 if (PyTuple_GET_ITEM(data, 0) == name)
378 break;
379 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200380 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
381 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
382 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
Benjamin Petersond9c87022012-10-31 20:26:20 -0400383 return 0;
384}
385
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386
387/* Analyze raw symbol information to determine scope of each name.
388
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000389 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000391 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395 explicit global is declared with the global statement. An implicit
396 global is a free variable for which the compiler has found no binding
397 in an enclosing function scope. The implicit global is either a global
398 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
399 to handle these names to implement slightly odd semantics. In such a
400 block, the name is treated as global until it is assigned to; then it
401 is treated as a local.
402
403 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000404 The first pass collects raw facts from the AST via the symtable_visit_*
405 functions: the name is a parameter here, the name is used but not defined
406 here, etc. The second pass analyzes these facts during a pass over the
407 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408
409 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000411 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000412 Names which are explicitly declared nonlocal must exist in this set of
413 visible names - if they do not, a syntax error is raised. After doing
414 the local analysis, it analyzes each of its child blocks using an
415 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000416
Nick Coghlan650f0d02007-04-15 12:05:43 +0000417 The children update the free variable set. If a local variable is added to
418 the free variable set by the child, the variable is marked as a cell. The
419 function object being defined must provide runtime storage for the variable
420 that may outlive the function's frame. Cell variables are removed from the
421 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000422
Nick Coghlan650f0d02007-04-15 12:05:43 +0000423 During analysis, the names are:
424 symbols: dict mapping from symbol names to flag values (including offset scope values)
425 scopes: dict mapping from symbol names to scope values (no offset)
426 local: set of all symbol names local to the current scope
427 bound: set of all symbol names local to a containing function scope
428 free: set of all symbol names referenced but not bound in child scopes
429 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430*/
431
432#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyObject *o = PyLong_FromLong(I); \
434 if (!o) \
435 return 0; \
436 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
437 Py_DECREF(o); \
438 return 0; \
439 } \
440 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441}
442
443/* Decide on scope of name, given flags.
444
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000445 The namespace dictionaries may be modified to record information
446 about the new name. For example, a new global will add an entry to
447 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000448*/
449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000451analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 PyObject *bound, PyObject *local, PyObject *free,
453 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 if (flags & DEF_GLOBAL) {
456 if (flags & DEF_PARAM) {
457 PyErr_Format(PyExc_SyntaxError,
458 "name '%U' is parameter and global",
459 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400460 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000461 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 if (flags & DEF_NONLOCAL) {
463 PyErr_Format(PyExc_SyntaxError,
464 "name '%U' is nonlocal and global",
465 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400466 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 }
468 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
469 if (PySet_Add(global, name) < 0)
470 return 0;
471 if (bound && (PySet_Discard(bound, name) < 0))
472 return 0;
473 return 1;
474 }
475 if (flags & DEF_NONLOCAL) {
476 if (flags & DEF_PARAM) {
477 PyErr_Format(PyExc_SyntaxError,
478 "name '%U' is parameter and nonlocal",
479 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400480 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 }
482 if (!bound) {
483 PyErr_Format(PyExc_SyntaxError,
484 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400485 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 }
487 if (!PySet_Contains(bound, name)) {
488 PyErr_Format(PyExc_SyntaxError,
489 "no binding for nonlocal '%U' found",
490 name);
491
Benjamin Petersond9c87022012-10-31 20:26:20 -0400492 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 }
494 SET_SCOPE(scopes, name, FREE);
495 ste->ste_free = 1;
496 return PySet_Add(free, name) >= 0;
497 }
498 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000499 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (PySet_Add(local, name) < 0)
501 return 0;
502 if (PySet_Discard(global, name) < 0)
503 return 0;
504 return 1;
505 }
506 /* If an enclosing block has a binding for this name, it
507 is a free variable rather than a global variable.
508 Note that having a non-NULL bound implies that the block
509 is nested.
510 */
511 if (bound && PySet_Contains(bound, name)) {
512 SET_SCOPE(scopes, name, FREE);
513 ste->ste_free = 1;
514 return PySet_Add(free, name) >= 0;
515 }
516 /* If a parent has a global statement, then call it global
517 explicit? It could also be global implicit.
518 */
519 if (global && PySet_Contains(global, name)) {
520 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
521 return 1;
522 }
523 if (ste->ste_nested)
524 ste->ste_free = 1;
525 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
526 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527}
528
529#undef SET_SCOPE
530
531/* If a name is defined in free and also in locals, then this block
532 provides the binding for the free variable. The name should be
533 marked CELL in this block and removed from the free list.
534
535 Note that the current block's free variables are included in free.
536 That's safe because no name can be free and local in the same scope.
537*/
538
539static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500540analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 PyObject *name, *v, *v_cell;
543 int success = 0;
544 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 v_cell = PyLong_FromLong(CELL);
547 if (!v_cell)
548 return 0;
549 while (PyDict_Next(scopes, &pos, &name, &v)) {
550 long scope;
551 assert(PyLong_Check(v));
552 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000553 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 continue;
555 if (!PySet_Contains(free, name))
556 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* Replace LOCAL with CELL for this name, and remove
558 from free. It is safe to replace the value of name
559 in the dict, because it will not cause a resize.
560 */
561 if (PyDict_SetItem(scopes, name, v_cell) < 0)
562 goto error;
563 if (PySet_Discard(free, name) < 0)
564 goto error;
565 }
566 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 Py_DECREF(v_cell);
569 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570}
571
Benjamin Peterson312595c2013-05-15 15:26:42 -0500572static int
573drop_class_free(PySTEntryObject *ste, PyObject *free)
574{
575 int res;
576 if (!GET_IDENTIFIER(__class__))
577 return 0;
578 res = PySet_Discard(free, __class__);
579 if (res < 0)
580 return 0;
581 if (res)
582 ste->ste_needs_class_closure = 1;
583 return 1;
584}
585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586/* Enter the final scope information into the ste_symbols dict.
587 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588 * All arguments are dicts. Modifies symbols, others are read-only.
589*/
590static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 PyObject *name = NULL, *itr = NULL;
595 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
596 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* Update scope information for all symbols in this scope */
599 while (PyDict_Next(symbols, &pos, &name, &v)) {
600 long scope, flags;
601 assert(PyLong_Check(v));
602 flags = PyLong_AS_LONG(v);
603 v_scope = PyDict_GetItem(scopes, name);
604 assert(v_scope && PyLong_Check(v_scope));
605 scope = PyLong_AS_LONG(v_scope);
606 flags |= (scope << SCOPE_OFFSET);
607 v_new = PyLong_FromLong(flags);
608 if (!v_new)
609 return 0;
610 if (PyDict_SetItem(symbols, name, v_new) < 0) {
611 Py_DECREF(v_new);
612 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 Py_DECREF(v_new);
615 }
616
617 /* Record not yet resolved free variables from children (if any) */
618 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
619 if (!v_free)
620 return 0;
621
622 itr = PyObject_GetIter(free);
623 if (!itr)
624 goto error;
625
626 while ((name = PyIter_Next(itr))) {
627 v = PyDict_GetItem(symbols, name);
628
629 /* Handle symbol that already exists in this scope */
630 if (v) {
631 /* Handle a free variable in a method of
632 the class that has the same name as a local
633 or global in the class scope.
634 */
635 if (classflag &&
636 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
637 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
638 v_new = PyLong_FromLong(flags);
639 if (!v_new) {
640 goto error;
641 }
642 if (PyDict_SetItem(symbols, name, v_new) < 0) {
643 Py_DECREF(v_new);
644 goto error;
645 }
646 Py_DECREF(v_new);
647 }
648 /* It's a cell, or already free in this scope */
649 Py_DECREF(name);
650 continue;
651 }
652 /* Handle global symbol */
653 if (!PySet_Contains(bound, name)) {
654 Py_DECREF(name);
655 continue; /* it's a global */
656 }
657 /* Propagate new free symbol up the lexical stack */
658 if (PyDict_SetItem(symbols, name, v_free) < 0) {
659 goto error;
660 }
661 Py_DECREF(name);
662 }
663 Py_DECREF(itr);
664 Py_DECREF(v_free);
665 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000666error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 Py_XDECREF(v_free);
668 Py_XDECREF(itr);
669 Py_XDECREF(name);
670 return 0;
671}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672
673/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000674
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675 Arguments:
676 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000677 bound -- set of variables bound in enclosing scopes (input). bound
678 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 free -- set of free variables in enclosed scopes (output)
680 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000681
682 The implementation uses two mutually recursive functions,
683 analyze_block() and analyze_child_block(). analyze_block() is
684 responsible for analyzing the individual names defined in a block.
685 analyze_child_block() prepares temporary namespace dictionaries
686 used to evaluated nested blocks.
687
688 The two functions exist because a child block should see the name
689 bindings of its enclosing blocks, but those bindings should not
690 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691*/
692
693static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
695 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000696
697static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
699 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
702 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
703 PyObject *temp;
704 int i, success = 0;
705 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 local = PySet_New(NULL); /* collect new names bound in block */
708 if (!local)
709 goto error;
710 scopes = PyDict_New(); /* collect scopes defined for each name */
711 if (!scopes)
712 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* Allocate new global and bound variable dictionaries. These
715 dictionaries hold the names visible in nested blocks. For
716 ClassBlocks, the bound and global names are initialized
717 before analyzing names, because class bindings aren't
718 visible in methods. For other blocks, they are initialized
719 after names are analyzed.
720 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 /* TODO(jhylton): Package these dicts in a struct so that we
723 can write reasonable helper functions?
724 */
725 newglobal = PySet_New(NULL);
726 if (!newglobal)
727 goto error;
728 newfree = PySet_New(NULL);
729 if (!newfree)
730 goto error;
731 newbound = PySet_New(NULL);
732 if (!newbound)
733 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 /* Class namespace has no effect on names visible in
736 nested functions, so populate the global and bound
737 sets to be passed to child blocks before analyzing
738 this one.
739 */
740 if (ste->ste_type == ClassBlock) {
741 /* Pass down known globals */
742 temp = PyNumber_InPlaceOr(newglobal, global);
743 if (!temp)
744 goto error;
745 Py_DECREF(temp);
746 /* Pass down previously bound symbols */
747 if (bound) {
748 temp = PyNumber_InPlaceOr(newbound, bound);
749 if (!temp)
750 goto error;
751 Py_DECREF(temp);
752 }
753 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
756 long flags = PyLong_AS_LONG(v);
757 if (!analyze_name(ste, scopes, name, flags,
758 bound, local, free, global))
759 goto error;
760 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 /* Populate global and bound sets to be passed to children. */
763 if (ste->ste_type != ClassBlock) {
764 /* Add function locals to bound set */
765 if (ste->ste_type == FunctionBlock) {
766 temp = PyNumber_InPlaceOr(newbound, local);
767 if (!temp)
768 goto error;
769 Py_DECREF(temp);
770 }
771 /* Pass down previously bound symbols */
772 if (bound) {
773 temp = PyNumber_InPlaceOr(newbound, bound);
774 if (!temp)
775 goto error;
776 Py_DECREF(temp);
777 }
778 /* Pass down known globals */
779 temp = PyNumber_InPlaceOr(newglobal, global);
780 if (!temp)
781 goto error;
782 Py_DECREF(temp);
783 }
784 else {
785 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000786 if (!GET_IDENTIFIER(__class__))
787 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 if (PySet_Add(newbound, __class__) < 0)
789 goto error;
790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300792 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 newbound, newglobal now contain the names visible in
795 nested blocks. The free variables in the children will
796 be collected in allfree.
797 */
798 allfree = PySet_New(NULL);
799 if (!allfree)
800 goto error;
801 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
802 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
803 PySTEntryObject* entry;
804 assert(c && PySTEntry_Check(c));
805 entry = (PySTEntryObject*)c;
806 if (!analyze_child_block(entry, newbound, newfree, newglobal,
807 allfree))
808 goto error;
809 /* Check if any children have free variables */
810 if (entry->ste_free || entry->ste_child_free)
811 ste->ste_child_free = 1;
812 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 temp = PyNumber_InPlaceOr(newfree, allfree);
815 if (!temp)
816 goto error;
817 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500820 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500822 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 goto error;
824 /* Records the results of the analysis in the symbol table entry */
825 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
826 ste->ste_type == ClassBlock))
827 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 temp = PyNumber_InPlaceOr(free, newfree);
830 if (!temp)
831 goto error;
832 Py_DECREF(temp);
833 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 Py_XDECREF(scopes);
836 Py_XDECREF(local);
837 Py_XDECREF(newbound);
838 Py_XDECREF(newglobal);
839 Py_XDECREF(newfree);
840 Py_XDECREF(allfree);
841 if (!success)
842 assert(PyErr_Occurred());
843 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844}
845
846static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
848 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
851 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 These dictionary are used by all blocks enclosed by the
856 current block. The analyze_block() call modifies these
857 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 */
860 temp_bound = PySet_New(bound);
861 if (!temp_bound)
862 goto error;
863 temp_free = PySet_New(free);
864 if (!temp_free)
865 goto error;
866 temp_global = PySet_New(global);
867 if (!temp_global)
868 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
871 goto error;
872 temp = PyNumber_InPlaceOr(child_free, temp_free);
873 if (!temp)
874 goto error;
875 Py_DECREF(temp);
876 Py_DECREF(temp_bound);
877 Py_DECREF(temp_free);
878 Py_DECREF(temp_global);
879 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000880 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 Py_XDECREF(temp_bound);
882 Py_XDECREF(temp_free);
883 Py_XDECREF(temp_global);
884 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000885}
886
887static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888symtable_analyze(struct symtable *st)
889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 PyObject *free, *global;
891 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 free = PySet_New(NULL);
894 if (!free)
895 return 0;
896 global = PySet_New(NULL);
897 if (!global) {
898 Py_DECREF(free);
899 return 0;
900 }
901 r = analyze_block(st->st_top, NULL, free, global);
902 Py_DECREF(free);
903 Py_DECREF(global);
904 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905}
906
907
908static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000909symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910{
Victor Stinner14e461d2013-08-26 22:28:21 +0200911 PyObject *message = PyUnicode_FromString(msg);
912 if (message == NULL)
913 return 0;
914 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, message, st->st_filename,
915 lineno, NULL, NULL) < 0) {
916 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
918 PyErr_SetString(PyExc_SyntaxError, msg);
Victor Stinner14e461d2013-08-26 22:28:21 +0200919 PyErr_SyntaxLocationObject(st->st_filename, st->st_cur->ste_lineno,
920 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 }
922 return 0;
923 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200924 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926}
927
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000928/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929 This reference is released when the block is exited, via the DECREF
930 in symtable_exit_block().
931*/
932
933static int
934symtable_exit_block(struct symtable *st, void *ast)
935{
Benjamin Peterson609da582011-06-29 22:52:39 -0500936 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937
Benjamin Peterson609da582011-06-29 22:52:39 -0500938 st->st_cur = NULL;
939 size = PyList_GET_SIZE(st->st_stack);
940 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500941 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500943 if (--size)
944 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 }
946 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947}
948
949static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000951 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952{
Benjamin Peterson609da582011-06-29 22:52:39 -0500953 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954
Benjamin Peterson609da582011-06-29 22:52:39 -0500955 ste = ste_new(st, name, block, ast, lineno, col_offset);
956 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500958 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
959 Py_DECREF(ste);
960 return 0;
961 }
962 prev = st->st_cur;
963 /* The entry is owned by the stack. Borrow it for st_cur. */
964 Py_DECREF(ste);
965 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000966 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 st->st_global = st->st_cur->ste_symbols;
968 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500969 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 return 0;
971 }
972 }
973 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974}
975
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000976static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977symtable_lookup(struct symtable *st, PyObject *name)
978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyObject *o;
980 PyObject *mangled = _Py_Mangle(st->st_private, name);
981 if (!mangled)
982 return 0;
983 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
984 Py_DECREF(mangled);
985 if (!o)
986 return 0;
987 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988}
989
990static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 PyObject *o;
994 PyObject *dict;
995 long val;
996 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997
Jeremy Hylton81e95022007-02-27 06:50:52 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 if (!mangled)
1000 return 0;
1001 dict = st->st_cur->ste_symbols;
1002 if ((o = PyDict_GetItem(dict, mangled))) {
1003 val = PyLong_AS_LONG(o);
1004 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1005 /* Is it better to use 'mangled' or 'name' here? */
1006 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001007 PyErr_SyntaxLocationObject(st->st_filename,
1008 st->st_cur->ste_lineno,
1009 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 goto error;
1011 }
1012 val |= flag;
1013 } else
1014 val = flag;
1015 o = PyLong_FromLong(val);
1016 if (o == NULL)
1017 goto error;
1018 if (PyDict_SetItem(dict, mangled, o) < 0) {
1019 Py_DECREF(o);
1020 goto error;
1021 }
1022 Py_DECREF(o);
1023
1024 if (flag & DEF_PARAM) {
1025 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1026 goto error;
1027 } else if (flag & DEF_GLOBAL) {
1028 /* XXX need to update DEF_GLOBAL for other flags too;
1029 perhaps only DEF_FREE_GLOBAL */
1030 val = flag;
1031 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1032 val |= PyLong_AS_LONG(o);
1033 }
1034 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 goto error;
1037 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1038 Py_DECREF(o);
1039 goto error;
1040 }
1041 Py_DECREF(o);
1042 }
1043 Py_DECREF(mangled);
1044 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001045
1046error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001047 Py_DECREF(mangled);
1048 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049}
1050
1051/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1052 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 function.
1054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1056 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001057
1058 VISIT_QUIT macro returns the specified value exiting from the function but
1059 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060*/
1061
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001062#define VISIT_QUIT(ST, X) \
1063 return --(ST)->recursion_depth,(X)
1064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001067 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001068
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 int i; \
1071 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1072 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1073 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1074 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001075 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001078
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 int i; \
1081 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1082 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1083 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1084 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001085 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001088
Guido van Rossum4f72a782006-10-27 23:31:49 +00001089#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 int i = 0; \
1091 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1092 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1093 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1094 if (!elt) continue; /* can be NULL */ \
1095 if (!symtable_visit_expr((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001096 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001098}
1099
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001101symtable_new_tmpname(struct symtable *st)
1102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 char tmpname[256];
1104 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1107 ++st->st_cur->ste_tmpname);
1108 tmp = PyUnicode_InternFromString(tmpname);
1109 if (!tmp)
1110 return 0;
1111 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1112 return 0;
1113 Py_DECREF(tmp);
1114 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001115}
1116
Guido van Rossum4f72a782006-10-27 23:31:49 +00001117
Guido van Rossumc2e20742006-02-27 22:32:47 +00001118static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001119symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1120{
1121 PyObject *data;
1122 int res;
1123 if (!st->st_cur->ste_directives) {
1124 st->st_cur->ste_directives = PyList_New(0);
1125 if (!st->st_cur->ste_directives)
1126 return 0;
1127 }
1128 data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset);
1129 if (!data)
1130 return 0;
1131 res = PyList_Append(st->st_cur->ste_directives, data);
1132 Py_DECREF(data);
1133 return res == 0;
1134}
1135
1136
1137static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138symtable_visit_stmt(struct symtable *st, stmt_ty s)
1139{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001140 if (++st->recursion_depth > st->recursion_limit) {
1141 PyErr_SetString(PyExc_RuntimeError,
1142 "maximum recursion depth exceeded during compilation");
1143 VISIT_QUIT(st, 0);
1144 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 switch (s->kind) {
1146 case FunctionDef_kind:
1147 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001148 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 if (s->v.FunctionDef.args->defaults)
1150 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1151 if (s->v.FunctionDef.args->kw_defaults)
1152 VISIT_KWONLYDEFAULTS(st,
1153 s->v.FunctionDef.args->kw_defaults);
1154 if (!symtable_visit_annotations(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001155 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 if (s->v.FunctionDef.decorator_list)
1157 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1158 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001159 FunctionBlock, (void *)s, s->lineno,
1160 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001161 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001162 VISIT(st, arguments, s->v.FunctionDef.args);
1163 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001165 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 break;
1167 case ClassDef_kind: {
1168 PyObject *tmp;
1169 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001170 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1172 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1173 if (s->v.ClassDef.starargs)
1174 VISIT(st, expr, s->v.ClassDef.starargs);
1175 if (s->v.ClassDef.kwargs)
1176 VISIT(st, expr, s->v.ClassDef.kwargs);
1177 if (s->v.ClassDef.decorator_list)
1178 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1179 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001180 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001181 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 tmp = st->st_private;
1183 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001184 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 st->st_private = tmp;
1186 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001187 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 break;
1189 }
1190 case Return_kind:
1191 if (s->v.Return.value) {
1192 VISIT(st, expr, s->v.Return.value);
1193 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 }
1195 break;
1196 case Delete_kind:
1197 VISIT_SEQ(st, expr, s->v.Delete.targets);
1198 break;
1199 case Assign_kind:
1200 VISIT_SEQ(st, expr, s->v.Assign.targets);
1201 VISIT(st, expr, s->v.Assign.value);
1202 break;
1203 case AugAssign_kind:
1204 VISIT(st, expr, s->v.AugAssign.target);
1205 VISIT(st, expr, s->v.AugAssign.value);
1206 break;
1207 case For_kind:
1208 VISIT(st, expr, s->v.For.target);
1209 VISIT(st, expr, s->v.For.iter);
1210 VISIT_SEQ(st, stmt, s->v.For.body);
1211 if (s->v.For.orelse)
1212 VISIT_SEQ(st, stmt, s->v.For.orelse);
1213 break;
1214 case While_kind:
1215 VISIT(st, expr, s->v.While.test);
1216 VISIT_SEQ(st, stmt, s->v.While.body);
1217 if (s->v.While.orelse)
1218 VISIT_SEQ(st, stmt, s->v.While.orelse);
1219 break;
1220 case If_kind:
1221 /* XXX if 0: and lookup_yield() hacks */
1222 VISIT(st, expr, s->v.If.test);
1223 VISIT_SEQ(st, stmt, s->v.If.body);
1224 if (s->v.If.orelse)
1225 VISIT_SEQ(st, stmt, s->v.If.orelse);
1226 break;
1227 case Raise_kind:
1228 if (s->v.Raise.exc) {
1229 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001230 if (s->v.Raise.cause) {
1231 VISIT(st, expr, s->v.Raise.cause);
1232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 }
1234 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001235 case Try_kind:
1236 VISIT_SEQ(st, stmt, s->v.Try.body);
1237 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1238 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1239 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 break;
1241 case Assert_kind:
1242 VISIT(st, expr, s->v.Assert.test);
1243 if (s->v.Assert.msg)
1244 VISIT(st, expr, s->v.Assert.msg);
1245 break;
1246 case Import_kind:
1247 VISIT_SEQ(st, alias, s->v.Import.names);
1248 /* XXX Don't have the lineno available inside
1249 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001250 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001252 st->st_cur->ste_opt_col_offset = s->col_offset;
1253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 break;
1255 case ImportFrom_kind:
1256 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1257 /* XXX Don't have the lineno available inside
1258 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001259 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001261 st->st_cur->ste_opt_col_offset = s->col_offset;
1262 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 break;
1264 case Global_kind: {
1265 int i;
1266 asdl_seq *seq = s->v.Global.names;
1267 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1268 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 long cur = symtable_lookup(st, name);
1270 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001271 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 if (cur & (DEF_LOCAL | USE)) {
1273 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001274 char *c_name = _PyUnicode_AsString(name);
1275 if (!c_name)
1276 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (cur & DEF_LOCAL)
1278 PyOS_snprintf(buf, sizeof(buf),
1279 GLOBAL_AFTER_ASSIGN,
1280 c_name);
1281 else
1282 PyOS_snprintf(buf, sizeof(buf),
1283 GLOBAL_AFTER_USE,
1284 c_name);
1285 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001286 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 }
1288 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001289 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001290 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001291 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 }
1293 break;
1294 }
1295 case Nonlocal_kind: {
1296 int i;
1297 asdl_seq *seq = s->v.Nonlocal.names;
1298 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1299 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 long cur = symtable_lookup(st, name);
1301 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001302 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 if (cur & (DEF_LOCAL | USE)) {
1304 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001305 char *c_name = _PyUnicode_AsString(name);
1306 if (!c_name)
1307 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 if (cur & DEF_LOCAL)
1309 PyOS_snprintf(buf, sizeof(buf),
1310 NONLOCAL_AFTER_ASSIGN,
1311 c_name);
1312 else
1313 PyOS_snprintf(buf, sizeof(buf),
1314 NONLOCAL_AFTER_USE,
1315 c_name);
1316 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001317 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 }
1319 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001320 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001321 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001322 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 }
1324 break;
1325 }
1326 case Expr_kind:
1327 VISIT(st, expr, s->v.Expr.value);
1328 break;
1329 case Pass_kind:
1330 case Break_kind:
1331 case Continue_kind:
1332 /* nothing to do here */
1333 break;
1334 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001335 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 VISIT_SEQ(st, stmt, s->v.With.body);
1337 break;
1338 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001339 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340}
1341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343symtable_visit_expr(struct symtable *st, expr_ty e)
1344{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001345 if (++st->recursion_depth > st->recursion_limit) {
1346 PyErr_SetString(PyExc_RuntimeError,
1347 "maximum recursion depth exceeded during compilation");
1348 VISIT_QUIT(st, 0);
1349 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 switch (e->kind) {
1351 case BoolOp_kind:
1352 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1353 break;
1354 case BinOp_kind:
1355 VISIT(st, expr, e->v.BinOp.left);
1356 VISIT(st, expr, e->v.BinOp.right);
1357 break;
1358 case UnaryOp_kind:
1359 VISIT(st, expr, e->v.UnaryOp.operand);
1360 break;
1361 case Lambda_kind: {
1362 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001363 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 if (e->v.Lambda.args->defaults)
1365 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001366 if (e->v.Lambda.args->kw_defaults)
1367 VISIT_KWONLYDEFAULTS(st,
1368 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001370 FunctionBlock, (void *)e, e->lineno,
1371 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001372 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001373 VISIT(st, arguments, e->v.Lambda.args);
1374 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001376 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 break;
1378 }
1379 case IfExp_kind:
1380 VISIT(st, expr, e->v.IfExp.test);
1381 VISIT(st, expr, e->v.IfExp.body);
1382 VISIT(st, expr, e->v.IfExp.orelse);
1383 break;
1384 case Dict_kind:
1385 VISIT_SEQ(st, expr, e->v.Dict.keys);
1386 VISIT_SEQ(st, expr, e->v.Dict.values);
1387 break;
1388 case Set_kind:
1389 VISIT_SEQ(st, expr, e->v.Set.elts);
1390 break;
1391 case GeneratorExp_kind:
1392 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001393 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 break;
1395 case ListComp_kind:
1396 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001397 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 break;
1399 case SetComp_kind:
1400 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001401 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 break;
1403 case DictComp_kind:
1404 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001405 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 break;
1407 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001408 if (e->v.Yield.value)
1409 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001412 case YieldFrom_kind:
1413 VISIT(st, expr, e->v.YieldFrom.value);
1414 st->st_cur->ste_generator = 1;
1415 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 case Compare_kind:
1417 VISIT(st, expr, e->v.Compare.left);
1418 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1419 break;
1420 case Call_kind:
1421 VISIT(st, expr, e->v.Call.func);
1422 VISIT_SEQ(st, expr, e->v.Call.args);
1423 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1424 if (e->v.Call.starargs)
1425 VISIT(st, expr, e->v.Call.starargs);
1426 if (e->v.Call.kwargs)
1427 VISIT(st, expr, e->v.Call.kwargs);
1428 break;
1429 case Num_kind:
1430 case Str_kind:
1431 case Bytes_kind:
1432 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001433 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 /* Nothing to do here. */
1435 break;
1436 /* The following exprs can be assignment targets. */
1437 case Attribute_kind:
1438 VISIT(st, expr, e->v.Attribute.value);
1439 break;
1440 case Subscript_kind:
1441 VISIT(st, expr, e->v.Subscript.value);
1442 VISIT(st, slice, e->v.Subscript.slice);
1443 break;
1444 case Starred_kind:
1445 VISIT(st, expr, e->v.Starred.value);
1446 break;
1447 case Name_kind:
1448 if (!symtable_add_def(st, e->v.Name.id,
1449 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001450 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 /* Special-case super: it counts as a use of __class__ */
1452 if (e->v.Name.ctx == Load &&
1453 st->st_cur->ste_type == FunctionBlock &&
1454 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001455 if (!GET_IDENTIFIER(__class__) ||
1456 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001457 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 }
1459 break;
1460 /* child nodes of List and Tuple will have expr_context set */
1461 case List_kind:
1462 VISIT_SEQ(st, expr, e->v.List.elts);
1463 break;
1464 case Tuple_kind:
1465 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1466 break;
1467 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001468 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469}
1470
1471static int
1472symtable_implicit_arg(struct symtable *st, int pos)
1473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1475 if (id == NULL)
1476 return 0;
1477 if (!symtable_add_def(st, id, DEF_PARAM)) {
1478 Py_DECREF(id);
1479 return 0;
1480 }
1481 Py_DECREF(id);
1482 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483}
1484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001486symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 if (!args)
1491 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 for (i = 0; i < asdl_seq_LEN(args); i++) {
1494 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1495 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1496 return 0;
1497 }
1498
1499 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500}
1501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001503symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 if (!args)
1508 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 for (i = 0; i < asdl_seq_LEN(args); i++) {
1511 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1512 if (arg->annotation)
1513 VISIT(st, expr, arg->annotation);
1514 }
1515
1516 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001517}
1518
Neal Norwitzc1505362006-12-28 06:47:50 +00001519static int
1520symtable_visit_annotations(struct symtable *st, stmt_ty s)
1521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 arguments_ty a = s->v.FunctionDef.args;
1523
1524 if (a->args && !symtable_visit_argannotations(st, a->args))
1525 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001526 if (a->vararg && a->vararg->annotation)
1527 VISIT(st, expr, a->vararg->annotation);
1528 if (a->kwarg && a->kwarg->annotation)
1529 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1531 return 0;
1532 if (s->v.FunctionDef.returns)
1533 VISIT(st, expr, s->v.FunctionDef.returns);
1534 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535}
1536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538symtable_visit_arguments(struct symtable *st, arguments_ty a)
1539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 /* skip default arguments inside function block
1541 XXX should ast be different?
1542 */
1543 if (a->args && !symtable_visit_params(st, a->args))
1544 return 0;
1545 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1546 return 0;
1547 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001548 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 return 0;
1550 st->st_cur->ste_varargs = 1;
1551 }
1552 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001553 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 return 0;
1555 st->st_cur->ste_varkeywords = 1;
1556 }
1557 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558}
1559
1560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (eh->v.ExceptHandler.type)
1565 VISIT(st, expr, eh->v.ExceptHandler.type);
1566 if (eh->v.ExceptHandler.name)
1567 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1568 return 0;
1569 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1570 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571}
1572
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001573static int
1574symtable_visit_withitem(struct symtable *st, withitem_ty item)
1575{
1576 VISIT(st, expr, item->context_expr);
1577 if (item->optional_vars) {
1578 VISIT(st, expr, item->optional_vars);
1579 }
1580 return 1;
1581}
1582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585symtable_visit_alias(struct symtable *st, alias_ty a)
1586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001588 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 dotted package name (e.g. spam.eggs)
1590 */
1591 PyObject *store_name;
1592 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001593 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1594 PyUnicode_GET_LENGTH(name), 1);
1595 if (dot != -1) {
1596 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 if (!store_name)
1598 return 0;
1599 }
1600 else {
1601 store_name = name;
1602 Py_INCREF(store_name);
1603 }
1604 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1605 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1606 Py_DECREF(store_name);
1607 return r;
1608 }
1609 else {
1610 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001611 int lineno = st->st_cur->ste_lineno;
1612 int col_offset = st->st_cur->ste_col_offset;
1613 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001614 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001615 Py_DECREF(store_name);
1616 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 }
1618 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1619 Py_DECREF(store_name);
1620 return 1;
1621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622}
1623
1624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 VISIT(st, expr, lc->target);
1629 VISIT(st, expr, lc->iter);
1630 VISIT_SEQ(st, expr, lc->ifs);
1631 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632}
1633
1634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636symtable_visit_keyword(struct symtable *st, keyword_ty k)
1637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 VISIT(st, expr, k->value);
1639 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640}
1641
1642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644symtable_visit_slice(struct symtable *st, slice_ty s)
1645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 switch (s->kind) {
1647 case Slice_kind:
1648 if (s->v.Slice.lower)
1649 VISIT(st, expr, s->v.Slice.lower)
1650 if (s->v.Slice.upper)
1651 VISIT(st, expr, s->v.Slice.upper)
1652 if (s->v.Slice.step)
1653 VISIT(st, expr, s->v.Slice.step)
1654 break;
1655 case ExtSlice_kind:
1656 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1657 break;
1658 case Index_kind:
1659 VISIT(st, expr, s->v.Index.value)
1660 break;
1661 }
1662 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663}
1664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001666symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001667 identifier scope_name, asdl_seq *generators,
1668 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 int is_generator = (e->kind == GeneratorExp_kind);
1671 int needs_tmp = !is_generator;
1672 comprehension_ty outermost = ((comprehension_ty)
1673 asdl_seq_GET(generators, 0));
1674 /* Outermost iterator is evaluated in current scope */
1675 VISIT(st, expr, outermost->iter);
1676 /* Create comprehension scope for the rest */
1677 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001678 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1679 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 return 0;
1681 }
1682 st->st_cur->ste_generator = is_generator;
1683 /* Outermost iter is received as an argument */
1684 if (!symtable_implicit_arg(st, 0)) {
1685 symtable_exit_block(st, (void *)e);
1686 return 0;
1687 }
1688 /* Allocate temporary name if needed */
1689 if (needs_tmp && !symtable_new_tmpname(st)) {
1690 symtable_exit_block(st, (void *)e);
1691 return 0;
1692 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001693 VISIT(st, expr, outermost->target);
1694 VISIT_SEQ(st, expr, outermost->ifs);
1695 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001697 VISIT(st, expr, value);
1698 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001703symtable_visit_genexp(struct symtable *st, expr_ty e)
1704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1706 e->v.GeneratorExp.generators,
1707 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001708}
1709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001711symtable_visit_listcomp(struct symtable *st, expr_ty e)
1712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1714 e->v.ListComp.generators,
1715 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001716}
1717
1718static int
1719symtable_visit_setcomp(struct symtable *st, expr_ty e)
1720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1722 e->v.SetComp.generators,
1723 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001724}
1725
1726static int
1727symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1730 e->v.DictComp.generators,
1731 e->v.DictComp.key,
1732 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001733}