blob: 618a81442bcac91c0842b9b135707ad10e8e77c9 [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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200244 return NULL;
245 if (filename == NULL) {
246 PySymtable_Free(st);
247 return NULL;
248 }
249 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 st->st_filename = filename;
251 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000252
253 /* Setup recursion depth check counters */
254 tstate = PyThreadState_GET();
255 if (!tstate) {
256 PySymtable_Free(st);
257 return NULL;
258 }
259 st->recursion_depth = tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE;
260 st->recursion_limit = Py_GetRecursionLimit() * COMPILER_STACK_FRAME_SCALE;
261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 /* Make the initial symbol information gathering pass */
263 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000264 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 PySymtable_Free(st);
266 return NULL;
267 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 st->st_top = st->st_cur;
270 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
271 switch (mod->kind) {
272 case Module_kind:
273 seq = mod->v.Module.body;
274 for (i = 0; i < asdl_seq_LEN(seq); i++)
275 if (!symtable_visit_stmt(st,
276 (stmt_ty)asdl_seq_GET(seq, i)))
277 goto error;
278 break;
279 case Expression_kind:
280 if (!symtable_visit_expr(st, mod->v.Expression.body))
281 goto error;
282 break;
283 case Interactive_kind:
284 seq = mod->v.Interactive.body;
285 for (i = 0; i < asdl_seq_LEN(seq); i++)
286 if (!symtable_visit_stmt(st,
287 (stmt_ty)asdl_seq_GET(seq, i)))
288 goto error;
289 break;
290 case Suite_kind:
291 PyErr_SetString(PyExc_RuntimeError,
292 "this compiler does not handle Suites");
293 goto error;
294 }
295 if (!symtable_exit_block(st, (void *)mod)) {
296 PySymtable_Free(st);
297 return NULL;
298 }
299 /* Make the second symbol analysis pass */
300 if (symtable_analyze(st))
301 return st;
302 PySymtable_Free(st);
303 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 (void) symtable_exit_block(st, (void *)mod);
306 PySymtable_Free(st);
307 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308}
309
Victor Stinner14e461d2013-08-26 22:28:21 +0200310struct symtable *
311PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
312{
313 PyObject *filename;
314 struct symtable *st;
315 filename = PyUnicode_DecodeFSDefault(filename_str);
316 if (filename == NULL)
317 return NULL;
318 st = PySymtable_BuildObject(mod, filename, future);
319 Py_DECREF(filename);
320 return st;
321}
322
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323void
324PySymtable_Free(struct symtable *st)
325{
Victor Stinner14e461d2013-08-26 22:28:21 +0200326 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 Py_XDECREF(st->st_blocks);
328 Py_XDECREF(st->st_stack);
329 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330}
331
332PySTEntryObject *
333PySymtable_Lookup(struct symtable *st, void *key)
334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 k = PyLong_FromVoidPtr(key);
338 if (k == NULL)
339 return NULL;
340 v = PyDict_GetItem(st->st_blocks, k);
341 if (v) {
342 assert(PySTEntry_Check(v));
343 Py_INCREF(v);
344 }
345 else {
346 PyErr_SetString(PyExc_KeyError,
347 "unknown symbol table entry");
348 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 Py_DECREF(k);
351 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352}
353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355PyST_GetScope(PySTEntryObject *ste, PyObject *name)
356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
358 if (!v)
359 return 0;
360 assert(PyLong_Check(v));
361 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362}
363
Benjamin Petersond9c87022012-10-31 20:26:20 -0400364static int
365error_at_directive(PySTEntryObject *ste, PyObject *name)
366{
367 Py_ssize_t i;
368 PyObject *data;
369 assert(ste->ste_directives);
370 for (i = 0; ; i++) {
371 data = PyList_GET_ITEM(ste->ste_directives, i);
372 assert(PyTuple_CheckExact(data));
373 if (PyTuple_GET_ITEM(data, 0) == name)
374 break;
375 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200376 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
377 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
378 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
Benjamin Petersond9c87022012-10-31 20:26:20 -0400379 return 0;
380}
381
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382
383/* Analyze raw symbol information to determine scope of each name.
384
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000385 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000391 explicit global is declared with the global statement. An implicit
392 global is a free variable for which the compiler has found no binding
393 in an enclosing function scope. The implicit global is either a global
394 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
395 to handle these names to implement slightly odd semantics. In such a
396 block, the name is treated as global until it is assigned to; then it
397 is treated as a local.
398
399 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000400 The first pass collects raw facts from the AST via the symtable_visit_*
401 functions: the name is a parameter here, the name is used but not defined
402 here, etc. The second pass analyzes these facts during a pass over the
403 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404
405 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000407 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000408 Names which are explicitly declared nonlocal must exist in this set of
409 visible names - if they do not, a syntax error is raised. After doing
410 the local analysis, it analyzes each of its child blocks using an
411 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412
Nick Coghlan650f0d02007-04-15 12:05:43 +0000413 The children update the free variable set. If a local variable is added to
414 the free variable set by the child, the variable is marked as a cell. The
415 function object being defined must provide runtime storage for the variable
416 that may outlive the function's frame. Cell variables are removed from the
417 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000418
Nick Coghlan650f0d02007-04-15 12:05:43 +0000419 During analysis, the names are:
420 symbols: dict mapping from symbol names to flag values (including offset scope values)
421 scopes: dict mapping from symbol names to scope values (no offset)
422 local: set of all symbol names local to the current scope
423 bound: set of all symbol names local to a containing function scope
424 free: set of all symbol names referenced but not bound in child scopes
425 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426*/
427
428#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 PyObject *o = PyLong_FromLong(I); \
430 if (!o) \
431 return 0; \
432 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
433 Py_DECREF(o); \
434 return 0; \
435 } \
436 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437}
438
439/* Decide on scope of name, given flags.
440
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000441 The namespace dictionaries may be modified to record information
442 about the new name. For example, a new global will add an entry to
443 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444*/
445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000447analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyObject *bound, PyObject *local, PyObject *free,
449 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (flags & DEF_GLOBAL) {
452 if (flags & DEF_PARAM) {
453 PyErr_Format(PyExc_SyntaxError,
454 "name '%U' is parameter and global",
455 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400456 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000457 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (flags & DEF_NONLOCAL) {
459 PyErr_Format(PyExc_SyntaxError,
460 "name '%U' is nonlocal and global",
461 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400462 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 }
464 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
465 if (PySet_Add(global, name) < 0)
466 return 0;
467 if (bound && (PySet_Discard(bound, name) < 0))
468 return 0;
469 return 1;
470 }
471 if (flags & DEF_NONLOCAL) {
472 if (flags & DEF_PARAM) {
473 PyErr_Format(PyExc_SyntaxError,
474 "name '%U' is parameter and nonlocal",
475 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400476 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 }
478 if (!bound) {
479 PyErr_Format(PyExc_SyntaxError,
480 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400481 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 }
483 if (!PySet_Contains(bound, name)) {
484 PyErr_Format(PyExc_SyntaxError,
485 "no binding for nonlocal '%U' found",
486 name);
487
Benjamin Petersond9c87022012-10-31 20:26:20 -0400488 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 }
490 SET_SCOPE(scopes, name, FREE);
491 ste->ste_free = 1;
492 return PySet_Add(free, name) >= 0;
493 }
494 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000495 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if (PySet_Add(local, name) < 0)
497 return 0;
498 if (PySet_Discard(global, name) < 0)
499 return 0;
500 return 1;
501 }
502 /* If an enclosing block has a binding for this name, it
503 is a free variable rather than a global variable.
504 Note that having a non-NULL bound implies that the block
505 is nested.
506 */
507 if (bound && PySet_Contains(bound, name)) {
508 SET_SCOPE(scopes, name, FREE);
509 ste->ste_free = 1;
510 return PySet_Add(free, name) >= 0;
511 }
512 /* If a parent has a global statement, then call it global
513 explicit? It could also be global implicit.
514 */
515 if (global && PySet_Contains(global, name)) {
516 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
517 return 1;
518 }
519 if (ste->ste_nested)
520 ste->ste_free = 1;
521 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
522 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523}
524
525#undef SET_SCOPE
526
527/* If a name is defined in free and also in locals, then this block
528 provides the binding for the free variable. The name should be
529 marked CELL in this block and removed from the free list.
530
531 Note that the current block's free variables are included in free.
532 That's safe because no name can be free and local in the same scope.
533*/
534
535static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500536analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 PyObject *name, *v, *v_cell;
539 int success = 0;
540 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 v_cell = PyLong_FromLong(CELL);
543 if (!v_cell)
544 return 0;
545 while (PyDict_Next(scopes, &pos, &name, &v)) {
546 long scope;
547 assert(PyLong_Check(v));
548 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000549 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 continue;
551 if (!PySet_Contains(free, name))
552 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 /* Replace LOCAL with CELL for this name, and remove
554 from free. It is safe to replace the value of name
555 in the dict, because it will not cause a resize.
556 */
557 if (PyDict_SetItem(scopes, name, v_cell) < 0)
558 goto error;
559 if (PySet_Discard(free, name) < 0)
560 goto error;
561 }
562 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 Py_DECREF(v_cell);
565 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566}
567
Benjamin Peterson312595c2013-05-15 15:26:42 -0500568static int
569drop_class_free(PySTEntryObject *ste, PyObject *free)
570{
571 int res;
572 if (!GET_IDENTIFIER(__class__))
573 return 0;
574 res = PySet_Discard(free, __class__);
575 if (res < 0)
576 return 0;
577 if (res)
578 ste->ste_needs_class_closure = 1;
579 return 1;
580}
581
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582/* Check for illegal statements in unoptimized namespaces */
583static int
584check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
588 || !(ste->ste_free || ste->ste_child_free))
589 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 trailer = (ste->ste_child_free ?
592 "contains a nested function with free variables" :
593 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 switch (ste->ste_unoptimized) {
596 case OPT_TOPLEVEL: /* import * at top-level is fine */
597 return 1;
598 case OPT_IMPORT_STAR:
599 PyErr_Format(PyExc_SyntaxError,
600 "import * is not allowed in function '%U' because it %s",
601 ste->ste_name, trailer);
602 break;
603 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604
Victor Stinner14e461d2013-08-26 22:28:21 +0200605 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
606 ste->ste_opt_lineno,
607 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609}
610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611/* Enter the final scope information into the ste_symbols dict.
612 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 * All arguments are dicts. Modifies symbols, others are read-only.
614*/
615static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000617 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PyObject *name = NULL, *itr = NULL;
620 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
621 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 /* Update scope information for all symbols in this scope */
624 while (PyDict_Next(symbols, &pos, &name, &v)) {
625 long scope, flags;
626 assert(PyLong_Check(v));
627 flags = PyLong_AS_LONG(v);
628 v_scope = PyDict_GetItem(scopes, name);
629 assert(v_scope && PyLong_Check(v_scope));
630 scope = PyLong_AS_LONG(v_scope);
631 flags |= (scope << SCOPE_OFFSET);
632 v_new = PyLong_FromLong(flags);
633 if (!v_new)
634 return 0;
635 if (PyDict_SetItem(symbols, name, v_new) < 0) {
636 Py_DECREF(v_new);
637 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 Py_DECREF(v_new);
640 }
641
642 /* Record not yet resolved free variables from children (if any) */
643 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
644 if (!v_free)
645 return 0;
646
647 itr = PyObject_GetIter(free);
648 if (!itr)
649 goto error;
650
651 while ((name = PyIter_Next(itr))) {
652 v = PyDict_GetItem(symbols, name);
653
654 /* Handle symbol that already exists in this scope */
655 if (v) {
656 /* Handle a free variable in a method of
657 the class that has the same name as a local
658 or global in the class scope.
659 */
660 if (classflag &&
661 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
662 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
663 v_new = PyLong_FromLong(flags);
664 if (!v_new) {
665 goto error;
666 }
667 if (PyDict_SetItem(symbols, name, v_new) < 0) {
668 Py_DECREF(v_new);
669 goto error;
670 }
671 Py_DECREF(v_new);
672 }
673 /* It's a cell, or already free in this scope */
674 Py_DECREF(name);
675 continue;
676 }
677 /* Handle global symbol */
678 if (!PySet_Contains(bound, name)) {
679 Py_DECREF(name);
680 continue; /* it's a global */
681 }
682 /* Propagate new free symbol up the lexical stack */
683 if (PyDict_SetItem(symbols, name, v_free) < 0) {
684 goto error;
685 }
686 Py_DECREF(name);
687 }
688 Py_DECREF(itr);
689 Py_DECREF(v_free);
690 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000691error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 Py_XDECREF(v_free);
693 Py_XDECREF(itr);
694 Py_XDECREF(name);
695 return 0;
696}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697
698/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000699
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 Arguments:
701 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000702 bound -- set of variables bound in enclosing scopes (input). bound
703 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 free -- set of free variables in enclosed scopes (output)
705 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000706
707 The implementation uses two mutually recursive functions,
708 analyze_block() and analyze_child_block(). analyze_block() is
709 responsible for analyzing the individual names defined in a block.
710 analyze_child_block() prepares temporary namespace dictionaries
711 used to evaluated nested blocks.
712
713 The two functions exist because a child block should see the name
714 bindings of its enclosing blocks, but those bindings should not
715 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716*/
717
718static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
720 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000721
722static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
724 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
727 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
728 PyObject *temp;
729 int i, success = 0;
730 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 local = PySet_New(NULL); /* collect new names bound in block */
733 if (!local)
734 goto error;
735 scopes = PyDict_New(); /* collect scopes defined for each name */
736 if (!scopes)
737 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 /* Allocate new global and bound variable dictionaries. These
740 dictionaries hold the names visible in nested blocks. For
741 ClassBlocks, the bound and global names are initialized
742 before analyzing names, because class bindings aren't
743 visible in methods. For other blocks, they are initialized
744 after names are analyzed.
745 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 /* TODO(jhylton): Package these dicts in a struct so that we
748 can write reasonable helper functions?
749 */
750 newglobal = PySet_New(NULL);
751 if (!newglobal)
752 goto error;
753 newfree = PySet_New(NULL);
754 if (!newfree)
755 goto error;
756 newbound = PySet_New(NULL);
757 if (!newbound)
758 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 /* Class namespace has no effect on names visible in
761 nested functions, so populate the global and bound
762 sets to be passed to child blocks before analyzing
763 this one.
764 */
765 if (ste->ste_type == ClassBlock) {
766 /* Pass down known globals */
767 temp = PyNumber_InPlaceOr(newglobal, global);
768 if (!temp)
769 goto error;
770 Py_DECREF(temp);
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 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
781 long flags = PyLong_AS_LONG(v);
782 if (!analyze_name(ste, scopes, name, flags,
783 bound, local, free, global))
784 goto error;
785 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 /* Populate global and bound sets to be passed to children. */
788 if (ste->ste_type != ClassBlock) {
789 /* Add function locals to bound set */
790 if (ste->ste_type == FunctionBlock) {
791 temp = PyNumber_InPlaceOr(newbound, local);
792 if (!temp)
793 goto error;
794 Py_DECREF(temp);
795 }
796 /* Pass down previously bound symbols */
797 if (bound) {
798 temp = PyNumber_InPlaceOr(newbound, bound);
799 if (!temp)
800 goto error;
801 Py_DECREF(temp);
802 }
803 /* Pass down known globals */
804 temp = PyNumber_InPlaceOr(newglobal, global);
805 if (!temp)
806 goto error;
807 Py_DECREF(temp);
808 }
809 else {
810 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000811 if (!GET_IDENTIFIER(__class__))
812 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (PySet_Add(newbound, __class__) < 0)
814 goto error;
815 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300817 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 newbound, newglobal now contain the names visible in
820 nested blocks. The free variables in the children will
821 be collected in allfree.
822 */
823 allfree = PySet_New(NULL);
824 if (!allfree)
825 goto error;
826 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
827 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
828 PySTEntryObject* entry;
829 assert(c && PySTEntry_Check(c));
830 entry = (PySTEntryObject*)c;
831 if (!analyze_child_block(entry, newbound, newfree, newglobal,
832 allfree))
833 goto error;
834 /* Check if any children have free variables */
835 if (entry->ste_free || entry->ste_child_free)
836 ste->ste_child_free = 1;
837 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 temp = PyNumber_InPlaceOr(newfree, allfree);
840 if (!temp)
841 goto error;
842 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500845 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500847 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 goto error;
849 /* Records the results of the analysis in the symbol table entry */
850 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
851 ste->ste_type == ClassBlock))
852 goto error;
853 if (!check_unoptimized(ste))
854 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 temp = PyNumber_InPlaceOr(free, newfree);
857 if (!temp)
858 goto error;
859 Py_DECREF(temp);
860 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 Py_XDECREF(scopes);
863 Py_XDECREF(local);
864 Py_XDECREF(newbound);
865 Py_XDECREF(newglobal);
866 Py_XDECREF(newfree);
867 Py_XDECREF(allfree);
868 if (!success)
869 assert(PyErr_Occurred());
870 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871}
872
873static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
875 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
878 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 These dictionary are used by all blocks enclosed by the
883 current block. The analyze_block() call modifies these
884 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 */
887 temp_bound = PySet_New(bound);
888 if (!temp_bound)
889 goto error;
890 temp_free = PySet_New(free);
891 if (!temp_free)
892 goto error;
893 temp_global = PySet_New(global);
894 if (!temp_global)
895 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
898 goto error;
899 temp = PyNumber_InPlaceOr(child_free, temp_free);
900 if (!temp)
901 goto error;
902 Py_DECREF(temp);
903 Py_DECREF(temp_bound);
904 Py_DECREF(temp_free);
905 Py_DECREF(temp_global);
906 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000907 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 Py_XDECREF(temp_bound);
909 Py_XDECREF(temp_free);
910 Py_XDECREF(temp_global);
911 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000912}
913
914static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915symtable_analyze(struct symtable *st)
916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 PyObject *free, *global;
918 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 free = PySet_New(NULL);
921 if (!free)
922 return 0;
923 global = PySet_New(NULL);
924 if (!global) {
925 Py_DECREF(free);
926 return 0;
927 }
928 r = analyze_block(st->st_top, NULL, free, global);
929 Py_DECREF(free);
930 Py_DECREF(global);
931 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932}
933
934
935static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000936symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937{
Victor Stinner14e461d2013-08-26 22:28:21 +0200938 PyObject *message = PyUnicode_FromString(msg);
939 if (message == NULL)
940 return 0;
941 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, message, st->st_filename,
942 lineno, NULL, NULL) < 0) {
943 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
945 PyErr_SetString(PyExc_SyntaxError, msg);
Victor Stinner14e461d2013-08-26 22:28:21 +0200946 PyErr_SyntaxLocationObject(st->st_filename, st->st_cur->ste_lineno,
947 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 }
949 return 0;
950 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200951 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953}
954
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000955/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 This reference is released when the block is exited, via the DECREF
957 in symtable_exit_block().
958*/
959
960static int
961symtable_exit_block(struct symtable *st, void *ast)
962{
Benjamin Peterson609da582011-06-29 22:52:39 -0500963 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964
Benjamin Peterson609da582011-06-29 22:52:39 -0500965 st->st_cur = NULL;
966 size = PyList_GET_SIZE(st->st_stack);
967 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500968 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500970 if (--size)
971 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 }
973 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974}
975
976static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000978 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979{
Benjamin Peterson609da582011-06-29 22:52:39 -0500980 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981
Benjamin Peterson609da582011-06-29 22:52:39 -0500982 ste = ste_new(st, name, block, ast, lineno, col_offset);
983 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500985 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
986 Py_DECREF(ste);
987 return 0;
988 }
989 prev = st->st_cur;
990 /* The entry is owned by the stack. Borrow it for st_cur. */
991 Py_DECREF(ste);
992 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000993 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 st->st_global = st->st_cur->ste_symbols;
995 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500996 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 return 0;
998 }
999 }
1000 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001}
1002
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001003static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004symtable_lookup(struct symtable *st, PyObject *name)
1005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 PyObject *o;
1007 PyObject *mangled = _Py_Mangle(st->st_private, name);
1008 if (!mangled)
1009 return 0;
1010 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
1011 Py_DECREF(mangled);
1012 if (!o)
1013 return 0;
1014 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015}
1016
1017static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 PyObject *o;
1021 PyObject *dict;
1022 long val;
1023 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024
Jeremy Hylton81e95022007-02-27 06:50:52 +00001025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (!mangled)
1027 return 0;
1028 dict = st->st_cur->ste_symbols;
1029 if ((o = PyDict_GetItem(dict, mangled))) {
1030 val = PyLong_AS_LONG(o);
1031 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1032 /* Is it better to use 'mangled' or 'name' here? */
1033 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001034 PyErr_SyntaxLocationObject(st->st_filename,
1035 st->st_cur->ste_lineno,
1036 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 goto error;
1038 }
1039 val |= flag;
1040 } else
1041 val = flag;
1042 o = PyLong_FromLong(val);
1043 if (o == NULL)
1044 goto error;
1045 if (PyDict_SetItem(dict, mangled, o) < 0) {
1046 Py_DECREF(o);
1047 goto error;
1048 }
1049 Py_DECREF(o);
1050
1051 if (flag & DEF_PARAM) {
1052 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1053 goto error;
1054 } else if (flag & DEF_GLOBAL) {
1055 /* XXX need to update DEF_GLOBAL for other flags too;
1056 perhaps only DEF_FREE_GLOBAL */
1057 val = flag;
1058 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1059 val |= PyLong_AS_LONG(o);
1060 }
1061 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 goto error;
1064 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1065 Py_DECREF(o);
1066 goto error;
1067 }
1068 Py_DECREF(o);
1069 }
1070 Py_DECREF(mangled);
1071 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001072
1073error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 Py_DECREF(mangled);
1075 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076}
1077
1078/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1079 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 function.
1081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1083 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001084
1085 VISIT_QUIT macro returns the specified value exiting from the function but
1086 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087*/
1088
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001089#define VISIT_QUIT(ST, X) \
1090 return --(ST)->recursion_depth,(X)
1091
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001094 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 int i; \
1098 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1099 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1100 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1101 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001102 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 int i; \
1108 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1109 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1110 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1111 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001112 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001115
Guido van Rossum4f72a782006-10-27 23:31:49 +00001116#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 int i = 0; \
1118 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1119 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1120 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1121 if (!elt) continue; /* can be NULL */ \
1122 if (!symtable_visit_expr((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001123 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001125}
1126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001128symtable_new_tmpname(struct symtable *st)
1129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 char tmpname[256];
1131 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1134 ++st->st_cur->ste_tmpname);
1135 tmp = PyUnicode_InternFromString(tmpname);
1136 if (!tmp)
1137 return 0;
1138 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1139 return 0;
1140 Py_DECREF(tmp);
1141 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001142}
1143
Guido van Rossum4f72a782006-10-27 23:31:49 +00001144
Guido van Rossumc2e20742006-02-27 22:32:47 +00001145static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001146symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1147{
1148 PyObject *data;
1149 int res;
1150 if (!st->st_cur->ste_directives) {
1151 st->st_cur->ste_directives = PyList_New(0);
1152 if (!st->st_cur->ste_directives)
1153 return 0;
1154 }
1155 data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset);
1156 if (!data)
1157 return 0;
1158 res = PyList_Append(st->st_cur->ste_directives, data);
1159 Py_DECREF(data);
1160 return res == 0;
1161}
1162
1163
1164static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165symtable_visit_stmt(struct symtable *st, stmt_ty s)
1166{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001167 if (++st->recursion_depth > st->recursion_limit) {
1168 PyErr_SetString(PyExc_RuntimeError,
1169 "maximum recursion depth exceeded during compilation");
1170 VISIT_QUIT(st, 0);
1171 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 switch (s->kind) {
1173 case FunctionDef_kind:
1174 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001175 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 if (s->v.FunctionDef.args->defaults)
1177 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1178 if (s->v.FunctionDef.args->kw_defaults)
1179 VISIT_KWONLYDEFAULTS(st,
1180 s->v.FunctionDef.args->kw_defaults);
1181 if (!symtable_visit_annotations(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001182 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 if (s->v.FunctionDef.decorator_list)
1184 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1185 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001186 FunctionBlock, (void *)s, s->lineno,
1187 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001188 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001189 VISIT(st, arguments, s->v.FunctionDef.args);
1190 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001192 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 break;
1194 case ClassDef_kind: {
1195 PyObject *tmp;
1196 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001197 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1199 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1200 if (s->v.ClassDef.starargs)
1201 VISIT(st, expr, s->v.ClassDef.starargs);
1202 if (s->v.ClassDef.kwargs)
1203 VISIT(st, expr, s->v.ClassDef.kwargs);
1204 if (s->v.ClassDef.decorator_list)
1205 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1206 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001207 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001208 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 tmp = st->st_private;
1210 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001211 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 st->st_private = tmp;
1213 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001214 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 break;
1216 }
1217 case Return_kind:
1218 if (s->v.Return.value) {
1219 VISIT(st, expr, s->v.Return.value);
1220 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 }
1222 break;
1223 case Delete_kind:
1224 VISIT_SEQ(st, expr, s->v.Delete.targets);
1225 break;
1226 case Assign_kind:
1227 VISIT_SEQ(st, expr, s->v.Assign.targets);
1228 VISIT(st, expr, s->v.Assign.value);
1229 break;
1230 case AugAssign_kind:
1231 VISIT(st, expr, s->v.AugAssign.target);
1232 VISIT(st, expr, s->v.AugAssign.value);
1233 break;
1234 case For_kind:
1235 VISIT(st, expr, s->v.For.target);
1236 VISIT(st, expr, s->v.For.iter);
1237 VISIT_SEQ(st, stmt, s->v.For.body);
1238 if (s->v.For.orelse)
1239 VISIT_SEQ(st, stmt, s->v.For.orelse);
1240 break;
1241 case While_kind:
1242 VISIT(st, expr, s->v.While.test);
1243 VISIT_SEQ(st, stmt, s->v.While.body);
1244 if (s->v.While.orelse)
1245 VISIT_SEQ(st, stmt, s->v.While.orelse);
1246 break;
1247 case If_kind:
1248 /* XXX if 0: and lookup_yield() hacks */
1249 VISIT(st, expr, s->v.If.test);
1250 VISIT_SEQ(st, stmt, s->v.If.body);
1251 if (s->v.If.orelse)
1252 VISIT_SEQ(st, stmt, s->v.If.orelse);
1253 break;
1254 case Raise_kind:
1255 if (s->v.Raise.exc) {
1256 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001257 if (s->v.Raise.cause) {
1258 VISIT(st, expr, s->v.Raise.cause);
1259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 }
1261 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001262 case Try_kind:
1263 VISIT_SEQ(st, stmt, s->v.Try.body);
1264 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1265 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1266 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 break;
1268 case Assert_kind:
1269 VISIT(st, expr, s->v.Assert.test);
1270 if (s->v.Assert.msg)
1271 VISIT(st, expr, s->v.Assert.msg);
1272 break;
1273 case Import_kind:
1274 VISIT_SEQ(st, alias, s->v.Import.names);
1275 /* XXX Don't have the lineno available inside
1276 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001277 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001279 st->st_cur->ste_opt_col_offset = s->col_offset;
1280 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 break;
1282 case ImportFrom_kind:
1283 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1284 /* XXX Don't have the lineno available inside
1285 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001286 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001288 st->st_cur->ste_opt_col_offset = s->col_offset;
1289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 break;
1291 case Global_kind: {
1292 int i;
1293 asdl_seq *seq = s->v.Global.names;
1294 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1295 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 long cur = symtable_lookup(st, name);
1297 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001298 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 if (cur & (DEF_LOCAL | USE)) {
1300 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001301 char *c_name = _PyUnicode_AsString(name);
1302 if (!c_name)
1303 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 if (cur & DEF_LOCAL)
1305 PyOS_snprintf(buf, sizeof(buf),
1306 GLOBAL_AFTER_ASSIGN,
1307 c_name);
1308 else
1309 PyOS_snprintf(buf, sizeof(buf),
1310 GLOBAL_AFTER_USE,
1311 c_name);
1312 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001313 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 }
1315 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001316 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001317 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001318 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 }
1320 break;
1321 }
1322 case Nonlocal_kind: {
1323 int i;
1324 asdl_seq *seq = s->v.Nonlocal.names;
1325 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1326 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 long cur = symtable_lookup(st, name);
1328 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001329 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 if (cur & (DEF_LOCAL | USE)) {
1331 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001332 char *c_name = _PyUnicode_AsString(name);
1333 if (!c_name)
1334 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (cur & DEF_LOCAL)
1336 PyOS_snprintf(buf, sizeof(buf),
1337 NONLOCAL_AFTER_ASSIGN,
1338 c_name);
1339 else
1340 PyOS_snprintf(buf, sizeof(buf),
1341 NONLOCAL_AFTER_USE,
1342 c_name);
1343 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001344 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 }
1346 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001347 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001348 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001349 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 }
1351 break;
1352 }
1353 case Expr_kind:
1354 VISIT(st, expr, s->v.Expr.value);
1355 break;
1356 case Pass_kind:
1357 case Break_kind:
1358 case Continue_kind:
1359 /* nothing to do here */
1360 break;
1361 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001362 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 VISIT_SEQ(st, stmt, s->v.With.body);
1364 break;
1365 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001366 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367}
1368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370symtable_visit_expr(struct symtable *st, expr_ty e)
1371{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001372 if (++st->recursion_depth > st->recursion_limit) {
1373 PyErr_SetString(PyExc_RuntimeError,
1374 "maximum recursion depth exceeded during compilation");
1375 VISIT_QUIT(st, 0);
1376 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 switch (e->kind) {
1378 case BoolOp_kind:
1379 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1380 break;
1381 case BinOp_kind:
1382 VISIT(st, expr, e->v.BinOp.left);
1383 VISIT(st, expr, e->v.BinOp.right);
1384 break;
1385 case UnaryOp_kind:
1386 VISIT(st, expr, e->v.UnaryOp.operand);
1387 break;
1388 case Lambda_kind: {
1389 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001390 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 if (e->v.Lambda.args->defaults)
1392 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001393 if (e->v.Lambda.args->kw_defaults)
1394 VISIT_KWONLYDEFAULTS(st,
1395 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001397 FunctionBlock, (void *)e, e->lineno,
1398 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001399 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001400 VISIT(st, arguments, e->v.Lambda.args);
1401 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001403 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 break;
1405 }
1406 case IfExp_kind:
1407 VISIT(st, expr, e->v.IfExp.test);
1408 VISIT(st, expr, e->v.IfExp.body);
1409 VISIT(st, expr, e->v.IfExp.orelse);
1410 break;
1411 case Dict_kind:
1412 VISIT_SEQ(st, expr, e->v.Dict.keys);
1413 VISIT_SEQ(st, expr, e->v.Dict.values);
1414 break;
1415 case Set_kind:
1416 VISIT_SEQ(st, expr, e->v.Set.elts);
1417 break;
1418 case GeneratorExp_kind:
1419 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001420 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 break;
1422 case ListComp_kind:
1423 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001424 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 break;
1426 case SetComp_kind:
1427 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001428 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 break;
1430 case DictComp_kind:
1431 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001432 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 break;
1434 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001435 if (e->v.Yield.value)
1436 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001439 case YieldFrom_kind:
1440 VISIT(st, expr, e->v.YieldFrom.value);
1441 st->st_cur->ste_generator = 1;
1442 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 case Compare_kind:
1444 VISIT(st, expr, e->v.Compare.left);
1445 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1446 break;
1447 case Call_kind:
1448 VISIT(st, expr, e->v.Call.func);
1449 VISIT_SEQ(st, expr, e->v.Call.args);
1450 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1451 if (e->v.Call.starargs)
1452 VISIT(st, expr, e->v.Call.starargs);
1453 if (e->v.Call.kwargs)
1454 VISIT(st, expr, e->v.Call.kwargs);
1455 break;
1456 case Num_kind:
1457 case Str_kind:
1458 case Bytes_kind:
1459 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001460 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 /* Nothing to do here. */
1462 break;
1463 /* The following exprs can be assignment targets. */
1464 case Attribute_kind:
1465 VISIT(st, expr, e->v.Attribute.value);
1466 break;
1467 case Subscript_kind:
1468 VISIT(st, expr, e->v.Subscript.value);
1469 VISIT(st, slice, e->v.Subscript.slice);
1470 break;
1471 case Starred_kind:
1472 VISIT(st, expr, e->v.Starred.value);
1473 break;
1474 case Name_kind:
1475 if (!symtable_add_def(st, e->v.Name.id,
1476 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001477 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 /* Special-case super: it counts as a use of __class__ */
1479 if (e->v.Name.ctx == Load &&
1480 st->st_cur->ste_type == FunctionBlock &&
1481 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001482 if (!GET_IDENTIFIER(__class__) ||
1483 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001484 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 }
1486 break;
1487 /* child nodes of List and Tuple will have expr_context set */
1488 case List_kind:
1489 VISIT_SEQ(st, expr, e->v.List.elts);
1490 break;
1491 case Tuple_kind:
1492 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1493 break;
1494 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001495 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496}
1497
1498static int
1499symtable_implicit_arg(struct symtable *st, int pos)
1500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1502 if (id == NULL)
1503 return 0;
1504 if (!symtable_add_def(st, id, DEF_PARAM)) {
1505 Py_DECREF(id);
1506 return 0;
1507 }
1508 Py_DECREF(id);
1509 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510}
1511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001513symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 if (!args)
1518 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 for (i = 0; i < asdl_seq_LEN(args); i++) {
1521 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1522 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1523 return 0;
1524 }
1525
1526 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527}
1528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001530symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (!args)
1535 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 for (i = 0; i < asdl_seq_LEN(args); i++) {
1538 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1539 if (arg->annotation)
1540 VISIT(st, expr, arg->annotation);
1541 }
1542
1543 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001544}
1545
Neal Norwitzc1505362006-12-28 06:47:50 +00001546static int
1547symtable_visit_annotations(struct symtable *st, stmt_ty s)
1548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 arguments_ty a = s->v.FunctionDef.args;
1550
1551 if (a->args && !symtable_visit_argannotations(st, a->args))
1552 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001553 if (a->vararg && a->vararg->annotation)
1554 VISIT(st, expr, a->vararg->annotation);
1555 if (a->kwarg && a->kwarg->annotation)
1556 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1558 return 0;
1559 if (s->v.FunctionDef.returns)
1560 VISIT(st, expr, s->v.FunctionDef.returns);
1561 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562}
1563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565symtable_visit_arguments(struct symtable *st, arguments_ty a)
1566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 /* skip default arguments inside function block
1568 XXX should ast be different?
1569 */
1570 if (a->args && !symtable_visit_params(st, a->args))
1571 return 0;
1572 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1573 return 0;
1574 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001575 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 return 0;
1577 st->st_cur->ste_varargs = 1;
1578 }
1579 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001580 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 return 0;
1582 st->st_cur->ste_varkeywords = 1;
1583 }
1584 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585}
1586
1587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 if (eh->v.ExceptHandler.type)
1592 VISIT(st, expr, eh->v.ExceptHandler.type);
1593 if (eh->v.ExceptHandler.name)
1594 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1595 return 0;
1596 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1597 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598}
1599
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001600static int
1601symtable_visit_withitem(struct symtable *st, withitem_ty item)
1602{
1603 VISIT(st, expr, item->context_expr);
1604 if (item->optional_vars) {
1605 VISIT(st, expr, item->optional_vars);
1606 }
1607 return 1;
1608}
1609
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612symtable_visit_alias(struct symtable *st, alias_ty a)
1613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001615 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 dotted package name (e.g. spam.eggs)
1617 */
1618 PyObject *store_name;
1619 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001620 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1621 PyUnicode_GET_LENGTH(name), 1);
1622 if (dot != -1) {
1623 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 if (!store_name)
1625 return 0;
1626 }
1627 else {
1628 store_name = name;
1629 Py_INCREF(store_name);
1630 }
1631 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1632 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1633 Py_DECREF(store_name);
1634 return r;
1635 }
1636 else {
1637 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001638 int lineno = st->st_cur->ste_lineno;
1639 int col_offset = st->st_cur->ste_col_offset;
1640 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001641 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001642 Py_DECREF(store_name);
1643 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 }
1645 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1646 Py_DECREF(store_name);
1647 return 1;
1648 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649}
1650
1651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 VISIT(st, expr, lc->target);
1656 VISIT(st, expr, lc->iter);
1657 VISIT_SEQ(st, expr, lc->ifs);
1658 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659}
1660
1661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663symtable_visit_keyword(struct symtable *st, keyword_ty k)
1664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 VISIT(st, expr, k->value);
1666 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667}
1668
1669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671symtable_visit_slice(struct symtable *st, slice_ty s)
1672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 switch (s->kind) {
1674 case Slice_kind:
1675 if (s->v.Slice.lower)
1676 VISIT(st, expr, s->v.Slice.lower)
1677 if (s->v.Slice.upper)
1678 VISIT(st, expr, s->v.Slice.upper)
1679 if (s->v.Slice.step)
1680 VISIT(st, expr, s->v.Slice.step)
1681 break;
1682 case ExtSlice_kind:
1683 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1684 break;
1685 case Index_kind:
1686 VISIT(st, expr, s->v.Index.value)
1687 break;
1688 }
1689 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690}
1691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001693symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001694 identifier scope_name, asdl_seq *generators,
1695 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 int is_generator = (e->kind == GeneratorExp_kind);
1698 int needs_tmp = !is_generator;
1699 comprehension_ty outermost = ((comprehension_ty)
1700 asdl_seq_GET(generators, 0));
1701 /* Outermost iterator is evaluated in current scope */
1702 VISIT(st, expr, outermost->iter);
1703 /* Create comprehension scope for the rest */
1704 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001705 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1706 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 return 0;
1708 }
1709 st->st_cur->ste_generator = is_generator;
1710 /* Outermost iter is received as an argument */
1711 if (!symtable_implicit_arg(st, 0)) {
1712 symtable_exit_block(st, (void *)e);
1713 return 0;
1714 }
1715 /* Allocate temporary name if needed */
1716 if (needs_tmp && !symtable_new_tmpname(st)) {
1717 symtable_exit_block(st, (void *)e);
1718 return 0;
1719 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001720 VISIT(st, expr, outermost->target);
1721 VISIT_SEQ(st, expr, outermost->ifs);
1722 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001724 VISIT(st, expr, value);
1725 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001730symtable_visit_genexp(struct symtable *st, expr_ty e)
1731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1733 e->v.GeneratorExp.generators,
1734 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001735}
1736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001738symtable_visit_listcomp(struct symtable *st, expr_ty e)
1739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1741 e->v.ListComp.generators,
1742 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001743}
1744
1745static int
1746symtable_visit_setcomp(struct symtable *st, expr_ty e)
1747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1749 e->v.SetComp.generators,
1750 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001751}
1752
1753static int
1754symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1757 e->v.DictComp.generators,
1758 e->v.DictComp.key,
1759 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001760}