blob: da164aa877d3201d58f2c63590958770ef507690 [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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586/* Check for illegal statements in unoptimized namespaces */
587static int
588check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
592 || !(ste->ste_free || ste->ste_child_free))
593 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 trailer = (ste->ste_child_free ?
596 "contains a nested function with free variables" :
597 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 switch (ste->ste_unoptimized) {
600 case OPT_TOPLEVEL: /* import * at top-level is fine */
601 return 1;
602 case OPT_IMPORT_STAR:
603 PyErr_Format(PyExc_SyntaxError,
604 "import * is not allowed in function '%U' because it %s",
605 ste->ste_name, trailer);
606 break;
607 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608
Victor Stinner14e461d2013-08-26 22:28:21 +0200609 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
610 ste->ste_opt_lineno,
611 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613}
614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615/* Enter the final scope information into the ste_symbols dict.
616 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617 * All arguments are dicts. Modifies symbols, others are read-only.
618*/
619static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000621 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 PyObject *name = NULL, *itr = NULL;
624 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
625 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* Update scope information for all symbols in this scope */
628 while (PyDict_Next(symbols, &pos, &name, &v)) {
629 long scope, flags;
630 assert(PyLong_Check(v));
631 flags = PyLong_AS_LONG(v);
632 v_scope = PyDict_GetItem(scopes, name);
633 assert(v_scope && PyLong_Check(v_scope));
634 scope = PyLong_AS_LONG(v_scope);
635 flags |= (scope << SCOPE_OFFSET);
636 v_new = PyLong_FromLong(flags);
637 if (!v_new)
638 return 0;
639 if (PyDict_SetItem(symbols, name, v_new) < 0) {
640 Py_DECREF(v_new);
641 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 Py_DECREF(v_new);
644 }
645
646 /* Record not yet resolved free variables from children (if any) */
647 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
648 if (!v_free)
649 return 0;
650
651 itr = PyObject_GetIter(free);
652 if (!itr)
653 goto error;
654
655 while ((name = PyIter_Next(itr))) {
656 v = PyDict_GetItem(symbols, name);
657
658 /* Handle symbol that already exists in this scope */
659 if (v) {
660 /* Handle a free variable in a method of
661 the class that has the same name as a local
662 or global in the class scope.
663 */
664 if (classflag &&
665 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
666 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
667 v_new = PyLong_FromLong(flags);
668 if (!v_new) {
669 goto error;
670 }
671 if (PyDict_SetItem(symbols, name, v_new) < 0) {
672 Py_DECREF(v_new);
673 goto error;
674 }
675 Py_DECREF(v_new);
676 }
677 /* It's a cell, or already free in this scope */
678 Py_DECREF(name);
679 continue;
680 }
681 /* Handle global symbol */
682 if (!PySet_Contains(bound, name)) {
683 Py_DECREF(name);
684 continue; /* it's a global */
685 }
686 /* Propagate new free symbol up the lexical stack */
687 if (PyDict_SetItem(symbols, name, v_free) < 0) {
688 goto error;
689 }
690 Py_DECREF(name);
691 }
692 Py_DECREF(itr);
693 Py_DECREF(v_free);
694 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000695error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 Py_XDECREF(v_free);
697 Py_XDECREF(itr);
698 Py_XDECREF(name);
699 return 0;
700}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701
702/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000703
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 Arguments:
705 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000706 bound -- set of variables bound in enclosing scopes (input). bound
707 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708 free -- set of free variables in enclosed scopes (output)
709 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000710
711 The implementation uses two mutually recursive functions,
712 analyze_block() and analyze_child_block(). analyze_block() is
713 responsible for analyzing the individual names defined in a block.
714 analyze_child_block() prepares temporary namespace dictionaries
715 used to evaluated nested blocks.
716
717 The two functions exist because a child block should see the name
718 bindings of its enclosing blocks, but those bindings should not
719 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720*/
721
722static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
724 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000725
726static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
728 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
731 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
732 PyObject *temp;
733 int i, success = 0;
734 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 local = PySet_New(NULL); /* collect new names bound in block */
737 if (!local)
738 goto error;
739 scopes = PyDict_New(); /* collect scopes defined for each name */
740 if (!scopes)
741 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 /* Allocate new global and bound variable dictionaries. These
744 dictionaries hold the names visible in nested blocks. For
745 ClassBlocks, the bound and global names are initialized
746 before analyzing names, because class bindings aren't
747 visible in methods. For other blocks, they are initialized
748 after names are analyzed.
749 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 /* TODO(jhylton): Package these dicts in a struct so that we
752 can write reasonable helper functions?
753 */
754 newglobal = PySet_New(NULL);
755 if (!newglobal)
756 goto error;
757 newfree = PySet_New(NULL);
758 if (!newfree)
759 goto error;
760 newbound = PySet_New(NULL);
761 if (!newbound)
762 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 /* Class namespace has no effect on names visible in
765 nested functions, so populate the global and bound
766 sets to be passed to child blocks before analyzing
767 this one.
768 */
769 if (ste->ste_type == ClassBlock) {
770 /* Pass down known globals */
771 temp = PyNumber_InPlaceOr(newglobal, global);
772 if (!temp)
773 goto error;
774 Py_DECREF(temp);
775 /* Pass down previously bound symbols */
776 if (bound) {
777 temp = PyNumber_InPlaceOr(newbound, bound);
778 if (!temp)
779 goto error;
780 Py_DECREF(temp);
781 }
782 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
785 long flags = PyLong_AS_LONG(v);
786 if (!analyze_name(ste, scopes, name, flags,
787 bound, local, free, global))
788 goto error;
789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 /* Populate global and bound sets to be passed to children. */
792 if (ste->ste_type != ClassBlock) {
793 /* Add function locals to bound set */
794 if (ste->ste_type == FunctionBlock) {
795 temp = PyNumber_InPlaceOr(newbound, local);
796 if (!temp)
797 goto error;
798 Py_DECREF(temp);
799 }
800 /* Pass down previously bound symbols */
801 if (bound) {
802 temp = PyNumber_InPlaceOr(newbound, bound);
803 if (!temp)
804 goto error;
805 Py_DECREF(temp);
806 }
807 /* Pass down known globals */
808 temp = PyNumber_InPlaceOr(newglobal, global);
809 if (!temp)
810 goto error;
811 Py_DECREF(temp);
812 }
813 else {
814 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000815 if (!GET_IDENTIFIER(__class__))
816 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 if (PySet_Add(newbound, __class__) < 0)
818 goto error;
819 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300821 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 newbound, newglobal now contain the names visible in
824 nested blocks. The free variables in the children will
825 be collected in allfree.
826 */
827 allfree = PySet_New(NULL);
828 if (!allfree)
829 goto error;
830 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
831 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
832 PySTEntryObject* entry;
833 assert(c && PySTEntry_Check(c));
834 entry = (PySTEntryObject*)c;
835 if (!analyze_child_block(entry, newbound, newfree, newglobal,
836 allfree))
837 goto error;
838 /* Check if any children have free variables */
839 if (entry->ste_free || entry->ste_child_free)
840 ste->ste_child_free = 1;
841 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 temp = PyNumber_InPlaceOr(newfree, allfree);
844 if (!temp)
845 goto error;
846 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500849 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500851 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 goto error;
853 /* Records the results of the analysis in the symbol table entry */
854 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
855 ste->ste_type == ClassBlock))
856 goto error;
857 if (!check_unoptimized(ste))
858 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 temp = PyNumber_InPlaceOr(free, newfree);
861 if (!temp)
862 goto error;
863 Py_DECREF(temp);
864 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 Py_XDECREF(scopes);
867 Py_XDECREF(local);
868 Py_XDECREF(newbound);
869 Py_XDECREF(newglobal);
870 Py_XDECREF(newfree);
871 Py_XDECREF(allfree);
872 if (!success)
873 assert(PyErr_Occurred());
874 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875}
876
877static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
879 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
882 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 These dictionary are used by all blocks enclosed by the
887 current block. The analyze_block() call modifies these
888 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 */
891 temp_bound = PySet_New(bound);
892 if (!temp_bound)
893 goto error;
894 temp_free = PySet_New(free);
895 if (!temp_free)
896 goto error;
897 temp_global = PySet_New(global);
898 if (!temp_global)
899 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
902 goto error;
903 temp = PyNumber_InPlaceOr(child_free, temp_free);
904 if (!temp)
905 goto error;
906 Py_DECREF(temp);
907 Py_DECREF(temp_bound);
908 Py_DECREF(temp_free);
909 Py_DECREF(temp_global);
910 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000911 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 Py_XDECREF(temp_bound);
913 Py_XDECREF(temp_free);
914 Py_XDECREF(temp_global);
915 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000916}
917
918static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919symtable_analyze(struct symtable *st)
920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 PyObject *free, *global;
922 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 free = PySet_New(NULL);
925 if (!free)
926 return 0;
927 global = PySet_New(NULL);
928 if (!global) {
929 Py_DECREF(free);
930 return 0;
931 }
932 r = analyze_block(st->st_top, NULL, free, global);
933 Py_DECREF(free);
934 Py_DECREF(global);
935 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936}
937
938
939static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000940symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941{
Victor Stinner14e461d2013-08-26 22:28:21 +0200942 PyObject *message = PyUnicode_FromString(msg);
943 if (message == NULL)
944 return 0;
945 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, message, st->st_filename,
946 lineno, NULL, NULL) < 0) {
947 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
949 PyErr_SetString(PyExc_SyntaxError, msg);
Victor Stinner14e461d2013-08-26 22:28:21 +0200950 PyErr_SyntaxLocationObject(st->st_filename, st->st_cur->ste_lineno,
951 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 }
953 return 0;
954 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200955 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957}
958
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000959/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 This reference is released when the block is exited, via the DECREF
961 in symtable_exit_block().
962*/
963
964static int
965symtable_exit_block(struct symtable *st, void *ast)
966{
Benjamin Peterson609da582011-06-29 22:52:39 -0500967 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968
Benjamin Peterson609da582011-06-29 22:52:39 -0500969 st->st_cur = NULL;
970 size = PyList_GET_SIZE(st->st_stack);
971 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500972 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500974 if (--size)
975 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 }
977 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978}
979
980static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000982 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983{
Benjamin Peterson609da582011-06-29 22:52:39 -0500984 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985
Benjamin Peterson609da582011-06-29 22:52:39 -0500986 ste = ste_new(st, name, block, ast, lineno, col_offset);
987 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500989 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
990 Py_DECREF(ste);
991 return 0;
992 }
993 prev = st->st_cur;
994 /* The entry is owned by the stack. Borrow it for st_cur. */
995 Py_DECREF(ste);
996 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000997 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 st->st_global = st->st_cur->ste_symbols;
999 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -05001000 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 return 0;
1002 }
1003 }
1004 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005}
1006
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001007static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008symtable_lookup(struct symtable *st, PyObject *name)
1009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 PyObject *o;
1011 PyObject *mangled = _Py_Mangle(st->st_private, name);
1012 if (!mangled)
1013 return 0;
1014 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
1015 Py_DECREF(mangled);
1016 if (!o)
1017 return 0;
1018 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019}
1020
1021static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 PyObject *o;
1025 PyObject *dict;
1026 long val;
1027 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028
Jeremy Hylton81e95022007-02-27 06:50:52 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 if (!mangled)
1031 return 0;
1032 dict = st->st_cur->ste_symbols;
1033 if ((o = PyDict_GetItem(dict, mangled))) {
1034 val = PyLong_AS_LONG(o);
1035 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1036 /* Is it better to use 'mangled' or 'name' here? */
1037 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001038 PyErr_SyntaxLocationObject(st->st_filename,
1039 st->st_cur->ste_lineno,
1040 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 goto error;
1042 }
1043 val |= flag;
1044 } else
1045 val = flag;
1046 o = PyLong_FromLong(val);
1047 if (o == NULL)
1048 goto error;
1049 if (PyDict_SetItem(dict, mangled, o) < 0) {
1050 Py_DECREF(o);
1051 goto error;
1052 }
1053 Py_DECREF(o);
1054
1055 if (flag & DEF_PARAM) {
1056 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1057 goto error;
1058 } else if (flag & DEF_GLOBAL) {
1059 /* XXX need to update DEF_GLOBAL for other flags too;
1060 perhaps only DEF_FREE_GLOBAL */
1061 val = flag;
1062 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1063 val |= PyLong_AS_LONG(o);
1064 }
1065 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 goto error;
1068 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1069 Py_DECREF(o);
1070 goto error;
1071 }
1072 Py_DECREF(o);
1073 }
1074 Py_DECREF(mangled);
1075 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001076
1077error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 Py_DECREF(mangled);
1079 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080}
1081
1082/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1083 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 function.
1085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1087 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001088
1089 VISIT_QUIT macro returns the specified value exiting from the function but
1090 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091*/
1092
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001093#define VISIT_QUIT(ST, X) \
1094 return --(ST)->recursion_depth,(X)
1095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001098 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001099
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 int i; \
1102 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1103 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1104 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1105 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001106 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 int i; \
1112 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1113 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1114 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1115 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001116 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001119
Guido van Rossum4f72a782006-10-27 23:31:49 +00001120#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 int i = 0; \
1122 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1123 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1124 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1125 if (!elt) continue; /* can be NULL */ \
1126 if (!symtable_visit_expr((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001127 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001129}
1130
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001132symtable_new_tmpname(struct symtable *st)
1133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 char tmpname[256];
1135 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1138 ++st->st_cur->ste_tmpname);
1139 tmp = PyUnicode_InternFromString(tmpname);
1140 if (!tmp)
1141 return 0;
1142 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1143 return 0;
1144 Py_DECREF(tmp);
1145 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001146}
1147
Guido van Rossum4f72a782006-10-27 23:31:49 +00001148
Guido van Rossumc2e20742006-02-27 22:32:47 +00001149static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001150symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1151{
1152 PyObject *data;
1153 int res;
1154 if (!st->st_cur->ste_directives) {
1155 st->st_cur->ste_directives = PyList_New(0);
1156 if (!st->st_cur->ste_directives)
1157 return 0;
1158 }
1159 data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset);
1160 if (!data)
1161 return 0;
1162 res = PyList_Append(st->st_cur->ste_directives, data);
1163 Py_DECREF(data);
1164 return res == 0;
1165}
1166
1167
1168static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169symtable_visit_stmt(struct symtable *st, stmt_ty s)
1170{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001171 if (++st->recursion_depth > st->recursion_limit) {
1172 PyErr_SetString(PyExc_RuntimeError,
1173 "maximum recursion depth exceeded during compilation");
1174 VISIT_QUIT(st, 0);
1175 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 switch (s->kind) {
1177 case FunctionDef_kind:
1178 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001179 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 if (s->v.FunctionDef.args->defaults)
1181 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1182 if (s->v.FunctionDef.args->kw_defaults)
1183 VISIT_KWONLYDEFAULTS(st,
1184 s->v.FunctionDef.args->kw_defaults);
1185 if (!symtable_visit_annotations(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001186 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (s->v.FunctionDef.decorator_list)
1188 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1189 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001190 FunctionBlock, (void *)s, s->lineno,
1191 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001192 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001193 VISIT(st, arguments, s->v.FunctionDef.args);
1194 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001196 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 break;
1198 case ClassDef_kind: {
1199 PyObject *tmp;
1200 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001201 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1203 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1204 if (s->v.ClassDef.starargs)
1205 VISIT(st, expr, s->v.ClassDef.starargs);
1206 if (s->v.ClassDef.kwargs)
1207 VISIT(st, expr, s->v.ClassDef.kwargs);
1208 if (s->v.ClassDef.decorator_list)
1209 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1210 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001211 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001212 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 tmp = st->st_private;
1214 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001215 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 st->st_private = tmp;
1217 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001218 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 break;
1220 }
1221 case Return_kind:
1222 if (s->v.Return.value) {
1223 VISIT(st, expr, s->v.Return.value);
1224 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 }
1226 break;
1227 case Delete_kind:
1228 VISIT_SEQ(st, expr, s->v.Delete.targets);
1229 break;
1230 case Assign_kind:
1231 VISIT_SEQ(st, expr, s->v.Assign.targets);
1232 VISIT(st, expr, s->v.Assign.value);
1233 break;
1234 case AugAssign_kind:
1235 VISIT(st, expr, s->v.AugAssign.target);
1236 VISIT(st, expr, s->v.AugAssign.value);
1237 break;
1238 case For_kind:
1239 VISIT(st, expr, s->v.For.target);
1240 VISIT(st, expr, s->v.For.iter);
1241 VISIT_SEQ(st, stmt, s->v.For.body);
1242 if (s->v.For.orelse)
1243 VISIT_SEQ(st, stmt, s->v.For.orelse);
1244 break;
1245 case While_kind:
1246 VISIT(st, expr, s->v.While.test);
1247 VISIT_SEQ(st, stmt, s->v.While.body);
1248 if (s->v.While.orelse)
1249 VISIT_SEQ(st, stmt, s->v.While.orelse);
1250 break;
1251 case If_kind:
1252 /* XXX if 0: and lookup_yield() hacks */
1253 VISIT(st, expr, s->v.If.test);
1254 VISIT_SEQ(st, stmt, s->v.If.body);
1255 if (s->v.If.orelse)
1256 VISIT_SEQ(st, stmt, s->v.If.orelse);
1257 break;
1258 case Raise_kind:
1259 if (s->v.Raise.exc) {
1260 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001261 if (s->v.Raise.cause) {
1262 VISIT(st, expr, s->v.Raise.cause);
1263 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 }
1265 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001266 case Try_kind:
1267 VISIT_SEQ(st, stmt, s->v.Try.body);
1268 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1269 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1270 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 break;
1272 case Assert_kind:
1273 VISIT(st, expr, s->v.Assert.test);
1274 if (s->v.Assert.msg)
1275 VISIT(st, expr, s->v.Assert.msg);
1276 break;
1277 case Import_kind:
1278 VISIT_SEQ(st, alias, s->v.Import.names);
1279 /* XXX Don't have the lineno available inside
1280 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001281 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001283 st->st_cur->ste_opt_col_offset = s->col_offset;
1284 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 break;
1286 case ImportFrom_kind:
1287 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1288 /* XXX Don't have the lineno available inside
1289 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001290 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001292 st->st_cur->ste_opt_col_offset = s->col_offset;
1293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 break;
1295 case Global_kind: {
1296 int i;
1297 asdl_seq *seq = s->v.Global.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 GLOBAL_AFTER_ASSIGN,
1311 c_name);
1312 else
1313 PyOS_snprintf(buf, sizeof(buf),
1314 GLOBAL_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_GLOBAL))
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 Nonlocal_kind: {
1327 int i;
1328 asdl_seq *seq = s->v.Nonlocal.names;
1329 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1330 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 long cur = symtable_lookup(st, name);
1332 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001333 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 if (cur & (DEF_LOCAL | USE)) {
1335 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001336 char *c_name = _PyUnicode_AsString(name);
1337 if (!c_name)
1338 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 if (cur & DEF_LOCAL)
1340 PyOS_snprintf(buf, sizeof(buf),
1341 NONLOCAL_AFTER_ASSIGN,
1342 c_name);
1343 else
1344 PyOS_snprintf(buf, sizeof(buf),
1345 NONLOCAL_AFTER_USE,
1346 c_name);
1347 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001348 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 }
1350 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001351 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001352 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001353 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 }
1355 break;
1356 }
1357 case Expr_kind:
1358 VISIT(st, expr, s->v.Expr.value);
1359 break;
1360 case Pass_kind:
1361 case Break_kind:
1362 case Continue_kind:
1363 /* nothing to do here */
1364 break;
1365 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001366 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 VISIT_SEQ(st, stmt, s->v.With.body);
1368 break;
1369 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001370 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371}
1372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374symtable_visit_expr(struct symtable *st, expr_ty e)
1375{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001376 if (++st->recursion_depth > st->recursion_limit) {
1377 PyErr_SetString(PyExc_RuntimeError,
1378 "maximum recursion depth exceeded during compilation");
1379 VISIT_QUIT(st, 0);
1380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 switch (e->kind) {
1382 case BoolOp_kind:
1383 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1384 break;
1385 case BinOp_kind:
1386 VISIT(st, expr, e->v.BinOp.left);
1387 VISIT(st, expr, e->v.BinOp.right);
1388 break;
1389 case UnaryOp_kind:
1390 VISIT(st, expr, e->v.UnaryOp.operand);
1391 break;
1392 case Lambda_kind: {
1393 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001394 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 if (e->v.Lambda.args->defaults)
1396 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001397 if (e->v.Lambda.args->kw_defaults)
1398 VISIT_KWONLYDEFAULTS(st,
1399 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001401 FunctionBlock, (void *)e, e->lineno,
1402 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001403 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001404 VISIT(st, arguments, e->v.Lambda.args);
1405 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001407 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 break;
1409 }
1410 case IfExp_kind:
1411 VISIT(st, expr, e->v.IfExp.test);
1412 VISIT(st, expr, e->v.IfExp.body);
1413 VISIT(st, expr, e->v.IfExp.orelse);
1414 break;
1415 case Dict_kind:
1416 VISIT_SEQ(st, expr, e->v.Dict.keys);
1417 VISIT_SEQ(st, expr, e->v.Dict.values);
1418 break;
1419 case Set_kind:
1420 VISIT_SEQ(st, expr, e->v.Set.elts);
1421 break;
1422 case GeneratorExp_kind:
1423 if (!symtable_visit_genexp(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 ListComp_kind:
1427 if (!symtable_visit_listcomp(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 SetComp_kind:
1431 if (!symtable_visit_setcomp(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 DictComp_kind:
1435 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001436 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 break;
1438 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001439 if (e->v.Yield.value)
1440 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001443 case YieldFrom_kind:
1444 VISIT(st, expr, e->v.YieldFrom.value);
1445 st->st_cur->ste_generator = 1;
1446 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 case Compare_kind:
1448 VISIT(st, expr, e->v.Compare.left);
1449 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1450 break;
1451 case Call_kind:
1452 VISIT(st, expr, e->v.Call.func);
1453 VISIT_SEQ(st, expr, e->v.Call.args);
1454 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1455 if (e->v.Call.starargs)
1456 VISIT(st, expr, e->v.Call.starargs);
1457 if (e->v.Call.kwargs)
1458 VISIT(st, expr, e->v.Call.kwargs);
1459 break;
1460 case Num_kind:
1461 case Str_kind:
1462 case Bytes_kind:
1463 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001464 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 /* Nothing to do here. */
1466 break;
1467 /* The following exprs can be assignment targets. */
1468 case Attribute_kind:
1469 VISIT(st, expr, e->v.Attribute.value);
1470 break;
1471 case Subscript_kind:
1472 VISIT(st, expr, e->v.Subscript.value);
1473 VISIT(st, slice, e->v.Subscript.slice);
1474 break;
1475 case Starred_kind:
1476 VISIT(st, expr, e->v.Starred.value);
1477 break;
1478 case Name_kind:
1479 if (!symtable_add_def(st, e->v.Name.id,
1480 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001481 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 /* Special-case super: it counts as a use of __class__ */
1483 if (e->v.Name.ctx == Load &&
1484 st->st_cur->ste_type == FunctionBlock &&
1485 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001486 if (!GET_IDENTIFIER(__class__) ||
1487 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001488 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 }
1490 break;
1491 /* child nodes of List and Tuple will have expr_context set */
1492 case List_kind:
1493 VISIT_SEQ(st, expr, e->v.List.elts);
1494 break;
1495 case Tuple_kind:
1496 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1497 break;
1498 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001499 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500}
1501
1502static int
1503symtable_implicit_arg(struct symtable *st, int pos)
1504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1506 if (id == NULL)
1507 return 0;
1508 if (!symtable_add_def(st, id, DEF_PARAM)) {
1509 Py_DECREF(id);
1510 return 0;
1511 }
1512 Py_DECREF(id);
1513 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514}
1515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001517symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (!args)
1522 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 for (i = 0; i < asdl_seq_LEN(args); i++) {
1525 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1526 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1527 return 0;
1528 }
1529
1530 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531}
1532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001534symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 if (!args)
1539 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 for (i = 0; i < asdl_seq_LEN(args); i++) {
1542 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1543 if (arg->annotation)
1544 VISIT(st, expr, arg->annotation);
1545 }
1546
1547 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001548}
1549
Neal Norwitzc1505362006-12-28 06:47:50 +00001550static int
1551symtable_visit_annotations(struct symtable *st, stmt_ty s)
1552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 arguments_ty a = s->v.FunctionDef.args;
1554
1555 if (a->args && !symtable_visit_argannotations(st, a->args))
1556 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001557 if (a->vararg && a->vararg->annotation)
1558 VISIT(st, expr, a->vararg->annotation);
1559 if (a->kwarg && a->kwarg->annotation)
1560 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1562 return 0;
1563 if (s->v.FunctionDef.returns)
1564 VISIT(st, expr, s->v.FunctionDef.returns);
1565 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566}
1567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569symtable_visit_arguments(struct symtable *st, arguments_ty a)
1570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 /* skip default arguments inside function block
1572 XXX should ast be different?
1573 */
1574 if (a->args && !symtable_visit_params(st, a->args))
1575 return 0;
1576 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1577 return 0;
1578 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001579 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 return 0;
1581 st->st_cur->ste_varargs = 1;
1582 }
1583 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001584 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 return 0;
1586 st->st_cur->ste_varkeywords = 1;
1587 }
1588 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589}
1590
1591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 if (eh->v.ExceptHandler.type)
1596 VISIT(st, expr, eh->v.ExceptHandler.type);
1597 if (eh->v.ExceptHandler.name)
1598 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1599 return 0;
1600 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1601 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602}
1603
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001604static int
1605symtable_visit_withitem(struct symtable *st, withitem_ty item)
1606{
1607 VISIT(st, expr, item->context_expr);
1608 if (item->optional_vars) {
1609 VISIT(st, expr, item->optional_vars);
1610 }
1611 return 1;
1612}
1613
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616symtable_visit_alias(struct symtable *st, alias_ty a)
1617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001619 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 dotted package name (e.g. spam.eggs)
1621 */
1622 PyObject *store_name;
1623 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001624 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1625 PyUnicode_GET_LENGTH(name), 1);
1626 if (dot != -1) {
1627 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 if (!store_name)
1629 return 0;
1630 }
1631 else {
1632 store_name = name;
1633 Py_INCREF(store_name);
1634 }
1635 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1636 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1637 Py_DECREF(store_name);
1638 return r;
1639 }
1640 else {
1641 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001642 int lineno = st->st_cur->ste_lineno;
1643 int col_offset = st->st_cur->ste_col_offset;
1644 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001645 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001646 Py_DECREF(store_name);
1647 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 }
1649 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1650 Py_DECREF(store_name);
1651 return 1;
1652 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653}
1654
1655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 VISIT(st, expr, lc->target);
1660 VISIT(st, expr, lc->iter);
1661 VISIT_SEQ(st, expr, lc->ifs);
1662 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663}
1664
1665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667symtable_visit_keyword(struct symtable *st, keyword_ty k)
1668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 VISIT(st, expr, k->value);
1670 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671}
1672
1673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675symtable_visit_slice(struct symtable *st, slice_ty s)
1676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 switch (s->kind) {
1678 case Slice_kind:
1679 if (s->v.Slice.lower)
1680 VISIT(st, expr, s->v.Slice.lower)
1681 if (s->v.Slice.upper)
1682 VISIT(st, expr, s->v.Slice.upper)
1683 if (s->v.Slice.step)
1684 VISIT(st, expr, s->v.Slice.step)
1685 break;
1686 case ExtSlice_kind:
1687 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1688 break;
1689 case Index_kind:
1690 VISIT(st, expr, s->v.Index.value)
1691 break;
1692 }
1693 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694}
1695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001697symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001698 identifier scope_name, asdl_seq *generators,
1699 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 int is_generator = (e->kind == GeneratorExp_kind);
1702 int needs_tmp = !is_generator;
1703 comprehension_ty outermost = ((comprehension_ty)
1704 asdl_seq_GET(generators, 0));
1705 /* Outermost iterator is evaluated in current scope */
1706 VISIT(st, expr, outermost->iter);
1707 /* Create comprehension scope for the rest */
1708 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001709 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1710 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 return 0;
1712 }
1713 st->st_cur->ste_generator = is_generator;
1714 /* Outermost iter is received as an argument */
1715 if (!symtable_implicit_arg(st, 0)) {
1716 symtable_exit_block(st, (void *)e);
1717 return 0;
1718 }
1719 /* Allocate temporary name if needed */
1720 if (needs_tmp && !symtable_new_tmpname(st)) {
1721 symtable_exit_block(st, (void *)e);
1722 return 0;
1723 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001724 VISIT(st, expr, outermost->target);
1725 VISIT_SEQ(st, expr, outermost->ifs);
1726 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001728 VISIT(st, expr, value);
1729 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001734symtable_visit_genexp(struct symtable *st, expr_ty e)
1735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1737 e->v.GeneratorExp.generators,
1738 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001739}
1740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001742symtable_visit_listcomp(struct symtable *st, expr_ty e)
1743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1745 e->v.ListComp.generators,
1746 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001747}
1748
1749static int
1750symtable_visit_setcomp(struct symtable *st, expr_ty e)
1751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1753 e->v.SetComp.generators,
1754 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001755}
1756
1757static int
1758symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1761 e->v.DictComp.generators,
1762 e->v.DictComp.key,
1763 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001764}