blob: 806364ad07bc77089a7bfdb97a8234ba7dda58c6 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
3#include "code.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005#include "structmember.h"
6
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#define GLOBAL_AFTER_ASSIGN \
9"name '%.400s' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000010
Jeremy Hylton81e95022007-02-27 06:50:52 +000011#define NONLOCAL_AFTER_ASSIGN \
12"name '%.400s' is assigned to before nonlocal declaration"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_USE \
15"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_USE \
18"name '%.400s' is used prior to nonlocal declaration"
19
Neal Norwitz5d0ad502005-12-19 04:27:42 +000020#define IMPORT_STAR_WARNING "import * only allowed at module level"
21
Neal Norwitz090b3dd2006-02-28 22:36:46 +000022static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000023ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000024 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020027 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 k = PyLong_FromVoidPtr(key);
30 if (k == NULL)
31 goto fail;
32 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020033 if (ste == NULL) {
34 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020036 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020038 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020041 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 ste->ste_symbols = NULL;
44 ste->ste_varnames = NULL;
45 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000046
Benjamin Petersond9c87022012-10-31 20:26:20 -040047 ste->ste_directives = NULL;
48
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 ste->ste_nested = 0;
51 ste->ste_free = 0;
52 ste->ste_varargs = 0;
53 ste->ste_varkeywords = 0;
54 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000055 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000056 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000058 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 if (st->st_cur != NULL &&
61 (st->st_cur->ste_nested ||
62 st->st_cur->ste_type == FunctionBlock))
63 ste->ste_nested = 1;
64 ste->ste_child_free = 0;
65 ste->ste_generator = 0;
66 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050067 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000068
Victor Stinner9a4fb662013-07-11 22:49:00 +020069 ste->ste_symbols = PyDict_New();
70 ste->ste_varnames = PyList_New(0);
71 ste->ste_children = PyList_New(0);
72 if (ste->ste_symbols == NULL
73 || ste->ste_varnames == NULL
74 || ste->ste_children == NULL)
75 goto fail;
76
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
78 goto fail;
79
80 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000081 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 Py_XDECREF(ste);
83 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084}
85
86static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
90 ste->ste_name,
91 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092}
93
94static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 ste->ste_table = NULL;
98 Py_XDECREF(ste->ste_id);
99 Py_XDECREF(ste->ste_name);
100 Py_XDECREF(ste->ste_symbols);
101 Py_XDECREF(ste->ste_varnames);
102 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400103 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000105}
106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108
Guido van Rossum6f799372001-09-20 20:46:19 +0000109static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 {"id", T_OBJECT, OFF(ste_id), READONLY},
111 {"name", T_OBJECT, OFF(ste_name), READONLY},
112 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
113 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
114 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 {"nested", T_INT, OFF(ste_nested), READONLY},
116 {"type", T_INT, OFF(ste_type), READONLY},
117 {"lineno", T_INT, OFF(ste_lineno), READONLY},
118 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119};
120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 PyVarObject_HEAD_INIT(&PyType_Type, 0)
123 "symtable entry",
124 sizeof(PySTEntryObject),
125 0,
126 (destructor)ste_dealloc, /* tp_dealloc */
127 0, /* tp_print */
128 0, /* tp_getattr */
129 0, /* tp_setattr */
130 0, /* tp_reserved */
131 (reprfunc)ste_repr, /* tp_repr */
132 0, /* tp_as_number */
133 0, /* tp_as_sequence */
134 0, /* tp_as_mapping */
135 0, /* tp_hash */
136 0, /* tp_call */
137 0, /* tp_str */
138 PyObject_GenericGetAttr, /* tp_getattro */
139 0, /* tp_setattro */
140 0, /* tp_as_buffer */
141 Py_TPFLAGS_DEFAULT, /* tp_flags */
142 0, /* tp_doc */
143 0, /* tp_traverse */
144 0, /* tp_clear */
145 0, /* tp_richcompare */
146 0, /* tp_weaklistoffset */
147 0, /* tp_iter */
148 0, /* tp_iternext */
149 0, /* tp_methods */
150 ste_memberlist, /* tp_members */
151 0, /* tp_getset */
152 0, /* tp_base */
153 0, /* tp_dict */
154 0, /* tp_descr_get */
155 0, /* tp_descr_set */
156 0, /* tp_dictoffset */
157 0, /* tp_init */
158 0, /* tp_alloc */
159 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000160};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161
162static int symtable_analyze(struct symtable *st);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200163static int symtable_warn(struct symtable *st, const char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000165 _Py_block_ty block, void *ast, int lineno,
166 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_exit_block(struct symtable *st, void *ast);
168static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
169static int symtable_visit_expr(struct symtable *st, expr_ty s);
170static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000171static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
172static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000173static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static int symtable_visit_arguments(struct symtable *st, arguments_ty);
175static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
176static int symtable_visit_alias(struct symtable *st, alias_ty);
177static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
178static int symtable_visit_keyword(struct symtable *st, keyword_ty);
179static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000180static int symtable_visit_params(struct symtable *st, asdl_seq *args);
181static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182static int symtable_implicit_arg(struct symtable *st, int pos);
Yury Selivanov75445082015-05-11 22:57:16 -0400183static int symtable_visit_annotations(struct symtable *st, stmt_ty s, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500184static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185
186
Nick Coghlan650f0d02007-04-15 12:05:43 +0000187static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500189 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
194#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000195"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
197static struct symtable *
198symtable_new(void)
199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
203 if (st == NULL)
204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 st->st_filename = NULL;
207 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if ((st->st_stack = PyList_New(0)) == NULL)
210 goto fail;
211 if ((st->st_blocks = PyDict_New()) == NULL)
212 goto fail;
213 st->st_cur = NULL;
214 st->st_private = NULL;
215 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 PySymtable_Free(st);
218 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219}
220
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000221/* When compiling the use of C stack is probably going to be a lot
222 lighter than when executing Python code but still can overflow
223 and causing a Python crash if not checked (e.g. eval("()"*300000)).
224 Using the current recursion limit for the compiler seems too
225 restrictive (it caused at least one test to fail) so a factor is
226 used to allow deeper recursion when compiling an expression.
227
228 Using a scaling factor means this should automatically adjust when
229 the recursion limit is adjusted for small or large C stack allocations.
230*/
231#define COMPILER_STACK_FRAME_SCALE 3
232
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200234PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000236 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 asdl_seq *seq;
238 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000239 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400240 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200243 return NULL;
244 if (filename == NULL) {
245 PySymtable_Free(st);
246 return NULL;
247 }
248 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 st->st_filename = filename;
250 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000251
252 /* Setup recursion depth check counters */
253 tstate = PyThreadState_GET();
254 if (!tstate) {
255 PySymtable_Free(st);
256 return NULL;
257 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400258 /* Be careful here to prevent overflow. */
259 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
260 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
261 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
262 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 /* Make the initial symbol information gathering pass */
265 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000266 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 PySymtable_Free(st);
268 return NULL;
269 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 switch (mod->kind) {
273 case Module_kind:
274 seq = mod->v.Module.body;
275 for (i = 0; i < asdl_seq_LEN(seq); i++)
276 if (!symtable_visit_stmt(st,
277 (stmt_ty)asdl_seq_GET(seq, i)))
278 goto error;
279 break;
280 case Expression_kind:
281 if (!symtable_visit_expr(st, mod->v.Expression.body))
282 goto error;
283 break;
284 case Interactive_kind:
285 seq = mod->v.Interactive.body;
286 for (i = 0; i < asdl_seq_LEN(seq); i++)
287 if (!symtable_visit_stmt(st,
288 (stmt_ty)asdl_seq_GET(seq, i)))
289 goto error;
290 break;
291 case Suite_kind:
292 PyErr_SetString(PyExc_RuntimeError,
293 "this compiler does not handle Suites");
294 goto error;
295 }
296 if (!symtable_exit_block(st, (void *)mod)) {
297 PySymtable_Free(st);
298 return NULL;
299 }
300 /* Make the second symbol analysis pass */
301 if (symtable_analyze(st))
302 return st;
303 PySymtable_Free(st);
304 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 (void) symtable_exit_block(st, (void *)mod);
307 PySymtable_Free(st);
308 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309}
310
Victor Stinner14e461d2013-08-26 22:28:21 +0200311struct symtable *
312PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
313{
314 PyObject *filename;
315 struct symtable *st;
316 filename = PyUnicode_DecodeFSDefault(filename_str);
317 if (filename == NULL)
318 return NULL;
319 st = PySymtable_BuildObject(mod, filename, future);
320 Py_DECREF(filename);
321 return st;
322}
323
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324void
325PySymtable_Free(struct symtable *st)
326{
Victor Stinner14e461d2013-08-26 22:28:21 +0200327 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 Py_XDECREF(st->st_blocks);
329 Py_XDECREF(st->st_stack);
330 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000331}
332
333PySTEntryObject *
334PySymtable_Lookup(struct symtable *st, void *key)
335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 k = PyLong_FromVoidPtr(key);
339 if (k == NULL)
340 return NULL;
341 v = PyDict_GetItem(st->st_blocks, k);
342 if (v) {
343 assert(PySTEntry_Check(v));
344 Py_INCREF(v);
345 }
346 else {
347 PyErr_SetString(PyExc_KeyError,
348 "unknown symbol table entry");
349 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 Py_DECREF(k);
352 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353}
354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356PyST_GetScope(PySTEntryObject *ste, PyObject *name)
357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
359 if (!v)
360 return 0;
361 assert(PyLong_Check(v));
362 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363}
364
Benjamin Petersond9c87022012-10-31 20:26:20 -0400365static int
366error_at_directive(PySTEntryObject *ste, PyObject *name)
367{
368 Py_ssize_t i;
369 PyObject *data;
370 assert(ste->ste_directives);
371 for (i = 0; ; i++) {
372 data = PyList_GET_ITEM(ste->ste_directives, i);
373 assert(PyTuple_CheckExact(data));
374 if (PyTuple_GET_ITEM(data, 0) == name)
375 break;
376 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200377 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
378 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
379 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
Benjamin Petersond9c87022012-10-31 20:26:20 -0400380 return 0;
381}
382
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383
384/* Analyze raw symbol information to determine scope of each name.
385
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000386 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000392 explicit global is declared with the global statement. An implicit
393 global is a free variable for which the compiler has found no binding
394 in an enclosing function scope. The implicit global is either a global
395 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
396 to handle these names to implement slightly odd semantics. In such a
397 block, the name is treated as global until it is assigned to; then it
398 is treated as a local.
399
400 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000401 The first pass collects raw facts from the AST via the symtable_visit_*
402 functions: the name is a parameter here, the name is used but not defined
403 here, etc. The second pass analyzes these facts during a pass over the
404 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405
406 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000408 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000409 Names which are explicitly declared nonlocal must exist in this set of
410 visible names - if they do not, a syntax error is raised. After doing
411 the local analysis, it analyzes each of its child blocks using an
412 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000413
Nick Coghlan650f0d02007-04-15 12:05:43 +0000414 The children update the free variable set. If a local variable is added to
415 the free variable set by the child, the variable is marked as a cell. The
416 function object being defined must provide runtime storage for the variable
417 that may outlive the function's frame. Cell variables are removed from the
418 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000419
Nick Coghlan650f0d02007-04-15 12:05:43 +0000420 During analysis, the names are:
421 symbols: dict mapping from symbol names to flag values (including offset scope values)
422 scopes: dict mapping from symbol names to scope values (no offset)
423 local: set of all symbol names local to the current scope
424 bound: set of all symbol names local to a containing function scope
425 free: set of all symbol names referenced but not bound in child scopes
426 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427*/
428
429#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 PyObject *o = PyLong_FromLong(I); \
431 if (!o) \
432 return 0; \
433 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
434 Py_DECREF(o); \
435 return 0; \
436 } \
437 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000438}
439
440/* Decide on scope of name, given flags.
441
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000442 The namespace dictionaries may be modified to record information
443 about the new name. For example, a new global will add an entry to
444 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445*/
446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000448analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 PyObject *bound, PyObject *local, PyObject *free,
450 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 if (flags & DEF_GLOBAL) {
453 if (flags & DEF_PARAM) {
454 PyErr_Format(PyExc_SyntaxError,
455 "name '%U' is parameter and global",
456 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400457 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000458 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (flags & DEF_NONLOCAL) {
460 PyErr_Format(PyExc_SyntaxError,
461 "name '%U' is nonlocal and global",
462 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400463 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 }
465 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
466 if (PySet_Add(global, name) < 0)
467 return 0;
468 if (bound && (PySet_Discard(bound, name) < 0))
469 return 0;
470 return 1;
471 }
472 if (flags & DEF_NONLOCAL) {
473 if (flags & DEF_PARAM) {
474 PyErr_Format(PyExc_SyntaxError,
475 "name '%U' is parameter and nonlocal",
476 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400477 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 }
479 if (!bound) {
480 PyErr_Format(PyExc_SyntaxError,
481 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400482 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 }
484 if (!PySet_Contains(bound, name)) {
485 PyErr_Format(PyExc_SyntaxError,
486 "no binding for nonlocal '%U' found",
487 name);
488
Benjamin Petersond9c87022012-10-31 20:26:20 -0400489 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 }
491 SET_SCOPE(scopes, name, FREE);
492 ste->ste_free = 1;
493 return PySet_Add(free, name) >= 0;
494 }
495 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000496 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (PySet_Add(local, name) < 0)
498 return 0;
499 if (PySet_Discard(global, name) < 0)
500 return 0;
501 return 1;
502 }
503 /* If an enclosing block has a binding for this name, it
504 is a free variable rather than a global variable.
505 Note that having a non-NULL bound implies that the block
506 is nested.
507 */
508 if (bound && PySet_Contains(bound, name)) {
509 SET_SCOPE(scopes, name, FREE);
510 ste->ste_free = 1;
511 return PySet_Add(free, name) >= 0;
512 }
513 /* If a parent has a global statement, then call it global
514 explicit? It could also be global implicit.
515 */
516 if (global && PySet_Contains(global, name)) {
517 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
518 return 1;
519 }
520 if (ste->ste_nested)
521 ste->ste_free = 1;
522 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
523 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524}
525
526#undef SET_SCOPE
527
528/* If a name is defined in free and also in locals, then this block
529 provides the binding for the free variable. The name should be
530 marked CELL in this block and removed from the free list.
531
532 Note that the current block's free variables are included in free.
533 That's safe because no name can be free and local in the same scope.
534*/
535
536static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500537analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 PyObject *name, *v, *v_cell;
540 int success = 0;
541 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 v_cell = PyLong_FromLong(CELL);
544 if (!v_cell)
545 return 0;
546 while (PyDict_Next(scopes, &pos, &name, &v)) {
547 long scope;
548 assert(PyLong_Check(v));
549 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000550 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 continue;
552 if (!PySet_Contains(free, name))
553 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /* Replace LOCAL with CELL for this name, and remove
555 from free. It is safe to replace the value of name
556 in the dict, because it will not cause a resize.
557 */
558 if (PyDict_SetItem(scopes, name, v_cell) < 0)
559 goto error;
560 if (PySet_Discard(free, name) < 0)
561 goto error;
562 }
563 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 Py_DECREF(v_cell);
566 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567}
568
Benjamin Peterson312595c2013-05-15 15:26:42 -0500569static int
570drop_class_free(PySTEntryObject *ste, PyObject *free)
571{
572 int res;
573 if (!GET_IDENTIFIER(__class__))
574 return 0;
575 res = PySet_Discard(free, __class__);
576 if (res < 0)
577 return 0;
578 if (res)
579 ste->ste_needs_class_closure = 1;
580 return 1;
581}
582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583/* Enter the final scope information into the ste_symbols dict.
584 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585 * All arguments are dicts. Modifies symbols, others are read-only.
586*/
587static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000589 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 PyObject *name = NULL, *itr = NULL;
592 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
593 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 /* Update scope information for all symbols in this scope */
596 while (PyDict_Next(symbols, &pos, &name, &v)) {
597 long scope, flags;
598 assert(PyLong_Check(v));
599 flags = PyLong_AS_LONG(v);
600 v_scope = PyDict_GetItem(scopes, name);
601 assert(v_scope && PyLong_Check(v_scope));
602 scope = PyLong_AS_LONG(v_scope);
603 flags |= (scope << SCOPE_OFFSET);
604 v_new = PyLong_FromLong(flags);
605 if (!v_new)
606 return 0;
607 if (PyDict_SetItem(symbols, name, v_new) < 0) {
608 Py_DECREF(v_new);
609 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 Py_DECREF(v_new);
612 }
613
614 /* Record not yet resolved free variables from children (if any) */
615 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
616 if (!v_free)
617 return 0;
618
619 itr = PyObject_GetIter(free);
620 if (!itr)
621 goto error;
622
623 while ((name = PyIter_Next(itr))) {
624 v = PyDict_GetItem(symbols, name);
625
626 /* Handle symbol that already exists in this scope */
627 if (v) {
628 /* Handle a free variable in a method of
629 the class that has the same name as a local
630 or global in the class scope.
631 */
632 if (classflag &&
633 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
634 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
635 v_new = PyLong_FromLong(flags);
636 if (!v_new) {
637 goto error;
638 }
639 if (PyDict_SetItem(symbols, name, v_new) < 0) {
640 Py_DECREF(v_new);
641 goto error;
642 }
643 Py_DECREF(v_new);
644 }
645 /* It's a cell, or already free in this scope */
646 Py_DECREF(name);
647 continue;
648 }
649 /* Handle global symbol */
650 if (!PySet_Contains(bound, name)) {
651 Py_DECREF(name);
652 continue; /* it's a global */
653 }
654 /* Propagate new free symbol up the lexical stack */
655 if (PyDict_SetItem(symbols, name, v_free) < 0) {
656 goto error;
657 }
658 Py_DECREF(name);
659 }
660 Py_DECREF(itr);
661 Py_DECREF(v_free);
662 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000663error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 Py_XDECREF(v_free);
665 Py_XDECREF(itr);
666 Py_XDECREF(name);
667 return 0;
668}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669
670/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000671
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672 Arguments:
673 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000674 bound -- set of variables bound in enclosing scopes (input). bound
675 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 free -- set of free variables in enclosed scopes (output)
677 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000678
679 The implementation uses two mutually recursive functions,
680 analyze_block() and analyze_child_block(). analyze_block() is
681 responsible for analyzing the individual names defined in a block.
682 analyze_child_block() prepares temporary namespace dictionaries
683 used to evaluated nested blocks.
684
685 The two functions exist because a child block should see the name
686 bindings of its enclosing blocks, but those bindings should not
687 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688*/
689
690static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
692 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000693
694static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
696 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
699 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
700 PyObject *temp;
701 int i, success = 0;
702 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 local = PySet_New(NULL); /* collect new names bound in block */
705 if (!local)
706 goto error;
707 scopes = PyDict_New(); /* collect scopes defined for each name */
708 if (!scopes)
709 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 /* Allocate new global and bound variable dictionaries. These
712 dictionaries hold the names visible in nested blocks. For
713 ClassBlocks, the bound and global names are initialized
714 before analyzing names, because class bindings aren't
715 visible in methods. For other blocks, they are initialized
716 after names are analyzed.
717 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 /* TODO(jhylton): Package these dicts in a struct so that we
720 can write reasonable helper functions?
721 */
722 newglobal = PySet_New(NULL);
723 if (!newglobal)
724 goto error;
725 newfree = PySet_New(NULL);
726 if (!newfree)
727 goto error;
728 newbound = PySet_New(NULL);
729 if (!newbound)
730 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 /* Class namespace has no effect on names visible in
733 nested functions, so populate the global and bound
734 sets to be passed to child blocks before analyzing
735 this one.
736 */
737 if (ste->ste_type == ClassBlock) {
738 /* Pass down known globals */
739 temp = PyNumber_InPlaceOr(newglobal, global);
740 if (!temp)
741 goto error;
742 Py_DECREF(temp);
743 /* Pass down previously bound symbols */
744 if (bound) {
745 temp = PyNumber_InPlaceOr(newbound, bound);
746 if (!temp)
747 goto error;
748 Py_DECREF(temp);
749 }
750 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
753 long flags = PyLong_AS_LONG(v);
754 if (!analyze_name(ste, scopes, name, flags,
755 bound, local, free, global))
756 goto error;
757 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 /* Populate global and bound sets to be passed to children. */
760 if (ste->ste_type != ClassBlock) {
761 /* Add function locals to bound set */
762 if (ste->ste_type == FunctionBlock) {
763 temp = PyNumber_InPlaceOr(newbound, local);
764 if (!temp)
765 goto error;
766 Py_DECREF(temp);
767 }
768 /* Pass down previously bound symbols */
769 if (bound) {
770 temp = PyNumber_InPlaceOr(newbound, bound);
771 if (!temp)
772 goto error;
773 Py_DECREF(temp);
774 }
775 /* Pass down known globals */
776 temp = PyNumber_InPlaceOr(newglobal, global);
777 if (!temp)
778 goto error;
779 Py_DECREF(temp);
780 }
781 else {
782 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000783 if (!GET_IDENTIFIER(__class__))
784 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 if (PySet_Add(newbound, __class__) < 0)
786 goto error;
787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300789 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 newbound, newglobal now contain the names visible in
792 nested blocks. The free variables in the children will
793 be collected in allfree.
794 */
795 allfree = PySet_New(NULL);
796 if (!allfree)
797 goto error;
798 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
799 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
800 PySTEntryObject* entry;
801 assert(c && PySTEntry_Check(c));
802 entry = (PySTEntryObject*)c;
803 if (!analyze_child_block(entry, newbound, newfree, newglobal,
804 allfree))
805 goto error;
806 /* Check if any children have free variables */
807 if (entry->ste_free || entry->ste_child_free)
808 ste->ste_child_free = 1;
809 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 temp = PyNumber_InPlaceOr(newfree, allfree);
812 if (!temp)
813 goto error;
814 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500817 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500819 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 goto error;
821 /* Records the results of the analysis in the symbol table entry */
822 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
823 ste->ste_type == ClassBlock))
824 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 temp = PyNumber_InPlaceOr(free, newfree);
827 if (!temp)
828 goto error;
829 Py_DECREF(temp);
830 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 Py_XDECREF(scopes);
833 Py_XDECREF(local);
834 Py_XDECREF(newbound);
835 Py_XDECREF(newglobal);
836 Py_XDECREF(newfree);
837 Py_XDECREF(allfree);
838 if (!success)
839 assert(PyErr_Occurred());
840 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841}
842
843static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
845 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
848 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 These dictionary are used by all blocks enclosed by the
853 current block. The analyze_block() call modifies these
854 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 */
857 temp_bound = PySet_New(bound);
858 if (!temp_bound)
859 goto error;
860 temp_free = PySet_New(free);
861 if (!temp_free)
862 goto error;
863 temp_global = PySet_New(global);
864 if (!temp_global)
865 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
868 goto error;
869 temp = PyNumber_InPlaceOr(child_free, temp_free);
870 if (!temp)
871 goto error;
872 Py_DECREF(temp);
873 Py_DECREF(temp_bound);
874 Py_DECREF(temp_free);
875 Py_DECREF(temp_global);
876 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000877 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 Py_XDECREF(temp_bound);
879 Py_XDECREF(temp_free);
880 Py_XDECREF(temp_global);
881 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000882}
883
884static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885symtable_analyze(struct symtable *st)
886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 PyObject *free, *global;
888 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 free = PySet_New(NULL);
891 if (!free)
892 return 0;
893 global = PySet_New(NULL);
894 if (!global) {
895 Py_DECREF(free);
896 return 0;
897 }
898 r = analyze_block(st->st_top, NULL, free, global);
899 Py_DECREF(free);
900 Py_DECREF(global);
901 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902}
903
904
905static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200906symtable_warn(struct symtable *st, const char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907{
Victor Stinner14e461d2013-08-26 22:28:21 +0200908 PyObject *message = PyUnicode_FromString(msg);
909 if (message == NULL)
910 return 0;
911 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, message, st->st_filename,
912 lineno, NULL, NULL) < 0) {
913 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
915 PyErr_SetString(PyExc_SyntaxError, msg);
Victor Stinner14e461d2013-08-26 22:28:21 +0200916 PyErr_SyntaxLocationObject(st->st_filename, st->st_cur->ste_lineno,
917 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 }
919 return 0;
920 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200921 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923}
924
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000925/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 This reference is released when the block is exited, via the DECREF
927 in symtable_exit_block().
928*/
929
930static int
931symtable_exit_block(struct symtable *st, void *ast)
932{
Benjamin Peterson609da582011-06-29 22:52:39 -0500933 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934
Benjamin Peterson609da582011-06-29 22:52:39 -0500935 st->st_cur = NULL;
936 size = PyList_GET_SIZE(st->st_stack);
937 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500938 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500940 if (--size)
941 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 }
943 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944}
945
946static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000948 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949{
Benjamin Peterson609da582011-06-29 22:52:39 -0500950 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951
Benjamin Peterson609da582011-06-29 22:52:39 -0500952 ste = ste_new(st, name, block, ast, lineno, col_offset);
953 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500955 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
956 Py_DECREF(ste);
957 return 0;
958 }
959 prev = st->st_cur;
960 /* The entry is owned by the stack. Borrow it for st_cur. */
961 Py_DECREF(ste);
962 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000963 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 st->st_global = st->st_cur->ste_symbols;
965 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500966 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 return 0;
968 }
969 }
970 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971}
972
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000973static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974symtable_lookup(struct symtable *st, PyObject *name)
975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 PyObject *o;
977 PyObject *mangled = _Py_Mangle(st->st_private, name);
978 if (!mangled)
979 return 0;
980 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
981 Py_DECREF(mangled);
982 if (!o)
983 return 0;
984 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985}
986
987static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 PyObject *o;
991 PyObject *dict;
992 long val;
993 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994
Jeremy Hylton81e95022007-02-27 06:50:52 +0000995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 if (!mangled)
997 return 0;
998 dict = st->st_cur->ste_symbols;
999 if ((o = PyDict_GetItem(dict, mangled))) {
1000 val = PyLong_AS_LONG(o);
1001 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1002 /* Is it better to use 'mangled' or 'name' here? */
1003 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001004 PyErr_SyntaxLocationObject(st->st_filename,
1005 st->st_cur->ste_lineno,
1006 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 goto error;
1008 }
1009 val |= flag;
1010 } else
1011 val = flag;
1012 o = PyLong_FromLong(val);
1013 if (o == NULL)
1014 goto error;
1015 if (PyDict_SetItem(dict, mangled, o) < 0) {
1016 Py_DECREF(o);
1017 goto error;
1018 }
1019 Py_DECREF(o);
1020
1021 if (flag & DEF_PARAM) {
1022 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1023 goto error;
1024 } else if (flag & DEF_GLOBAL) {
1025 /* XXX need to update DEF_GLOBAL for other flags too;
1026 perhaps only DEF_FREE_GLOBAL */
1027 val = flag;
1028 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1029 val |= PyLong_AS_LONG(o);
1030 }
1031 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 goto error;
1034 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1035 Py_DECREF(o);
1036 goto error;
1037 }
1038 Py_DECREF(o);
1039 }
1040 Py_DECREF(mangled);
1041 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001042
1043error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 Py_DECREF(mangled);
1045 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046}
1047
1048/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1049 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 function.
1051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1053 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001054
1055 VISIT_QUIT macro returns the specified value exiting from the function but
1056 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057*/
1058
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001059#define VISIT_QUIT(ST, X) \
1060 return --(ST)->recursion_depth,(X)
1061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001064 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 int i; \
1068 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1069 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1070 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1071 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001072 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001075
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 int i; \
1078 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1079 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1080 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1081 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001082 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001085
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001086#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001088 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001090 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001092 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001093 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001095}
1096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001098symtable_new_tmpname(struct symtable *st)
1099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 char tmpname[256];
1101 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1104 ++st->st_cur->ste_tmpname);
1105 tmp = PyUnicode_InternFromString(tmpname);
1106 if (!tmp)
1107 return 0;
1108 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1109 return 0;
1110 Py_DECREF(tmp);
1111 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001112}
1113
Guido van Rossum4f72a782006-10-27 23:31:49 +00001114
Guido van Rossumc2e20742006-02-27 22:32:47 +00001115static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001116symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1117{
1118 PyObject *data;
1119 int res;
1120 if (!st->st_cur->ste_directives) {
1121 st->st_cur->ste_directives = PyList_New(0);
1122 if (!st->st_cur->ste_directives)
1123 return 0;
1124 }
1125 data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset);
1126 if (!data)
1127 return 0;
1128 res = PyList_Append(st->st_cur->ste_directives, data);
1129 Py_DECREF(data);
1130 return res == 0;
1131}
1132
1133
1134static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135symtable_visit_stmt(struct symtable *st, stmt_ty s)
1136{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001137 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001138 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001139 "maximum recursion depth exceeded during compilation");
1140 VISIT_QUIT(st, 0);
1141 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 switch (s->kind) {
1143 case FunctionDef_kind:
1144 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001145 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (s->v.FunctionDef.args->defaults)
1147 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1148 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001149 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001150 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1151 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001152 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (s->v.FunctionDef.decorator_list)
1154 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1155 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001156 FunctionBlock, (void *)s, s->lineno,
1157 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001158 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001159 VISIT(st, arguments, s->v.FunctionDef.args);
1160 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001162 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 break;
1164 case ClassDef_kind: {
1165 PyObject *tmp;
1166 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001167 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1169 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 if (s->v.ClassDef.decorator_list)
1171 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1172 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001173 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001174 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 tmp = st->st_private;
1176 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001177 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 st->st_private = tmp;
1179 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001180 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 break;
1182 }
1183 case Return_kind:
1184 if (s->v.Return.value) {
1185 VISIT(st, expr, s->v.Return.value);
1186 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 }
1188 break;
1189 case Delete_kind:
1190 VISIT_SEQ(st, expr, s->v.Delete.targets);
1191 break;
1192 case Assign_kind:
1193 VISIT_SEQ(st, expr, s->v.Assign.targets);
1194 VISIT(st, expr, s->v.Assign.value);
1195 break;
1196 case AugAssign_kind:
1197 VISIT(st, expr, s->v.AugAssign.target);
1198 VISIT(st, expr, s->v.AugAssign.value);
1199 break;
1200 case For_kind:
1201 VISIT(st, expr, s->v.For.target);
1202 VISIT(st, expr, s->v.For.iter);
1203 VISIT_SEQ(st, stmt, s->v.For.body);
1204 if (s->v.For.orelse)
1205 VISIT_SEQ(st, stmt, s->v.For.orelse);
1206 break;
1207 case While_kind:
1208 VISIT(st, expr, s->v.While.test);
1209 VISIT_SEQ(st, stmt, s->v.While.body);
1210 if (s->v.While.orelse)
1211 VISIT_SEQ(st, stmt, s->v.While.orelse);
1212 break;
1213 case If_kind:
1214 /* XXX if 0: and lookup_yield() hacks */
1215 VISIT(st, expr, s->v.If.test);
1216 VISIT_SEQ(st, stmt, s->v.If.body);
1217 if (s->v.If.orelse)
1218 VISIT_SEQ(st, stmt, s->v.If.orelse);
1219 break;
1220 case Raise_kind:
1221 if (s->v.Raise.exc) {
1222 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001223 if (s->v.Raise.cause) {
1224 VISIT(st, expr, s->v.Raise.cause);
1225 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 }
1227 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001228 case Try_kind:
1229 VISIT_SEQ(st, stmt, s->v.Try.body);
1230 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1231 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1232 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 break;
1234 case Assert_kind:
1235 VISIT(st, expr, s->v.Assert.test);
1236 if (s->v.Assert.msg)
1237 VISIT(st, expr, s->v.Assert.msg);
1238 break;
1239 case Import_kind:
1240 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 break;
1242 case ImportFrom_kind:
1243 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 break;
1245 case Global_kind: {
1246 int i;
1247 asdl_seq *seq = s->v.Global.names;
1248 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1249 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 long cur = symtable_lookup(st, name);
1251 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001252 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (cur & (DEF_LOCAL | USE)) {
1254 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001255 char *c_name = _PyUnicode_AsString(name);
1256 if (!c_name)
1257 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (cur & DEF_LOCAL)
1259 PyOS_snprintf(buf, sizeof(buf),
1260 GLOBAL_AFTER_ASSIGN,
1261 c_name);
1262 else
1263 PyOS_snprintf(buf, sizeof(buf),
1264 GLOBAL_AFTER_USE,
1265 c_name);
1266 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001267 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 }
1269 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001270 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001271 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001272 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 }
1274 break;
1275 }
1276 case Nonlocal_kind: {
1277 int i;
1278 asdl_seq *seq = s->v.Nonlocal.names;
1279 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1280 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 long cur = symtable_lookup(st, name);
1282 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001283 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 if (cur & (DEF_LOCAL | USE)) {
1285 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001286 char *c_name = _PyUnicode_AsString(name);
1287 if (!c_name)
1288 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 if (cur & DEF_LOCAL)
1290 PyOS_snprintf(buf, sizeof(buf),
1291 NONLOCAL_AFTER_ASSIGN,
1292 c_name);
1293 else
1294 PyOS_snprintf(buf, sizeof(buf),
1295 NONLOCAL_AFTER_USE,
1296 c_name);
1297 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001298 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 }
1300 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001301 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001302 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001303 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 }
1305 break;
1306 }
1307 case Expr_kind:
1308 VISIT(st, expr, s->v.Expr.value);
1309 break;
1310 case Pass_kind:
1311 case Break_kind:
1312 case Continue_kind:
1313 /* nothing to do here */
1314 break;
1315 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001316 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 VISIT_SEQ(st, stmt, s->v.With.body);
1318 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001319 case AsyncFunctionDef_kind:
1320 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1321 VISIT_QUIT(st, 0);
1322 if (s->v.AsyncFunctionDef.args->defaults)
1323 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1324 if (s->v.AsyncFunctionDef.args->kw_defaults)
1325 VISIT_SEQ_WITH_NULL(st, expr,
1326 s->v.AsyncFunctionDef.args->kw_defaults);
1327 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1328 s->v.AsyncFunctionDef.returns))
1329 VISIT_QUIT(st, 0);
1330 if (s->v.AsyncFunctionDef.decorator_list)
1331 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1332 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1333 FunctionBlock, (void *)s, s->lineno,
1334 s->col_offset))
1335 VISIT_QUIT(st, 0);
1336 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1337 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1338 if (!symtable_exit_block(st, s))
1339 VISIT_QUIT(st, 0);
1340 break;
1341 case AsyncWith_kind:
1342 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1343 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1344 break;
1345 case AsyncFor_kind:
1346 VISIT(st, expr, s->v.AsyncFor.target);
1347 VISIT(st, expr, s->v.AsyncFor.iter);
1348 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1349 if (s->v.AsyncFor.orelse)
1350 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1351 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001353 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354}
1355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357symtable_visit_expr(struct symtable *st, expr_ty e)
1358{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001359 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001360 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001361 "maximum recursion depth exceeded during compilation");
1362 VISIT_QUIT(st, 0);
1363 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 switch (e->kind) {
1365 case BoolOp_kind:
1366 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1367 break;
1368 case BinOp_kind:
1369 VISIT(st, expr, e->v.BinOp.left);
1370 VISIT(st, expr, e->v.BinOp.right);
1371 break;
1372 case UnaryOp_kind:
1373 VISIT(st, expr, e->v.UnaryOp.operand);
1374 break;
1375 case Lambda_kind: {
1376 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001377 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 if (e->v.Lambda.args->defaults)
1379 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001380 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001381 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001383 FunctionBlock, (void *)e, e->lineno,
1384 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001385 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001386 VISIT(st, arguments, e->v.Lambda.args);
1387 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001389 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 break;
1391 }
1392 case IfExp_kind:
1393 VISIT(st, expr, e->v.IfExp.test);
1394 VISIT(st, expr, e->v.IfExp.body);
1395 VISIT(st, expr, e->v.IfExp.orelse);
1396 break;
1397 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001398 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 VISIT_SEQ(st, expr, e->v.Dict.values);
1400 break;
1401 case Set_kind:
1402 VISIT_SEQ(st, expr, e->v.Set.elts);
1403 break;
1404 case GeneratorExp_kind:
1405 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001406 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 break;
1408 case ListComp_kind:
1409 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001410 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 break;
1412 case SetComp_kind:
1413 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001414 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 break;
1416 case DictComp_kind:
1417 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001418 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 break;
1420 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001421 if (e->v.Yield.value)
1422 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001425 case YieldFrom_kind:
1426 VISIT(st, expr, e->v.YieldFrom.value);
1427 st->st_cur->ste_generator = 1;
1428 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001429 case Await_kind:
1430 VISIT(st, expr, e->v.Await.value);
1431 st->st_cur->ste_generator = 1;
1432 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 case Compare_kind:
1434 VISIT(st, expr, e->v.Compare.left);
1435 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1436 break;
1437 case Call_kind:
1438 VISIT(st, expr, e->v.Call.func);
1439 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001440 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001442 case FormattedValue_kind:
1443 VISIT(st, expr, e->v.FormattedValue.value);
1444 if (e->v.FormattedValue.format_spec)
1445 VISIT(st, expr, e->v.FormattedValue.format_spec);
1446 break;
1447 case JoinedStr_kind:
1448 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1449 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 case Num_kind:
1451 case Str_kind:
1452 case Bytes_kind:
1453 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001454 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 /* Nothing to do here. */
1456 break;
1457 /* The following exprs can be assignment targets. */
1458 case Attribute_kind:
1459 VISIT(st, expr, e->v.Attribute.value);
1460 break;
1461 case Subscript_kind:
1462 VISIT(st, expr, e->v.Subscript.value);
1463 VISIT(st, slice, e->v.Subscript.slice);
1464 break;
1465 case Starred_kind:
1466 VISIT(st, expr, e->v.Starred.value);
1467 break;
1468 case Name_kind:
1469 if (!symtable_add_def(st, e->v.Name.id,
1470 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001471 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 /* Special-case super: it counts as a use of __class__ */
1473 if (e->v.Name.ctx == Load &&
1474 st->st_cur->ste_type == FunctionBlock &&
1475 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001476 if (!GET_IDENTIFIER(__class__) ||
1477 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001478 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 }
1480 break;
1481 /* child nodes of List and Tuple will have expr_context set */
1482 case List_kind:
1483 VISIT_SEQ(st, expr, e->v.List.elts);
1484 break;
1485 case Tuple_kind:
1486 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1487 break;
1488 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001489 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490}
1491
1492static int
1493symtable_implicit_arg(struct symtable *st, int pos)
1494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1496 if (id == NULL)
1497 return 0;
1498 if (!symtable_add_def(st, id, DEF_PARAM)) {
1499 Py_DECREF(id);
1500 return 0;
1501 }
1502 Py_DECREF(id);
1503 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504}
1505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001507symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 if (!args)
1512 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 for (i = 0; i < asdl_seq_LEN(args); i++) {
1515 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1516 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1517 return 0;
1518 }
1519
1520 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521}
1522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001524symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 if (!args)
1529 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 for (i = 0; i < asdl_seq_LEN(args); i++) {
1532 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1533 if (arg->annotation)
1534 VISIT(st, expr, arg->annotation);
1535 }
1536
1537 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001538}
1539
Neal Norwitzc1505362006-12-28 06:47:50 +00001540static int
Yury Selivanov75445082015-05-11 22:57:16 -04001541symtable_visit_annotations(struct symtable *st, stmt_ty s,
1542 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 if (a->args && !symtable_visit_argannotations(st, a->args))
1545 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001546 if (a->vararg && a->vararg->annotation)
1547 VISIT(st, expr, a->vararg->annotation);
1548 if (a->kwarg && a->kwarg->annotation)
1549 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1551 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001552 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001553 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555}
1556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558symtable_visit_arguments(struct symtable *st, arguments_ty a)
1559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 /* skip default arguments inside function block
1561 XXX should ast be different?
1562 */
1563 if (a->args && !symtable_visit_params(st, a->args))
1564 return 0;
1565 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1566 return 0;
1567 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001568 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 return 0;
1570 st->st_cur->ste_varargs = 1;
1571 }
1572 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001573 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 return 0;
1575 st->st_cur->ste_varkeywords = 1;
1576 }
1577 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578}
1579
1580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 if (eh->v.ExceptHandler.type)
1585 VISIT(st, expr, eh->v.ExceptHandler.type);
1586 if (eh->v.ExceptHandler.name)
1587 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1588 return 0;
1589 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1590 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591}
1592
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001593static int
1594symtable_visit_withitem(struct symtable *st, withitem_ty item)
1595{
1596 VISIT(st, expr, item->context_expr);
1597 if (item->optional_vars) {
1598 VISIT(st, expr, item->optional_vars);
1599 }
1600 return 1;
1601}
1602
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605symtable_visit_alias(struct symtable *st, alias_ty a)
1606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001608 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 dotted package name (e.g. spam.eggs)
1610 */
1611 PyObject *store_name;
1612 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001613 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1614 PyUnicode_GET_LENGTH(name), 1);
1615 if (dot != -1) {
1616 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 if (!store_name)
1618 return 0;
1619 }
1620 else {
1621 store_name = name;
1622 Py_INCREF(store_name);
1623 }
1624 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1625 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1626 Py_DECREF(store_name);
1627 return r;
1628 }
1629 else {
1630 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001631 int lineno = st->st_cur->ste_lineno;
1632 int col_offset = st->st_cur->ste_col_offset;
1633 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001634 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001635 Py_DECREF(store_name);
1636 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 Py_DECREF(store_name);
1639 return 1;
1640 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641}
1642
1643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 VISIT(st, expr, lc->target);
1648 VISIT(st, expr, lc->iter);
1649 VISIT_SEQ(st, expr, lc->ifs);
1650 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651}
1652
1653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655symtable_visit_keyword(struct symtable *st, keyword_ty k)
1656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 VISIT(st, expr, k->value);
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_slice(struct symtable *st, slice_ty s)
1664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 switch (s->kind) {
1666 case Slice_kind:
1667 if (s->v.Slice.lower)
1668 VISIT(st, expr, s->v.Slice.lower)
1669 if (s->v.Slice.upper)
1670 VISIT(st, expr, s->v.Slice.upper)
1671 if (s->v.Slice.step)
1672 VISIT(st, expr, s->v.Slice.step)
1673 break;
1674 case ExtSlice_kind:
1675 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1676 break;
1677 case Index_kind:
1678 VISIT(st, expr, s->v.Index.value)
1679 break;
1680 }
1681 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682}
1683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001685symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001686 identifier scope_name, asdl_seq *generators,
1687 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 int is_generator = (e->kind == GeneratorExp_kind);
1690 int needs_tmp = !is_generator;
1691 comprehension_ty outermost = ((comprehension_ty)
1692 asdl_seq_GET(generators, 0));
1693 /* Outermost iterator is evaluated in current scope */
1694 VISIT(st, expr, outermost->iter);
1695 /* Create comprehension scope for the rest */
1696 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001697 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1698 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 return 0;
1700 }
1701 st->st_cur->ste_generator = is_generator;
1702 /* Outermost iter is received as an argument */
1703 if (!symtable_implicit_arg(st, 0)) {
1704 symtable_exit_block(st, (void *)e);
1705 return 0;
1706 }
1707 /* Allocate temporary name if needed */
1708 if (needs_tmp && !symtable_new_tmpname(st)) {
1709 symtable_exit_block(st, (void *)e);
1710 return 0;
1711 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001712 VISIT(st, expr, outermost->target);
1713 VISIT_SEQ(st, expr, outermost->ifs);
1714 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001716 VISIT(st, expr, value);
1717 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001722symtable_visit_genexp(struct symtable *st, expr_ty e)
1723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1725 e->v.GeneratorExp.generators,
1726 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001727}
1728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001730symtable_visit_listcomp(struct symtable *st, expr_ty e)
1731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1733 e->v.ListComp.generators,
1734 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001735}
1736
1737static int
1738symtable_visit_setcomp(struct symtable *st, expr_ty e)
1739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1741 e->v.SetComp.generators,
1742 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001743}
1744
1745static int
1746symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1749 e->v.DictComp.generators,
1750 e->v.DictComp.key,
1751 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001752}