blob: 3f03184f088ac767c619697b0fac30d3ba765a14 [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);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600371 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400372 data = PyList_GET_ITEM(ste->ste_directives, i);
373 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600374 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
375 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
376 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
377 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
378 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
379
380 return 0;
381 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400382 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600383 PyErr_SetString(PyExc_RuntimeError,
384 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400385 return 0;
386}
387
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388
389/* Analyze raw symbol information to determine scope of each name.
390
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000391 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397 explicit global is declared with the global statement. An implicit
398 global is a free variable for which the compiler has found no binding
399 in an enclosing function scope. The implicit global is either a global
400 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
401 to handle these names to implement slightly odd semantics. In such a
402 block, the name is treated as global until it is assigned to; then it
403 is treated as a local.
404
405 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000406 The first pass collects raw facts from the AST via the symtable_visit_*
407 functions: the name is a parameter here, the name is used but not defined
408 here, etc. The second pass analyzes these facts during a pass over the
409 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410
411 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000413 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000414 Names which are explicitly declared nonlocal must exist in this set of
415 visible names - if they do not, a syntax error is raised. After doing
416 the local analysis, it analyzes each of its child blocks using an
417 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418
Nick Coghlan650f0d02007-04-15 12:05:43 +0000419 The children update the free variable set. If a local variable is added to
420 the free variable set by the child, the variable is marked as a cell. The
421 function object being defined must provide runtime storage for the variable
422 that may outlive the function's frame. Cell variables are removed from the
423 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000424
Nick Coghlan650f0d02007-04-15 12:05:43 +0000425 During analysis, the names are:
426 symbols: dict mapping from symbol names to flag values (including offset scope values)
427 scopes: dict mapping from symbol names to scope values (no offset)
428 local: set of all symbol names local to the current scope
429 bound: set of all symbol names local to a containing function scope
430 free: set of all symbol names referenced but not bound in child scopes
431 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432*/
433
434#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 PyObject *o = PyLong_FromLong(I); \
436 if (!o) \
437 return 0; \
438 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
439 Py_DECREF(o); \
440 return 0; \
441 } \
442 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000443}
444
445/* Decide on scope of name, given flags.
446
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000447 The namespace dictionaries may be modified to record information
448 about the new name. For example, a new global will add an entry to
449 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450*/
451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000453analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 PyObject *bound, PyObject *local, PyObject *free,
455 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (flags & DEF_GLOBAL) {
458 if (flags & DEF_PARAM) {
459 PyErr_Format(PyExc_SyntaxError,
460 "name '%U' is parameter and global",
461 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400462 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000463 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 if (flags & DEF_NONLOCAL) {
465 PyErr_Format(PyExc_SyntaxError,
466 "name '%U' is nonlocal and global",
467 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400468 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 }
470 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
471 if (PySet_Add(global, name) < 0)
472 return 0;
473 if (bound && (PySet_Discard(bound, name) < 0))
474 return 0;
475 return 1;
476 }
477 if (flags & DEF_NONLOCAL) {
478 if (flags & DEF_PARAM) {
479 PyErr_Format(PyExc_SyntaxError,
480 "name '%U' is parameter and nonlocal",
481 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400482 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 }
484 if (!bound) {
485 PyErr_Format(PyExc_SyntaxError,
486 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400487 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 }
489 if (!PySet_Contains(bound, name)) {
490 PyErr_Format(PyExc_SyntaxError,
491 "no binding for nonlocal '%U' found",
492 name);
493
Benjamin Petersond9c87022012-10-31 20:26:20 -0400494 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 }
496 SET_SCOPE(scopes, name, FREE);
497 ste->ste_free = 1;
498 return PySet_Add(free, name) >= 0;
499 }
500 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000501 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (PySet_Add(local, name) < 0)
503 return 0;
504 if (PySet_Discard(global, name) < 0)
505 return 0;
506 return 1;
507 }
508 /* If an enclosing block has a binding for this name, it
509 is a free variable rather than a global variable.
510 Note that having a non-NULL bound implies that the block
511 is nested.
512 */
513 if (bound && PySet_Contains(bound, name)) {
514 SET_SCOPE(scopes, name, FREE);
515 ste->ste_free = 1;
516 return PySet_Add(free, name) >= 0;
517 }
518 /* If a parent has a global statement, then call it global
519 explicit? It could also be global implicit.
520 */
521 if (global && PySet_Contains(global, name)) {
522 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
523 return 1;
524 }
525 if (ste->ste_nested)
526 ste->ste_free = 1;
527 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
528 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529}
530
531#undef SET_SCOPE
532
533/* If a name is defined in free and also in locals, then this block
534 provides the binding for the free variable. The name should be
535 marked CELL in this block and removed from the free list.
536
537 Note that the current block's free variables are included in free.
538 That's safe because no name can be free and local in the same scope.
539*/
540
541static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500542analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 PyObject *name, *v, *v_cell;
545 int success = 0;
546 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 v_cell = PyLong_FromLong(CELL);
549 if (!v_cell)
550 return 0;
551 while (PyDict_Next(scopes, &pos, &name, &v)) {
552 long scope;
553 assert(PyLong_Check(v));
554 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000555 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 continue;
557 if (!PySet_Contains(free, name))
558 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* Replace LOCAL with CELL for this name, and remove
560 from free. It is safe to replace the value of name
561 in the dict, because it will not cause a resize.
562 */
563 if (PyDict_SetItem(scopes, name, v_cell) < 0)
564 goto error;
565 if (PySet_Discard(free, name) < 0)
566 goto error;
567 }
568 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 Py_DECREF(v_cell);
571 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572}
573
Benjamin Peterson312595c2013-05-15 15:26:42 -0500574static int
575drop_class_free(PySTEntryObject *ste, PyObject *free)
576{
577 int res;
578 if (!GET_IDENTIFIER(__class__))
579 return 0;
580 res = PySet_Discard(free, __class__);
581 if (res < 0)
582 return 0;
583 if (res)
584 ste->ste_needs_class_closure = 1;
585 return 1;
586}
587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588/* Enter the final scope information into the ste_symbols dict.
589 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590 * All arguments are dicts. Modifies symbols, others are read-only.
591*/
592static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000594 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 PyObject *name = NULL, *itr = NULL;
597 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
598 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 /* Update scope information for all symbols in this scope */
601 while (PyDict_Next(symbols, &pos, &name, &v)) {
602 long scope, flags;
603 assert(PyLong_Check(v));
604 flags = PyLong_AS_LONG(v);
605 v_scope = PyDict_GetItem(scopes, name);
606 assert(v_scope && PyLong_Check(v_scope));
607 scope = PyLong_AS_LONG(v_scope);
608 flags |= (scope << SCOPE_OFFSET);
609 v_new = PyLong_FromLong(flags);
610 if (!v_new)
611 return 0;
612 if (PyDict_SetItem(symbols, name, v_new) < 0) {
613 Py_DECREF(v_new);
614 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 Py_DECREF(v_new);
617 }
618
619 /* Record not yet resolved free variables from children (if any) */
620 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
621 if (!v_free)
622 return 0;
623
624 itr = PyObject_GetIter(free);
625 if (!itr)
626 goto error;
627
628 while ((name = PyIter_Next(itr))) {
629 v = PyDict_GetItem(symbols, name);
630
631 /* Handle symbol that already exists in this scope */
632 if (v) {
633 /* Handle a free variable in a method of
634 the class that has the same name as a local
635 or global in the class scope.
636 */
637 if (classflag &&
638 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
639 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
640 v_new = PyLong_FromLong(flags);
641 if (!v_new) {
642 goto error;
643 }
644 if (PyDict_SetItem(symbols, name, v_new) < 0) {
645 Py_DECREF(v_new);
646 goto error;
647 }
648 Py_DECREF(v_new);
649 }
650 /* It's a cell, or already free in this scope */
651 Py_DECREF(name);
652 continue;
653 }
654 /* Handle global symbol */
655 if (!PySet_Contains(bound, name)) {
656 Py_DECREF(name);
657 continue; /* it's a global */
658 }
659 /* Propagate new free symbol up the lexical stack */
660 if (PyDict_SetItem(symbols, name, v_free) < 0) {
661 goto error;
662 }
663 Py_DECREF(name);
664 }
665 Py_DECREF(itr);
666 Py_DECREF(v_free);
667 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000668error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 Py_XDECREF(v_free);
670 Py_XDECREF(itr);
671 Py_XDECREF(name);
672 return 0;
673}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674
675/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000676
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 Arguments:
678 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000679 bound -- set of variables bound in enclosing scopes (input). bound
680 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681 free -- set of free variables in enclosed scopes (output)
682 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000683
684 The implementation uses two mutually recursive functions,
685 analyze_block() and analyze_child_block(). analyze_block() is
686 responsible for analyzing the individual names defined in a block.
687 analyze_child_block() prepares temporary namespace dictionaries
688 used to evaluated nested blocks.
689
690 The two functions exist because a child block should see the name
691 bindings of its enclosing blocks, but those bindings should not
692 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693*/
694
695static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
697 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000698
699static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
701 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
704 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
705 PyObject *temp;
706 int i, success = 0;
707 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 local = PySet_New(NULL); /* collect new names bound in block */
710 if (!local)
711 goto error;
712 scopes = PyDict_New(); /* collect scopes defined for each name */
713 if (!scopes)
714 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 /* Allocate new global and bound variable dictionaries. These
717 dictionaries hold the names visible in nested blocks. For
718 ClassBlocks, the bound and global names are initialized
719 before analyzing names, because class bindings aren't
720 visible in methods. For other blocks, they are initialized
721 after names are analyzed.
722 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 /* TODO(jhylton): Package these dicts in a struct so that we
725 can write reasonable helper functions?
726 */
727 newglobal = PySet_New(NULL);
728 if (!newglobal)
729 goto error;
730 newfree = PySet_New(NULL);
731 if (!newfree)
732 goto error;
733 newbound = PySet_New(NULL);
734 if (!newbound)
735 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 /* Class namespace has no effect on names visible in
738 nested functions, so populate the global and bound
739 sets to be passed to child blocks before analyzing
740 this one.
741 */
742 if (ste->ste_type == ClassBlock) {
743 /* Pass down known globals */
744 temp = PyNumber_InPlaceOr(newglobal, global);
745 if (!temp)
746 goto error;
747 Py_DECREF(temp);
748 /* Pass down previously bound symbols */
749 if (bound) {
750 temp = PyNumber_InPlaceOr(newbound, bound);
751 if (!temp)
752 goto error;
753 Py_DECREF(temp);
754 }
755 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
758 long flags = PyLong_AS_LONG(v);
759 if (!analyze_name(ste, scopes, name, flags,
760 bound, local, free, global))
761 goto error;
762 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 /* Populate global and bound sets to be passed to children. */
765 if (ste->ste_type != ClassBlock) {
766 /* Add function locals to bound set */
767 if (ste->ste_type == FunctionBlock) {
768 temp = PyNumber_InPlaceOr(newbound, local);
769 if (!temp)
770 goto error;
771 Py_DECREF(temp);
772 }
773 /* Pass down previously bound symbols */
774 if (bound) {
775 temp = PyNumber_InPlaceOr(newbound, bound);
776 if (!temp)
777 goto error;
778 Py_DECREF(temp);
779 }
780 /* Pass down known globals */
781 temp = PyNumber_InPlaceOr(newglobal, global);
782 if (!temp)
783 goto error;
784 Py_DECREF(temp);
785 }
786 else {
787 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000788 if (!GET_IDENTIFIER(__class__))
789 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 if (PySet_Add(newbound, __class__) < 0)
791 goto error;
792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300794 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 newbound, newglobal now contain the names visible in
797 nested blocks. The free variables in the children will
798 be collected in allfree.
799 */
800 allfree = PySet_New(NULL);
801 if (!allfree)
802 goto error;
803 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
804 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
805 PySTEntryObject* entry;
806 assert(c && PySTEntry_Check(c));
807 entry = (PySTEntryObject*)c;
808 if (!analyze_child_block(entry, newbound, newfree, newglobal,
809 allfree))
810 goto error;
811 /* Check if any children have free variables */
812 if (entry->ste_free || entry->ste_child_free)
813 ste->ste_child_free = 1;
814 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 temp = PyNumber_InPlaceOr(newfree, allfree);
817 if (!temp)
818 goto error;
819 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500822 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500824 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 goto error;
826 /* Records the results of the analysis in the symbol table entry */
827 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
828 ste->ste_type == ClassBlock))
829 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 temp = PyNumber_InPlaceOr(free, newfree);
832 if (!temp)
833 goto error;
834 Py_DECREF(temp);
835 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 Py_XDECREF(scopes);
838 Py_XDECREF(local);
839 Py_XDECREF(newbound);
840 Py_XDECREF(newglobal);
841 Py_XDECREF(newfree);
842 Py_XDECREF(allfree);
843 if (!success)
844 assert(PyErr_Occurred());
845 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846}
847
848static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
850 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
853 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000856
Martin Panter3ee62702016-06-04 04:57:19 +0000857 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 current block. The analyze_block() call modifies these
859 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 */
862 temp_bound = PySet_New(bound);
863 if (!temp_bound)
864 goto error;
865 temp_free = PySet_New(free);
866 if (!temp_free)
867 goto error;
868 temp_global = PySet_New(global);
869 if (!temp_global)
870 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
873 goto error;
874 temp = PyNumber_InPlaceOr(child_free, temp_free);
875 if (!temp)
876 goto error;
877 Py_DECREF(temp);
878 Py_DECREF(temp_bound);
879 Py_DECREF(temp_free);
880 Py_DECREF(temp_global);
881 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000882 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 Py_XDECREF(temp_bound);
884 Py_XDECREF(temp_free);
885 Py_XDECREF(temp_global);
886 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000887}
888
889static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890symtable_analyze(struct symtable *st)
891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 PyObject *free, *global;
893 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 free = PySet_New(NULL);
896 if (!free)
897 return 0;
898 global = PySet_New(NULL);
899 if (!global) {
900 Py_DECREF(free);
901 return 0;
902 }
903 r = analyze_block(st->st_top, NULL, free, global);
904 Py_DECREF(free);
905 Py_DECREF(global);
906 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907}
908
909
910static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200911symtable_warn(struct symtable *st, const char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912{
Victor Stinner14e461d2013-08-26 22:28:21 +0200913 PyObject *message = PyUnicode_FromString(msg);
914 if (message == NULL)
915 return 0;
916 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, message, st->st_filename,
917 lineno, NULL, NULL) < 0) {
918 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
920 PyErr_SetString(PyExc_SyntaxError, msg);
Victor Stinner14e461d2013-08-26 22:28:21 +0200921 PyErr_SyntaxLocationObject(st->st_filename, st->st_cur->ste_lineno,
922 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 }
924 return 0;
925 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200926 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928}
929
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000930/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931 This reference is released when the block is exited, via the DECREF
932 in symtable_exit_block().
933*/
934
935static int
936symtable_exit_block(struct symtable *st, void *ast)
937{
Benjamin Peterson609da582011-06-29 22:52:39 -0500938 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939
Benjamin Peterson609da582011-06-29 22:52:39 -0500940 st->st_cur = NULL;
941 size = PyList_GET_SIZE(st->st_stack);
942 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500943 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500945 if (--size)
946 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 }
948 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949}
950
951static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000953 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954{
Benjamin Peterson609da582011-06-29 22:52:39 -0500955 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956
Benjamin Peterson609da582011-06-29 22:52:39 -0500957 ste = ste_new(st, name, block, ast, lineno, col_offset);
958 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500960 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
961 Py_DECREF(ste);
962 return 0;
963 }
964 prev = st->st_cur;
965 /* The entry is owned by the stack. Borrow it for st_cur. */
966 Py_DECREF(ste);
967 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000968 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 st->st_global = st->st_cur->ste_symbols;
970 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500971 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 return 0;
973 }
974 }
975 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976}
977
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000978static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979symtable_lookup(struct symtable *st, PyObject *name)
980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 PyObject *o;
982 PyObject *mangled = _Py_Mangle(st->st_private, name);
983 if (!mangled)
984 return 0;
985 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
986 Py_DECREF(mangled);
987 if (!o)
988 return 0;
989 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990}
991
992static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 PyObject *o;
996 PyObject *dict;
997 long val;
998 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999
Jeremy Hylton81e95022007-02-27 06:50:52 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 if (!mangled)
1002 return 0;
1003 dict = st->st_cur->ste_symbols;
1004 if ((o = PyDict_GetItem(dict, mangled))) {
1005 val = PyLong_AS_LONG(o);
1006 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1007 /* Is it better to use 'mangled' or 'name' here? */
1008 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001009 PyErr_SyntaxLocationObject(st->st_filename,
1010 st->st_cur->ste_lineno,
1011 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 goto error;
1013 }
1014 val |= flag;
1015 } else
1016 val = flag;
1017 o = PyLong_FromLong(val);
1018 if (o == NULL)
1019 goto error;
1020 if (PyDict_SetItem(dict, mangled, o) < 0) {
1021 Py_DECREF(o);
1022 goto error;
1023 }
1024 Py_DECREF(o);
1025
1026 if (flag & DEF_PARAM) {
1027 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1028 goto error;
1029 } else if (flag & DEF_GLOBAL) {
1030 /* XXX need to update DEF_GLOBAL for other flags too;
1031 perhaps only DEF_FREE_GLOBAL */
1032 val = flag;
1033 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1034 val |= PyLong_AS_LONG(o);
1035 }
1036 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 goto error;
1039 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1040 Py_DECREF(o);
1041 goto error;
1042 }
1043 Py_DECREF(o);
1044 }
1045 Py_DECREF(mangled);
1046 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001047
1048error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 Py_DECREF(mangled);
1050 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051}
1052
1053/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1054 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 function.
1056
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1058 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001059
1060 VISIT_QUIT macro returns the specified value exiting from the function but
1061 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062*/
1063
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001064#define VISIT_QUIT(ST, X) \
1065 return --(ST)->recursion_depth,(X)
1066
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001069 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 int i; \
1073 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1074 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1075 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1076 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001077 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 int i; \
1083 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1084 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1085 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1086 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001087 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001090
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001091#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001093 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001095 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001097 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001098 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001100}
1101
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001103symtable_new_tmpname(struct symtable *st)
1104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 char tmpname[256];
1106 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1109 ++st->st_cur->ste_tmpname);
1110 tmp = PyUnicode_InternFromString(tmpname);
1111 if (!tmp)
1112 return 0;
1113 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1114 return 0;
1115 Py_DECREF(tmp);
1116 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001117}
1118
Guido van Rossum4f72a782006-10-27 23:31:49 +00001119
Guido van Rossumc2e20742006-02-27 22:32:47 +00001120static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001121symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1122{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001123 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001124 int res;
1125 if (!st->st_cur->ste_directives) {
1126 st->st_cur->ste_directives = PyList_New(0);
1127 if (!st->st_cur->ste_directives)
1128 return 0;
1129 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001130 mangled = _Py_Mangle(st->st_private, name);
1131 if (!mangled)
1132 return 0;
1133 data = Py_BuildValue("(Nii)", mangled, s->lineno, s->col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001134 if (!data)
1135 return 0;
1136 res = PyList_Append(st->st_cur->ste_directives, data);
1137 Py_DECREF(data);
1138 return res == 0;
1139}
1140
1141
1142static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143symtable_visit_stmt(struct symtable *st, stmt_ty s)
1144{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001145 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001146 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001147 "maximum recursion depth exceeded during compilation");
1148 VISIT_QUIT(st, 0);
1149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 switch (s->kind) {
1151 case FunctionDef_kind:
1152 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001153 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (s->v.FunctionDef.args->defaults)
1155 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1156 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001157 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001158 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1159 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001160 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (s->v.FunctionDef.decorator_list)
1162 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1163 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001164 FunctionBlock, (void *)s, s->lineno,
1165 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001166 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001167 VISIT(st, arguments, s->v.FunctionDef.args);
1168 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001170 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 break;
1172 case ClassDef_kind: {
1173 PyObject *tmp;
1174 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001175 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1177 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 if (s->v.ClassDef.decorator_list)
1179 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1180 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001181 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001182 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 tmp = st->st_private;
1184 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001185 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 st->st_private = tmp;
1187 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001188 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 break;
1190 }
1191 case Return_kind:
1192 if (s->v.Return.value) {
1193 VISIT(st, expr, s->v.Return.value);
1194 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 }
1196 break;
1197 case Delete_kind:
1198 VISIT_SEQ(st, expr, s->v.Delete.targets);
1199 break;
1200 case Assign_kind:
1201 VISIT_SEQ(st, expr, s->v.Assign.targets);
1202 VISIT(st, expr, s->v.Assign.value);
1203 break;
1204 case AugAssign_kind:
1205 VISIT(st, expr, s->v.AugAssign.target);
1206 VISIT(st, expr, s->v.AugAssign.value);
1207 break;
1208 case For_kind:
1209 VISIT(st, expr, s->v.For.target);
1210 VISIT(st, expr, s->v.For.iter);
1211 VISIT_SEQ(st, stmt, s->v.For.body);
1212 if (s->v.For.orelse)
1213 VISIT_SEQ(st, stmt, s->v.For.orelse);
1214 break;
1215 case While_kind:
1216 VISIT(st, expr, s->v.While.test);
1217 VISIT_SEQ(st, stmt, s->v.While.body);
1218 if (s->v.While.orelse)
1219 VISIT_SEQ(st, stmt, s->v.While.orelse);
1220 break;
1221 case If_kind:
1222 /* XXX if 0: and lookup_yield() hacks */
1223 VISIT(st, expr, s->v.If.test);
1224 VISIT_SEQ(st, stmt, s->v.If.body);
1225 if (s->v.If.orelse)
1226 VISIT_SEQ(st, stmt, s->v.If.orelse);
1227 break;
1228 case Raise_kind:
1229 if (s->v.Raise.exc) {
1230 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001231 if (s->v.Raise.cause) {
1232 VISIT(st, expr, s->v.Raise.cause);
1233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 }
1235 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001236 case Try_kind:
1237 VISIT_SEQ(st, stmt, s->v.Try.body);
1238 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1239 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1240 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 break;
1242 case Assert_kind:
1243 VISIT(st, expr, s->v.Assert.test);
1244 if (s->v.Assert.msg)
1245 VISIT(st, expr, s->v.Assert.msg);
1246 break;
1247 case Import_kind:
1248 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 break;
1250 case ImportFrom_kind:
1251 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 break;
1253 case Global_kind: {
1254 int i;
1255 asdl_seq *seq = s->v.Global.names;
1256 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1257 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 long cur = symtable_lookup(st, name);
1259 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001260 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (cur & (DEF_LOCAL | USE)) {
1262 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001263 char *c_name = _PyUnicode_AsString(name);
1264 if (!c_name)
1265 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 if (cur & DEF_LOCAL)
1267 PyOS_snprintf(buf, sizeof(buf),
1268 GLOBAL_AFTER_ASSIGN,
1269 c_name);
1270 else
1271 PyOS_snprintf(buf, sizeof(buf),
1272 GLOBAL_AFTER_USE,
1273 c_name);
1274 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001275 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 }
1277 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001278 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001279 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001280 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 }
1282 break;
1283 }
1284 case Nonlocal_kind: {
1285 int i;
1286 asdl_seq *seq = s->v.Nonlocal.names;
1287 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1288 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 long cur = symtable_lookup(st, name);
1290 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001291 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (cur & (DEF_LOCAL | USE)) {
1293 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001294 char *c_name = _PyUnicode_AsString(name);
1295 if (!c_name)
1296 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 if (cur & DEF_LOCAL)
1298 PyOS_snprintf(buf, sizeof(buf),
1299 NONLOCAL_AFTER_ASSIGN,
1300 c_name);
1301 else
1302 PyOS_snprintf(buf, sizeof(buf),
1303 NONLOCAL_AFTER_USE,
1304 c_name);
1305 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001306 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 }
1308 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001309 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001310 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001311 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 }
1313 break;
1314 }
1315 case Expr_kind:
1316 VISIT(st, expr, s->v.Expr.value);
1317 break;
1318 case Pass_kind:
1319 case Break_kind:
1320 case Continue_kind:
1321 /* nothing to do here */
1322 break;
1323 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001324 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 VISIT_SEQ(st, stmt, s->v.With.body);
1326 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001327 case AsyncFunctionDef_kind:
1328 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1329 VISIT_QUIT(st, 0);
1330 if (s->v.AsyncFunctionDef.args->defaults)
1331 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1332 if (s->v.AsyncFunctionDef.args->kw_defaults)
1333 VISIT_SEQ_WITH_NULL(st, expr,
1334 s->v.AsyncFunctionDef.args->kw_defaults);
1335 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1336 s->v.AsyncFunctionDef.returns))
1337 VISIT_QUIT(st, 0);
1338 if (s->v.AsyncFunctionDef.decorator_list)
1339 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1340 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1341 FunctionBlock, (void *)s, s->lineno,
1342 s->col_offset))
1343 VISIT_QUIT(st, 0);
1344 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1345 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1346 if (!symtable_exit_block(st, s))
1347 VISIT_QUIT(st, 0);
1348 break;
1349 case AsyncWith_kind:
1350 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1351 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1352 break;
1353 case AsyncFor_kind:
1354 VISIT(st, expr, s->v.AsyncFor.target);
1355 VISIT(st, expr, s->v.AsyncFor.iter);
1356 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1357 if (s->v.AsyncFor.orelse)
1358 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1359 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001361 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362}
1363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365symtable_visit_expr(struct symtable *st, expr_ty e)
1366{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001367 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001368 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001369 "maximum recursion depth exceeded during compilation");
1370 VISIT_QUIT(st, 0);
1371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 switch (e->kind) {
1373 case BoolOp_kind:
1374 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1375 break;
1376 case BinOp_kind:
1377 VISIT(st, expr, e->v.BinOp.left);
1378 VISIT(st, expr, e->v.BinOp.right);
1379 break;
1380 case UnaryOp_kind:
1381 VISIT(st, expr, e->v.UnaryOp.operand);
1382 break;
1383 case Lambda_kind: {
1384 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001385 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 if (e->v.Lambda.args->defaults)
1387 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001388 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001389 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001391 FunctionBlock, (void *)e, e->lineno,
1392 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001393 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001394 VISIT(st, arguments, e->v.Lambda.args);
1395 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001397 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 break;
1399 }
1400 case IfExp_kind:
1401 VISIT(st, expr, e->v.IfExp.test);
1402 VISIT(st, expr, e->v.IfExp.body);
1403 VISIT(st, expr, e->v.IfExp.orelse);
1404 break;
1405 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001406 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 VISIT_SEQ(st, expr, e->v.Dict.values);
1408 break;
1409 case Set_kind:
1410 VISIT_SEQ(st, expr, e->v.Set.elts);
1411 break;
1412 case GeneratorExp_kind:
1413 if (!symtable_visit_genexp(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 ListComp_kind:
1417 if (!symtable_visit_listcomp(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 SetComp_kind:
1421 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001422 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 break;
1424 case DictComp_kind:
1425 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001426 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 break;
1428 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001429 if (e->v.Yield.value)
1430 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001433 case YieldFrom_kind:
1434 VISIT(st, expr, e->v.YieldFrom.value);
1435 st->st_cur->ste_generator = 1;
1436 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001437 case Await_kind:
1438 VISIT(st, expr, e->v.Await.value);
1439 st->st_cur->ste_generator = 1;
1440 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 case Compare_kind:
1442 VISIT(st, expr, e->v.Compare.left);
1443 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1444 break;
1445 case Call_kind:
1446 VISIT(st, expr, e->v.Call.func);
1447 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001448 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001450 case FormattedValue_kind:
1451 VISIT(st, expr, e->v.FormattedValue.value);
1452 if (e->v.FormattedValue.format_spec)
1453 VISIT(st, expr, e->v.FormattedValue.format_spec);
1454 break;
1455 case JoinedStr_kind:
1456 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1457 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001458 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 case Num_kind:
1460 case Str_kind:
1461 case Bytes_kind:
1462 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001463 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 /* Nothing to do here. */
1465 break;
1466 /* The following exprs can be assignment targets. */
1467 case Attribute_kind:
1468 VISIT(st, expr, e->v.Attribute.value);
1469 break;
1470 case Subscript_kind:
1471 VISIT(st, expr, e->v.Subscript.value);
1472 VISIT(st, slice, e->v.Subscript.slice);
1473 break;
1474 case Starred_kind:
1475 VISIT(st, expr, e->v.Starred.value);
1476 break;
1477 case Name_kind:
1478 if (!symtable_add_def(st, e->v.Name.id,
1479 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001480 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 /* Special-case super: it counts as a use of __class__ */
1482 if (e->v.Name.ctx == Load &&
1483 st->st_cur->ste_type == FunctionBlock &&
1484 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001485 if (!GET_IDENTIFIER(__class__) ||
1486 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001487 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 }
1489 break;
1490 /* child nodes of List and Tuple will have expr_context set */
1491 case List_kind:
1492 VISIT_SEQ(st, expr, e->v.List.elts);
1493 break;
1494 case Tuple_kind:
1495 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1496 break;
1497 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001498 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499}
1500
1501static int
1502symtable_implicit_arg(struct symtable *st, int pos)
1503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1505 if (id == NULL)
1506 return 0;
1507 if (!symtable_add_def(st, id, DEF_PARAM)) {
1508 Py_DECREF(id);
1509 return 0;
1510 }
1511 Py_DECREF(id);
1512 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513}
1514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001516symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (!args)
1521 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 for (i = 0; i < asdl_seq_LEN(args); i++) {
1524 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1525 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1526 return 0;
1527 }
1528
1529 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530}
1531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001533symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 if (!args)
1538 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 for (i = 0; i < asdl_seq_LEN(args); i++) {
1541 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1542 if (arg->annotation)
1543 VISIT(st, expr, arg->annotation);
1544 }
1545
1546 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001547}
1548
Neal Norwitzc1505362006-12-28 06:47:50 +00001549static int
Yury Selivanov75445082015-05-11 22:57:16 -04001550symtable_visit_annotations(struct symtable *st, stmt_ty s,
1551 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 if (a->args && !symtable_visit_argannotations(st, a->args))
1554 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001555 if (a->vararg && a->vararg->annotation)
1556 VISIT(st, expr, a->vararg->annotation);
1557 if (a->kwarg && a->kwarg->annotation)
1558 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1560 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001561 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001562 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564}
1565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567symtable_visit_arguments(struct symtable *st, arguments_ty a)
1568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 /* skip default arguments inside function block
1570 XXX should ast be different?
1571 */
1572 if (a->args && !symtable_visit_params(st, a->args))
1573 return 0;
1574 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1575 return 0;
1576 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001577 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 return 0;
1579 st->st_cur->ste_varargs = 1;
1580 }
1581 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001582 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 return 0;
1584 st->st_cur->ste_varkeywords = 1;
1585 }
1586 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587}
1588
1589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 if (eh->v.ExceptHandler.type)
1594 VISIT(st, expr, eh->v.ExceptHandler.type);
1595 if (eh->v.ExceptHandler.name)
1596 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1597 return 0;
1598 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1599 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600}
1601
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001602static int
1603symtable_visit_withitem(struct symtable *st, withitem_ty item)
1604{
1605 VISIT(st, expr, item->context_expr);
1606 if (item->optional_vars) {
1607 VISIT(st, expr, item->optional_vars);
1608 }
1609 return 1;
1610}
1611
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614symtable_visit_alias(struct symtable *st, alias_ty a)
1615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001617 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 dotted package name (e.g. spam.eggs)
1619 */
1620 PyObject *store_name;
1621 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001622 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1623 PyUnicode_GET_LENGTH(name), 1);
1624 if (dot != -1) {
1625 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 if (!store_name)
1627 return 0;
1628 }
1629 else {
1630 store_name = name;
1631 Py_INCREF(store_name);
1632 }
1633 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1634 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1635 Py_DECREF(store_name);
1636 return r;
1637 }
1638 else {
1639 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001640 int lineno = st->st_cur->ste_lineno;
1641 int col_offset = st->st_cur->ste_col_offset;
1642 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001643 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001644 Py_DECREF(store_name);
1645 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 Py_DECREF(store_name);
1648 return 1;
1649 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650}
1651
1652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 VISIT(st, expr, lc->target);
1657 VISIT(st, expr, lc->iter);
1658 VISIT_SEQ(st, expr, lc->ifs);
1659 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660}
1661
1662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664symtable_visit_keyword(struct symtable *st, keyword_ty k)
1665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 VISIT(st, expr, k->value);
1667 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668}
1669
1670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672symtable_visit_slice(struct symtable *st, slice_ty s)
1673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 switch (s->kind) {
1675 case Slice_kind:
1676 if (s->v.Slice.lower)
1677 VISIT(st, expr, s->v.Slice.lower)
1678 if (s->v.Slice.upper)
1679 VISIT(st, expr, s->v.Slice.upper)
1680 if (s->v.Slice.step)
1681 VISIT(st, expr, s->v.Slice.step)
1682 break;
1683 case ExtSlice_kind:
1684 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1685 break;
1686 case Index_kind:
1687 VISIT(st, expr, s->v.Index.value)
1688 break;
1689 }
1690 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691}
1692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001694symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001695 identifier scope_name, asdl_seq *generators,
1696 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 int is_generator = (e->kind == GeneratorExp_kind);
1699 int needs_tmp = !is_generator;
1700 comprehension_ty outermost = ((comprehension_ty)
1701 asdl_seq_GET(generators, 0));
1702 /* Outermost iterator is evaluated in current scope */
1703 VISIT(st, expr, outermost->iter);
1704 /* Create comprehension scope for the rest */
1705 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001706 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1707 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 return 0;
1709 }
1710 st->st_cur->ste_generator = is_generator;
1711 /* Outermost iter is received as an argument */
1712 if (!symtable_implicit_arg(st, 0)) {
1713 symtable_exit_block(st, (void *)e);
1714 return 0;
1715 }
1716 /* Allocate temporary name if needed */
1717 if (needs_tmp && !symtable_new_tmpname(st)) {
1718 symtable_exit_block(st, (void *)e);
1719 return 0;
1720 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001721 VISIT(st, expr, outermost->target);
1722 VISIT_SEQ(st, expr, outermost->ifs);
1723 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001725 VISIT(st, expr, value);
1726 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001731symtable_visit_genexp(struct symtable *st, expr_ty e)
1732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1734 e->v.GeneratorExp.generators,
1735 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001736}
1737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001738static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001739symtable_visit_listcomp(struct symtable *st, expr_ty e)
1740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1742 e->v.ListComp.generators,
1743 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001744}
1745
1746static int
1747symtable_visit_setcomp(struct symtable *st, expr_ty e)
1748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1750 e->v.SetComp.generators,
1751 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001752}
1753
1754static int
1755symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1758 e->v.DictComp.generators,
1759 e->v.DictComp.key,
1760 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001761}