blob: e55813feb5318483cdb6f350e0f99d8505e88f77 [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;
Yury Selivanoveb636452016-09-08 22:01:51 -070066 ste->ste_coroutine = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050068 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069
Victor Stinner9a4fb662013-07-11 22:49:00 +020070 ste->ste_symbols = PyDict_New();
71 ste->ste_varnames = PyList_New(0);
72 ste->ste_children = PyList_New(0);
73 if (ste->ste_symbols == NULL
74 || ste->ste_varnames == NULL
75 || ste->ste_children == NULL)
76 goto fail;
77
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
79 goto fail;
80
81 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 Py_XDECREF(ste);
84 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085}
86
87static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
91 ste->ste_name,
92 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093}
94
95static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 ste->ste_table = NULL;
99 Py_XDECREF(ste->ste_id);
100 Py_XDECREF(ste->ste_name);
101 Py_XDECREF(ste->ste_symbols);
102 Py_XDECREF(ste->ste_varnames);
103 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400104 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000106}
107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109
Guido van Rossum6f799372001-09-20 20:46:19 +0000110static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 {"id", T_OBJECT, OFF(ste_id), READONLY},
112 {"name", T_OBJECT, OFF(ste_name), READONLY},
113 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
114 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
115 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 {"nested", T_INT, OFF(ste_nested), READONLY},
117 {"type", T_INT, OFF(ste_type), READONLY},
118 {"lineno", T_INT, OFF(ste_lineno), READONLY},
119 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120};
121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 PyVarObject_HEAD_INIT(&PyType_Type, 0)
124 "symtable entry",
125 sizeof(PySTEntryObject),
126 0,
127 (destructor)ste_dealloc, /* tp_dealloc */
128 0, /* tp_print */
129 0, /* tp_getattr */
130 0, /* tp_setattr */
131 0, /* tp_reserved */
132 (reprfunc)ste_repr, /* tp_repr */
133 0, /* tp_as_number */
134 0, /* tp_as_sequence */
135 0, /* tp_as_mapping */
136 0, /* tp_hash */
137 0, /* tp_call */
138 0, /* tp_str */
139 PyObject_GenericGetAttr, /* tp_getattro */
140 0, /* tp_setattro */
141 0, /* tp_as_buffer */
142 Py_TPFLAGS_DEFAULT, /* tp_flags */
143 0, /* tp_doc */
144 0, /* tp_traverse */
145 0, /* tp_clear */
146 0, /* tp_richcompare */
147 0, /* tp_weaklistoffset */
148 0, /* tp_iter */
149 0, /* tp_iternext */
150 0, /* tp_methods */
151 ste_memberlist, /* tp_members */
152 0, /* tp_getset */
153 0, /* tp_base */
154 0, /* tp_dict */
155 0, /* tp_descr_get */
156 0, /* tp_descr_set */
157 0, /* tp_dictoffset */
158 0, /* tp_init */
159 0, /* tp_alloc */
160 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000161};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162
163static int symtable_analyze(struct symtable *st);
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200164static int symtable_warn(struct symtable *st, const char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000166 _Py_block_ty block, void *ast, int lineno,
167 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168static int symtable_exit_block(struct symtable *st, void *ast);
169static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
170static int symtable_visit_expr(struct symtable *st, expr_ty s);
171static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000172static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
173static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000174static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000175static int symtable_visit_arguments(struct symtable *st, arguments_ty);
176static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
177static int symtable_visit_alias(struct symtable *st, alias_ty);
178static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
179static int symtable_visit_keyword(struct symtable *st, keyword_ty);
180static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000181static int symtable_visit_params(struct symtable *st, asdl_seq *args);
182static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183static int symtable_implicit_arg(struct symtable *st, int pos);
Yury Selivanov75445082015-05-11 22:57:16 -0400184static int symtable_visit_annotations(struct symtable *st, stmt_ty s, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500185static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186
187
Nick Coghlan650f0d02007-04-15 12:05:43 +0000188static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500190 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
192#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194
195#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000196"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
198static struct symtable *
199symtable_new(void)
200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
204 if (st == NULL)
205 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 st->st_filename = NULL;
208 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if ((st->st_stack = PyList_New(0)) == NULL)
211 goto fail;
212 if ((st->st_blocks = PyDict_New()) == NULL)
213 goto fail;
214 st->st_cur = NULL;
215 st->st_private = NULL;
216 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 PySymtable_Free(st);
219 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220}
221
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000222/* When compiling the use of C stack is probably going to be a lot
223 lighter than when executing Python code but still can overflow
224 and causing a Python crash if not checked (e.g. eval("()"*300000)).
225 Using the current recursion limit for the compiler seems too
226 restrictive (it caused at least one test to fail) so a factor is
227 used to allow deeper recursion when compiling an expression.
228
229 Using a scaling factor means this should automatically adjust when
230 the recursion limit is adjusted for small or large C stack allocations.
231*/
232#define COMPILER_STACK_FRAME_SCALE 3
233
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200235PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000237 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 asdl_seq *seq;
239 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000240 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400241 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200244 return NULL;
245 if (filename == NULL) {
246 PySymtable_Free(st);
247 return NULL;
248 }
249 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 st->st_filename = filename;
251 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000252
253 /* Setup recursion depth check counters */
254 tstate = PyThreadState_GET();
255 if (!tstate) {
256 PySymtable_Free(st);
257 return NULL;
258 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400259 /* Be careful here to prevent overflow. */
260 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
261 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
262 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
263 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 /* Make the initial symbol information gathering pass */
266 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000267 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 PySymtable_Free(st);
269 return NULL;
270 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 switch (mod->kind) {
274 case Module_kind:
275 seq = mod->v.Module.body;
276 for (i = 0; i < asdl_seq_LEN(seq); i++)
277 if (!symtable_visit_stmt(st,
278 (stmt_ty)asdl_seq_GET(seq, i)))
279 goto error;
280 break;
281 case Expression_kind:
282 if (!symtable_visit_expr(st, mod->v.Expression.body))
283 goto error;
284 break;
285 case Interactive_kind:
286 seq = mod->v.Interactive.body;
287 for (i = 0; i < asdl_seq_LEN(seq); i++)
288 if (!symtable_visit_stmt(st,
289 (stmt_ty)asdl_seq_GET(seq, i)))
290 goto error;
291 break;
292 case Suite_kind:
293 PyErr_SetString(PyExc_RuntimeError,
294 "this compiler does not handle Suites");
295 goto error;
296 }
297 if (!symtable_exit_block(st, (void *)mod)) {
298 PySymtable_Free(st);
299 return NULL;
300 }
301 /* Make the second symbol analysis pass */
302 if (symtable_analyze(st))
303 return st;
304 PySymtable_Free(st);
305 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 (void) symtable_exit_block(st, (void *)mod);
308 PySymtable_Free(st);
309 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310}
311
Victor Stinner14e461d2013-08-26 22:28:21 +0200312struct symtable *
313PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
314{
315 PyObject *filename;
316 struct symtable *st;
317 filename = PyUnicode_DecodeFSDefault(filename_str);
318 if (filename == NULL)
319 return NULL;
320 st = PySymtable_BuildObject(mod, filename, future);
321 Py_DECREF(filename);
322 return st;
323}
324
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325void
326PySymtable_Free(struct symtable *st)
327{
Victor Stinner14e461d2013-08-26 22:28:21 +0200328 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 Py_XDECREF(st->st_blocks);
330 Py_XDECREF(st->st_stack);
331 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332}
333
334PySTEntryObject *
335PySymtable_Lookup(struct symtable *st, void *key)
336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 k = PyLong_FromVoidPtr(key);
340 if (k == NULL)
341 return NULL;
342 v = PyDict_GetItem(st->st_blocks, k);
343 if (v) {
344 assert(PySTEntry_Check(v));
345 Py_INCREF(v);
346 }
347 else {
348 PyErr_SetString(PyExc_KeyError,
349 "unknown symbol table entry");
350 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 Py_DECREF(k);
353 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354}
355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357PyST_GetScope(PySTEntryObject *ste, PyObject *name)
358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
360 if (!v)
361 return 0;
362 assert(PyLong_Check(v));
363 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364}
365
Benjamin Petersond9c87022012-10-31 20:26:20 -0400366static int
367error_at_directive(PySTEntryObject *ste, PyObject *name)
368{
369 Py_ssize_t i;
370 PyObject *data;
371 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600372 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400373 data = PyList_GET_ITEM(ste->ste_directives, i);
374 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600375 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
376 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
377 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
378 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
379 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
380
381 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700382 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400383 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600384 PyErr_SetString(PyExc_RuntimeError,
385 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400386 return 0;
387}
388
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389
390/* Analyze raw symbol information to determine scope of each name.
391
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000392 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398 explicit global is declared with the global statement. An implicit
399 global is a free variable for which the compiler has found no binding
400 in an enclosing function scope. The implicit global is either a global
401 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
402 to handle these names to implement slightly odd semantics. In such a
403 block, the name is treated as global until it is assigned to; then it
404 is treated as a local.
405
406 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000407 The first pass collects raw facts from the AST via the symtable_visit_*
408 functions: the name is a parameter here, the name is used but not defined
409 here, etc. The second pass analyzes these facts during a pass over the
410 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000411
412 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000414 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000415 Names which are explicitly declared nonlocal must exist in this set of
416 visible names - if they do not, a syntax error is raised. After doing
417 the local analysis, it analyzes each of its child blocks using an
418 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000419
Nick Coghlan650f0d02007-04-15 12:05:43 +0000420 The children update the free variable set. If a local variable is added to
421 the free variable set by the child, the variable is marked as a cell. The
422 function object being defined must provide runtime storage for the variable
423 that may outlive the function's frame. Cell variables are removed from the
424 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000425
Nick Coghlan650f0d02007-04-15 12:05:43 +0000426 During analysis, the names are:
427 symbols: dict mapping from symbol names to flag values (including offset scope values)
428 scopes: dict mapping from symbol names to scope values (no offset)
429 local: set of all symbol names local to the current scope
430 bound: set of all symbol names local to a containing function scope
431 free: set of all symbol names referenced but not bound in child scopes
432 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433*/
434
435#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 PyObject *o = PyLong_FromLong(I); \
437 if (!o) \
438 return 0; \
439 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
440 Py_DECREF(o); \
441 return 0; \
442 } \
443 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444}
445
446/* Decide on scope of name, given flags.
447
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000448 The namespace dictionaries may be modified to record information
449 about the new name. For example, a new global will add an entry to
450 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451*/
452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000454analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 PyObject *bound, PyObject *local, PyObject *free,
456 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 if (flags & DEF_GLOBAL) {
459 if (flags & DEF_PARAM) {
460 PyErr_Format(PyExc_SyntaxError,
461 "name '%U' is parameter and global",
462 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400463 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000464 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 if (flags & DEF_NONLOCAL) {
466 PyErr_Format(PyExc_SyntaxError,
467 "name '%U' is nonlocal and global",
468 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400469 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 }
471 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
472 if (PySet_Add(global, name) < 0)
473 return 0;
474 if (bound && (PySet_Discard(bound, name) < 0))
475 return 0;
476 return 1;
477 }
478 if (flags & DEF_NONLOCAL) {
479 if (flags & DEF_PARAM) {
480 PyErr_Format(PyExc_SyntaxError,
481 "name '%U' is parameter and nonlocal",
482 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400483 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 }
485 if (!bound) {
486 PyErr_Format(PyExc_SyntaxError,
487 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400488 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 }
490 if (!PySet_Contains(bound, name)) {
491 PyErr_Format(PyExc_SyntaxError,
492 "no binding for nonlocal '%U' found",
493 name);
494
Benjamin Petersond9c87022012-10-31 20:26:20 -0400495 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 }
497 SET_SCOPE(scopes, name, FREE);
498 ste->ste_free = 1;
499 return PySet_Add(free, name) >= 0;
500 }
501 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000502 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (PySet_Add(local, name) < 0)
504 return 0;
505 if (PySet_Discard(global, name) < 0)
506 return 0;
507 return 1;
508 }
509 /* If an enclosing block has a binding for this name, it
510 is a free variable rather than a global variable.
511 Note that having a non-NULL bound implies that the block
512 is nested.
513 */
514 if (bound && PySet_Contains(bound, name)) {
515 SET_SCOPE(scopes, name, FREE);
516 ste->ste_free = 1;
517 return PySet_Add(free, name) >= 0;
518 }
519 /* If a parent has a global statement, then call it global
520 explicit? It could also be global implicit.
521 */
522 if (global && PySet_Contains(global, name)) {
523 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
524 return 1;
525 }
526 if (ste->ste_nested)
527 ste->ste_free = 1;
528 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
529 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530}
531
532#undef SET_SCOPE
533
534/* If a name is defined in free and also in locals, then this block
535 provides the binding for the free variable. The name should be
536 marked CELL in this block and removed from the free list.
537
538 Note that the current block's free variables are included in free.
539 That's safe because no name can be free and local in the same scope.
540*/
541
542static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500543analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyObject *name, *v, *v_cell;
546 int success = 0;
547 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 v_cell = PyLong_FromLong(CELL);
550 if (!v_cell)
551 return 0;
552 while (PyDict_Next(scopes, &pos, &name, &v)) {
553 long scope;
554 assert(PyLong_Check(v));
555 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000556 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 continue;
558 if (!PySet_Contains(free, name))
559 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* Replace LOCAL with CELL for this name, and remove
561 from free. It is safe to replace the value of name
562 in the dict, because it will not cause a resize.
563 */
564 if (PyDict_SetItem(scopes, name, v_cell) < 0)
565 goto error;
566 if (PySet_Discard(free, name) < 0)
567 goto error;
568 }
569 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 Py_DECREF(v_cell);
572 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573}
574
Benjamin Peterson312595c2013-05-15 15:26:42 -0500575static int
576drop_class_free(PySTEntryObject *ste, PyObject *free)
577{
578 int res;
579 if (!GET_IDENTIFIER(__class__))
580 return 0;
581 res = PySet_Discard(free, __class__);
582 if (res < 0)
583 return 0;
584 if (res)
585 ste->ste_needs_class_closure = 1;
586 return 1;
587}
588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589/* Enter the final scope information into the ste_symbols dict.
590 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591 * All arguments are dicts. Modifies symbols, others are read-only.
592*/
593static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 PyObject *name = NULL, *itr = NULL;
598 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
599 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 /* Update scope information for all symbols in this scope */
602 while (PyDict_Next(symbols, &pos, &name, &v)) {
603 long scope, flags;
604 assert(PyLong_Check(v));
605 flags = PyLong_AS_LONG(v);
606 v_scope = PyDict_GetItem(scopes, name);
607 assert(v_scope && PyLong_Check(v_scope));
608 scope = PyLong_AS_LONG(v_scope);
609 flags |= (scope << SCOPE_OFFSET);
610 v_new = PyLong_FromLong(flags);
611 if (!v_new)
612 return 0;
613 if (PyDict_SetItem(symbols, name, v_new) < 0) {
614 Py_DECREF(v_new);
615 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 Py_DECREF(v_new);
618 }
619
620 /* Record not yet resolved free variables from children (if any) */
621 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
622 if (!v_free)
623 return 0;
624
625 itr = PyObject_GetIter(free);
626 if (!itr)
627 goto error;
628
629 while ((name = PyIter_Next(itr))) {
630 v = PyDict_GetItem(symbols, name);
631
632 /* Handle symbol that already exists in this scope */
633 if (v) {
634 /* Handle a free variable in a method of
635 the class that has the same name as a local
636 or global in the class scope.
637 */
638 if (classflag &&
639 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
640 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
641 v_new = PyLong_FromLong(flags);
642 if (!v_new) {
643 goto error;
644 }
645 if (PyDict_SetItem(symbols, name, v_new) < 0) {
646 Py_DECREF(v_new);
647 goto error;
648 }
649 Py_DECREF(v_new);
650 }
651 /* It's a cell, or already free in this scope */
652 Py_DECREF(name);
653 continue;
654 }
655 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200656 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 Py_DECREF(name);
658 continue; /* it's a global */
659 }
660 /* Propagate new free symbol up the lexical stack */
661 if (PyDict_SetItem(symbols, name, v_free) < 0) {
662 goto error;
663 }
664 Py_DECREF(name);
665 }
666 Py_DECREF(itr);
667 Py_DECREF(v_free);
668 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000669error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 Py_XDECREF(v_free);
671 Py_XDECREF(itr);
672 Py_XDECREF(name);
673 return 0;
674}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675
676/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000677
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678 Arguments:
679 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000680 bound -- set of variables bound in enclosing scopes (input). bound
681 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 free -- set of free variables in enclosed scopes (output)
683 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000684
685 The implementation uses two mutually recursive functions,
686 analyze_block() and analyze_child_block(). analyze_block() is
687 responsible for analyzing the individual names defined in a block.
688 analyze_child_block() prepares temporary namespace dictionaries
689 used to evaluated nested blocks.
690
691 The two functions exist because a child block should see the name
692 bindings of its enclosing blocks, but those bindings should not
693 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694*/
695
696static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
698 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000699
700static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
702 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
705 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
706 PyObject *temp;
707 int i, success = 0;
708 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 local = PySet_New(NULL); /* collect new names bound in block */
711 if (!local)
712 goto error;
713 scopes = PyDict_New(); /* collect scopes defined for each name */
714 if (!scopes)
715 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 /* Allocate new global and bound variable dictionaries. These
718 dictionaries hold the names visible in nested blocks. For
719 ClassBlocks, the bound and global names are initialized
720 before analyzing names, because class bindings aren't
721 visible in methods. For other blocks, they are initialized
722 after names are analyzed.
723 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 /* TODO(jhylton): Package these dicts in a struct so that we
726 can write reasonable helper functions?
727 */
728 newglobal = PySet_New(NULL);
729 if (!newglobal)
730 goto error;
731 newfree = PySet_New(NULL);
732 if (!newfree)
733 goto error;
734 newbound = PySet_New(NULL);
735 if (!newbound)
736 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 /* Class namespace has no effect on names visible in
739 nested functions, so populate the global and bound
740 sets to be passed to child blocks before analyzing
741 this one.
742 */
743 if (ste->ste_type == ClassBlock) {
744 /* Pass down known globals */
745 temp = PyNumber_InPlaceOr(newglobal, global);
746 if (!temp)
747 goto error;
748 Py_DECREF(temp);
749 /* Pass down previously bound symbols */
750 if (bound) {
751 temp = PyNumber_InPlaceOr(newbound, bound);
752 if (!temp)
753 goto error;
754 Py_DECREF(temp);
755 }
756 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
759 long flags = PyLong_AS_LONG(v);
760 if (!analyze_name(ste, scopes, name, flags,
761 bound, local, free, global))
762 goto error;
763 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 /* Populate global and bound sets to be passed to children. */
766 if (ste->ste_type != ClassBlock) {
767 /* Add function locals to bound set */
768 if (ste->ste_type == FunctionBlock) {
769 temp = PyNumber_InPlaceOr(newbound, local);
770 if (!temp)
771 goto error;
772 Py_DECREF(temp);
773 }
774 /* Pass down previously bound symbols */
775 if (bound) {
776 temp = PyNumber_InPlaceOr(newbound, bound);
777 if (!temp)
778 goto error;
779 Py_DECREF(temp);
780 }
781 /* Pass down known globals */
782 temp = PyNumber_InPlaceOr(newglobal, global);
783 if (!temp)
784 goto error;
785 Py_DECREF(temp);
786 }
787 else {
788 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000789 if (!GET_IDENTIFIER(__class__))
790 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 if (PySet_Add(newbound, __class__) < 0)
792 goto error;
793 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300795 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 newbound, newglobal now contain the names visible in
798 nested blocks. The free variables in the children will
799 be collected in allfree.
800 */
801 allfree = PySet_New(NULL);
802 if (!allfree)
803 goto error;
804 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
805 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
806 PySTEntryObject* entry;
807 assert(c && PySTEntry_Check(c));
808 entry = (PySTEntryObject*)c;
809 if (!analyze_child_block(entry, newbound, newfree, newglobal,
810 allfree))
811 goto error;
812 /* Check if any children have free variables */
813 if (entry->ste_free || entry->ste_child_free)
814 ste->ste_child_free = 1;
815 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 temp = PyNumber_InPlaceOr(newfree, allfree);
818 if (!temp)
819 goto error;
820 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500823 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500825 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 goto error;
827 /* Records the results of the analysis in the symbol table entry */
828 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
829 ste->ste_type == ClassBlock))
830 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 temp = PyNumber_InPlaceOr(free, newfree);
833 if (!temp)
834 goto error;
835 Py_DECREF(temp);
836 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 Py_XDECREF(scopes);
839 Py_XDECREF(local);
840 Py_XDECREF(newbound);
841 Py_XDECREF(newglobal);
842 Py_XDECREF(newfree);
843 Py_XDECREF(allfree);
844 if (!success)
845 assert(PyErr_Occurred());
846 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847}
848
849static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
851 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
854 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000857
Martin Panter3ee62702016-06-04 04:57:19 +0000858 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 current block. The analyze_block() call modifies these
860 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 */
863 temp_bound = PySet_New(bound);
864 if (!temp_bound)
865 goto error;
866 temp_free = PySet_New(free);
867 if (!temp_free)
868 goto error;
869 temp_global = PySet_New(global);
870 if (!temp_global)
871 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
874 goto error;
875 temp = PyNumber_InPlaceOr(child_free, temp_free);
876 if (!temp)
877 goto error;
878 Py_DECREF(temp);
879 Py_DECREF(temp_bound);
880 Py_DECREF(temp_free);
881 Py_DECREF(temp_global);
882 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000883 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 Py_XDECREF(temp_bound);
885 Py_XDECREF(temp_free);
886 Py_XDECREF(temp_global);
887 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000888}
889
890static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891symtable_analyze(struct symtable *st)
892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 PyObject *free, *global;
894 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 free = PySet_New(NULL);
897 if (!free)
898 return 0;
899 global = PySet_New(NULL);
900 if (!global) {
901 Py_DECREF(free);
902 return 0;
903 }
904 r = analyze_block(st->st_top, NULL, free, global);
905 Py_DECREF(free);
906 Py_DECREF(global);
907 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908}
909
910
911static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200912symtable_warn(struct symtable *st, const char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913{
Victor Stinner14e461d2013-08-26 22:28:21 +0200914 PyObject *message = PyUnicode_FromString(msg);
915 if (message == NULL)
916 return 0;
917 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, message, st->st_filename,
918 lineno, NULL, NULL) < 0) {
919 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
921 PyErr_SetString(PyExc_SyntaxError, msg);
Victor Stinner14e461d2013-08-26 22:28:21 +0200922 PyErr_SyntaxLocationObject(st->st_filename, st->st_cur->ste_lineno,
923 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 }
925 return 0;
926 }
Victor Stinner14e461d2013-08-26 22:28:21 +0200927 Py_DECREF(message);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929}
930
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000931/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 This reference is released when the block is exited, via the DECREF
933 in symtable_exit_block().
934*/
935
936static int
937symtable_exit_block(struct symtable *st, void *ast)
938{
Benjamin Peterson609da582011-06-29 22:52:39 -0500939 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940
Benjamin Peterson609da582011-06-29 22:52:39 -0500941 st->st_cur = NULL;
942 size = PyList_GET_SIZE(st->st_stack);
943 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500944 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500946 if (--size)
947 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 }
949 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950}
951
952static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000954 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955{
Benjamin Peterson609da582011-06-29 22:52:39 -0500956 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957
Benjamin Peterson609da582011-06-29 22:52:39 -0500958 ste = ste_new(st, name, block, ast, lineno, col_offset);
959 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500961 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
962 Py_DECREF(ste);
963 return 0;
964 }
965 prev = st->st_cur;
966 /* The entry is owned by the stack. Borrow it for st_cur. */
967 Py_DECREF(ste);
968 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000969 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 st->st_global = st->st_cur->ste_symbols;
971 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500972 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 return 0;
974 }
975 }
976 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977}
978
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000979static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980symtable_lookup(struct symtable *st, PyObject *name)
981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 PyObject *o;
983 PyObject *mangled = _Py_Mangle(st->st_private, name);
984 if (!mangled)
985 return 0;
986 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
987 Py_DECREF(mangled);
988 if (!o)
989 return 0;
990 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991}
992
993static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 PyObject *o;
997 PyObject *dict;
998 long val;
999 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000
Jeremy Hylton81e95022007-02-27 06:50:52 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 if (!mangled)
1003 return 0;
1004 dict = st->st_cur->ste_symbols;
1005 if ((o = PyDict_GetItem(dict, mangled))) {
1006 val = PyLong_AS_LONG(o);
1007 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1008 /* Is it better to use 'mangled' or 'name' here? */
1009 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001010 PyErr_SyntaxLocationObject(st->st_filename,
1011 st->st_cur->ste_lineno,
1012 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 goto error;
1014 }
1015 val |= flag;
1016 } else
1017 val = flag;
1018 o = PyLong_FromLong(val);
1019 if (o == NULL)
1020 goto error;
1021 if (PyDict_SetItem(dict, mangled, o) < 0) {
1022 Py_DECREF(o);
1023 goto error;
1024 }
1025 Py_DECREF(o);
1026
1027 if (flag & DEF_PARAM) {
1028 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1029 goto error;
1030 } else if (flag & DEF_GLOBAL) {
1031 /* XXX need to update DEF_GLOBAL for other flags too;
1032 perhaps only DEF_FREE_GLOBAL */
1033 val = flag;
1034 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1035 val |= PyLong_AS_LONG(o);
1036 }
1037 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 goto error;
1040 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1041 Py_DECREF(o);
1042 goto error;
1043 }
1044 Py_DECREF(o);
1045 }
1046 Py_DECREF(mangled);
1047 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001048
1049error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 Py_DECREF(mangled);
1051 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052}
1053
1054/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1055 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 function.
1057
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1059 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001060
1061 VISIT_QUIT macro returns the specified value exiting from the function but
1062 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063*/
1064
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001065#define VISIT_QUIT(ST, X) \
1066 return --(ST)->recursion_depth,(X)
1067
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001070 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001071
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 int i; \
1074 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1075 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1076 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1077 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001078 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 int i; \
1084 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1085 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1086 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1087 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001088 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001091
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001092#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001094 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001096 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001098 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001099 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001101}
1102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001104symtable_new_tmpname(struct symtable *st)
1105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 char tmpname[256];
1107 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1110 ++st->st_cur->ste_tmpname);
1111 tmp = PyUnicode_InternFromString(tmpname);
1112 if (!tmp)
1113 return 0;
1114 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1115 return 0;
1116 Py_DECREF(tmp);
1117 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001118}
1119
Guido van Rossum4f72a782006-10-27 23:31:49 +00001120
Guido van Rossumc2e20742006-02-27 22:32:47 +00001121static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001122symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1123{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001124 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001125 int res;
1126 if (!st->st_cur->ste_directives) {
1127 st->st_cur->ste_directives = PyList_New(0);
1128 if (!st->st_cur->ste_directives)
1129 return 0;
1130 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001131 mangled = _Py_Mangle(st->st_private, name);
1132 if (!mangled)
1133 return 0;
1134 data = Py_BuildValue("(Nii)", mangled, s->lineno, s->col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001135 if (!data)
1136 return 0;
1137 res = PyList_Append(st->st_cur->ste_directives, data);
1138 Py_DECREF(data);
1139 return res == 0;
1140}
1141
1142
1143static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144symtable_visit_stmt(struct symtable *st, stmt_ty s)
1145{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001146 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001147 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001148 "maximum recursion depth exceeded during compilation");
1149 VISIT_QUIT(st, 0);
1150 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 switch (s->kind) {
1152 case FunctionDef_kind:
1153 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001154 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (s->v.FunctionDef.args->defaults)
1156 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1157 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001158 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001159 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1160 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001161 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (s->v.FunctionDef.decorator_list)
1163 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1164 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001165 FunctionBlock, (void *)s, s->lineno,
1166 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001167 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001168 VISIT(st, arguments, s->v.FunctionDef.args);
1169 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001171 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 break;
1173 case ClassDef_kind: {
1174 PyObject *tmp;
1175 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001176 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1178 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (s->v.ClassDef.decorator_list)
1180 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1181 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001182 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001183 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 tmp = st->st_private;
1185 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001186 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 st->st_private = tmp;
1188 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001189 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 break;
1191 }
1192 case Return_kind:
1193 if (s->v.Return.value) {
1194 VISIT(st, expr, s->v.Return.value);
1195 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 }
1197 break;
1198 case Delete_kind:
1199 VISIT_SEQ(st, expr, s->v.Delete.targets);
1200 break;
1201 case Assign_kind:
1202 VISIT_SEQ(st, expr, s->v.Assign.targets);
1203 VISIT(st, expr, s->v.Assign.value);
1204 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001205 case AnnAssign_kind:
1206 if (s->v.AnnAssign.target->kind == Name_kind) {
1207 expr_ty e_name = s->v.AnnAssign.target;
1208 long cur = symtable_lookup(st, e_name->v.Name.id);
1209 if (cur < 0) {
1210 VISIT_QUIT(st, 0);
1211 }
1212 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
1213 && s->v.AnnAssign.simple) {
1214 PyErr_Format(PyExc_SyntaxError,
1215 "annotated name '%U' can't be %s",
1216 e_name->v.Name.id,
1217 cur & DEF_GLOBAL ? "global" : "nonlocal");
1218 PyErr_SyntaxLocationObject(st->st_filename,
1219 s->lineno,
1220 s->col_offset);
1221 VISIT_QUIT(st, 0);
1222 }
1223 if (s->v.AnnAssign.simple &&
1224 !symtable_add_def(st, e_name->v.Name.id,
1225 DEF_ANNOT | DEF_LOCAL)) {
1226 VISIT_QUIT(st, 0);
1227 }
1228 else {
1229 if (s->v.AnnAssign.value
1230 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1231 VISIT_QUIT(st, 0);
1232 }
1233 }
1234 }
1235 else {
1236 VISIT(st, expr, s->v.AnnAssign.target);
1237 }
1238 VISIT(st, expr, s->v.AnnAssign.annotation);
1239 if (s->v.AnnAssign.value) {
1240 VISIT(st, expr, s->v.AnnAssign.value);
1241 }
1242 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 case AugAssign_kind:
1244 VISIT(st, expr, s->v.AugAssign.target);
1245 VISIT(st, expr, s->v.AugAssign.value);
1246 break;
1247 case For_kind:
1248 VISIT(st, expr, s->v.For.target);
1249 VISIT(st, expr, s->v.For.iter);
1250 VISIT_SEQ(st, stmt, s->v.For.body);
1251 if (s->v.For.orelse)
1252 VISIT_SEQ(st, stmt, s->v.For.orelse);
1253 break;
1254 case While_kind:
1255 VISIT(st, expr, s->v.While.test);
1256 VISIT_SEQ(st, stmt, s->v.While.body);
1257 if (s->v.While.orelse)
1258 VISIT_SEQ(st, stmt, s->v.While.orelse);
1259 break;
1260 case If_kind:
1261 /* XXX if 0: and lookup_yield() hacks */
1262 VISIT(st, expr, s->v.If.test);
1263 VISIT_SEQ(st, stmt, s->v.If.body);
1264 if (s->v.If.orelse)
1265 VISIT_SEQ(st, stmt, s->v.If.orelse);
1266 break;
1267 case Raise_kind:
1268 if (s->v.Raise.exc) {
1269 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001270 if (s->v.Raise.cause) {
1271 VISIT(st, expr, s->v.Raise.cause);
1272 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 }
1274 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001275 case Try_kind:
1276 VISIT_SEQ(st, stmt, s->v.Try.body);
1277 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1278 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1279 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 break;
1281 case Assert_kind:
1282 VISIT(st, expr, s->v.Assert.test);
1283 if (s->v.Assert.msg)
1284 VISIT(st, expr, s->v.Assert.msg);
1285 break;
1286 case Import_kind:
1287 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 break;
1289 case ImportFrom_kind:
1290 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 break;
1292 case Global_kind: {
1293 int i;
1294 asdl_seq *seq = s->v.Global.names;
1295 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1296 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 long cur = symtable_lookup(st, name);
1298 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001299 VISIT_QUIT(st, 0);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001300 if (cur & DEF_ANNOT) {
1301 PyErr_Format(PyExc_SyntaxError,
1302 "annotated name '%U' can't be global",
1303 name);
1304 PyErr_SyntaxLocationObject(st->st_filename,
1305 s->lineno,
1306 s->col_offset);
1307 VISIT_QUIT(st, 0);
1308 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 if (cur & (DEF_LOCAL | USE)) {
1310 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001311 char *c_name = _PyUnicode_AsString(name);
1312 if (!c_name)
1313 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 if (cur & DEF_LOCAL)
1315 PyOS_snprintf(buf, sizeof(buf),
1316 GLOBAL_AFTER_ASSIGN,
1317 c_name);
1318 else
1319 PyOS_snprintf(buf, sizeof(buf),
1320 GLOBAL_AFTER_USE,
1321 c_name);
1322 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001323 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 }
1325 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001326 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001327 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001328 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 }
1330 break;
1331 }
1332 case Nonlocal_kind: {
1333 int i;
1334 asdl_seq *seq = s->v.Nonlocal.names;
1335 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1336 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 long cur = symtable_lookup(st, name);
1338 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001339 VISIT_QUIT(st, 0);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001340 if (cur & DEF_ANNOT) {
1341 PyErr_Format(PyExc_SyntaxError,
1342 "annotated name '%U' can't be nonlocal",
1343 name);
1344 PyErr_SyntaxLocationObject(st->st_filename,
1345 s->lineno,
1346 s->col_offset);
1347 VISIT_QUIT(st, 0);
1348 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 if (cur & (DEF_LOCAL | USE)) {
1350 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001351 char *c_name = _PyUnicode_AsString(name);
1352 if (!c_name)
1353 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (cur & DEF_LOCAL)
1355 PyOS_snprintf(buf, sizeof(buf),
1356 NONLOCAL_AFTER_ASSIGN,
1357 c_name);
1358 else
1359 PyOS_snprintf(buf, sizeof(buf),
1360 NONLOCAL_AFTER_USE,
1361 c_name);
1362 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001363 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 }
1365 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001366 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001367 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001368 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 }
1370 break;
1371 }
1372 case Expr_kind:
1373 VISIT(st, expr, s->v.Expr.value);
1374 break;
1375 case Pass_kind:
1376 case Break_kind:
1377 case Continue_kind:
1378 /* nothing to do here */
1379 break;
1380 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001381 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 VISIT_SEQ(st, stmt, s->v.With.body);
1383 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001384 case AsyncFunctionDef_kind:
1385 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1386 VISIT_QUIT(st, 0);
1387 if (s->v.AsyncFunctionDef.args->defaults)
1388 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1389 if (s->v.AsyncFunctionDef.args->kw_defaults)
1390 VISIT_SEQ_WITH_NULL(st, expr,
1391 s->v.AsyncFunctionDef.args->kw_defaults);
1392 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1393 s->v.AsyncFunctionDef.returns))
1394 VISIT_QUIT(st, 0);
1395 if (s->v.AsyncFunctionDef.decorator_list)
1396 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1397 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1398 FunctionBlock, (void *)s, s->lineno,
1399 s->col_offset))
1400 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001401 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001402 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1403 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1404 if (!symtable_exit_block(st, s))
1405 VISIT_QUIT(st, 0);
1406 break;
1407 case AsyncWith_kind:
1408 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1409 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1410 break;
1411 case AsyncFor_kind:
1412 VISIT(st, expr, s->v.AsyncFor.target);
1413 VISIT(st, expr, s->v.AsyncFor.iter);
1414 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1415 if (s->v.AsyncFor.orelse)
1416 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1417 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001419 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420}
1421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423symtable_visit_expr(struct symtable *st, expr_ty e)
1424{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001425 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001426 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001427 "maximum recursion depth exceeded during compilation");
1428 VISIT_QUIT(st, 0);
1429 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 switch (e->kind) {
1431 case BoolOp_kind:
1432 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1433 break;
1434 case BinOp_kind:
1435 VISIT(st, expr, e->v.BinOp.left);
1436 VISIT(st, expr, e->v.BinOp.right);
1437 break;
1438 case UnaryOp_kind:
1439 VISIT(st, expr, e->v.UnaryOp.operand);
1440 break;
1441 case Lambda_kind: {
1442 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001443 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 if (e->v.Lambda.args->defaults)
1445 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001446 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001447 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001449 FunctionBlock, (void *)e, e->lineno,
1450 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001451 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001452 VISIT(st, arguments, e->v.Lambda.args);
1453 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001455 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 break;
1457 }
1458 case IfExp_kind:
1459 VISIT(st, expr, e->v.IfExp.test);
1460 VISIT(st, expr, e->v.IfExp.body);
1461 VISIT(st, expr, e->v.IfExp.orelse);
1462 break;
1463 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001464 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 VISIT_SEQ(st, expr, e->v.Dict.values);
1466 break;
1467 case Set_kind:
1468 VISIT_SEQ(st, expr, e->v.Set.elts);
1469 break;
1470 case GeneratorExp_kind:
1471 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001472 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 break;
1474 case ListComp_kind:
1475 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001476 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 break;
1478 case SetComp_kind:
1479 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001480 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 break;
1482 case DictComp_kind:
1483 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001484 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 break;
1486 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001487 if (e->v.Yield.value)
1488 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001491 case YieldFrom_kind:
1492 VISIT(st, expr, e->v.YieldFrom.value);
1493 st->st_cur->ste_generator = 1;
1494 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001495 case Await_kind:
1496 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001497 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001498 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 case Compare_kind:
1500 VISIT(st, expr, e->v.Compare.left);
1501 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1502 break;
1503 case Call_kind:
1504 VISIT(st, expr, e->v.Call.func);
1505 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001506 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001508 case FormattedValue_kind:
1509 VISIT(st, expr, e->v.FormattedValue.value);
1510 if (e->v.FormattedValue.format_spec)
1511 VISIT(st, expr, e->v.FormattedValue.format_spec);
1512 break;
1513 case JoinedStr_kind:
1514 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1515 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001516 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 case Num_kind:
1518 case Str_kind:
1519 case Bytes_kind:
1520 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001521 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 /* Nothing to do here. */
1523 break;
1524 /* The following exprs can be assignment targets. */
1525 case Attribute_kind:
1526 VISIT(st, expr, e->v.Attribute.value);
1527 break;
1528 case Subscript_kind:
1529 VISIT(st, expr, e->v.Subscript.value);
1530 VISIT(st, slice, e->v.Subscript.slice);
1531 break;
1532 case Starred_kind:
1533 VISIT(st, expr, e->v.Starred.value);
1534 break;
1535 case Name_kind:
1536 if (!symtable_add_def(st, e->v.Name.id,
1537 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001538 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 /* Special-case super: it counts as a use of __class__ */
1540 if (e->v.Name.ctx == Load &&
1541 st->st_cur->ste_type == FunctionBlock &&
1542 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001543 if (!GET_IDENTIFIER(__class__) ||
1544 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001545 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 }
1547 break;
1548 /* child nodes of List and Tuple will have expr_context set */
1549 case List_kind:
1550 VISIT_SEQ(st, expr, e->v.List.elts);
1551 break;
1552 case Tuple_kind:
1553 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1554 break;
1555 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001556 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557}
1558
1559static int
1560symtable_implicit_arg(struct symtable *st, int pos)
1561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1563 if (id == NULL)
1564 return 0;
1565 if (!symtable_add_def(st, id, DEF_PARAM)) {
1566 Py_DECREF(id);
1567 return 0;
1568 }
1569 Py_DECREF(id);
1570 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571}
1572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001574symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 if (!args)
1579 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 for (i = 0; i < asdl_seq_LEN(args); i++) {
1582 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1583 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1584 return 0;
1585 }
1586
1587 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588}
1589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001591symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 if (!args)
1596 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 for (i = 0; i < asdl_seq_LEN(args); i++) {
1599 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1600 if (arg->annotation)
1601 VISIT(st, expr, arg->annotation);
1602 }
1603
1604 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001605}
1606
Neal Norwitzc1505362006-12-28 06:47:50 +00001607static int
Yury Selivanov75445082015-05-11 22:57:16 -04001608symtable_visit_annotations(struct symtable *st, stmt_ty s,
1609 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 if (a->args && !symtable_visit_argannotations(st, a->args))
1612 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001613 if (a->vararg && a->vararg->annotation)
1614 VISIT(st, expr, a->vararg->annotation);
1615 if (a->kwarg && a->kwarg->annotation)
1616 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1618 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001619 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001620 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622}
1623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625symtable_visit_arguments(struct symtable *st, arguments_ty a)
1626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 /* skip default arguments inside function block
1628 XXX should ast be different?
1629 */
1630 if (a->args && !symtable_visit_params(st, a->args))
1631 return 0;
1632 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1633 return 0;
1634 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001635 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 return 0;
1637 st->st_cur->ste_varargs = 1;
1638 }
1639 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001640 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 return 0;
1642 st->st_cur->ste_varkeywords = 1;
1643 }
1644 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645}
1646
1647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 if (eh->v.ExceptHandler.type)
1652 VISIT(st, expr, eh->v.ExceptHandler.type);
1653 if (eh->v.ExceptHandler.name)
1654 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1655 return 0;
1656 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1657 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658}
1659
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001660static int
1661symtable_visit_withitem(struct symtable *st, withitem_ty item)
1662{
1663 VISIT(st, expr, item->context_expr);
1664 if (item->optional_vars) {
1665 VISIT(st, expr, item->optional_vars);
1666 }
1667 return 1;
1668}
1669
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672symtable_visit_alias(struct symtable *st, alias_ty a)
1673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001675 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 dotted package name (e.g. spam.eggs)
1677 */
1678 PyObject *store_name;
1679 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001680 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1681 PyUnicode_GET_LENGTH(name), 1);
1682 if (dot != -1) {
1683 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (!store_name)
1685 return 0;
1686 }
1687 else {
1688 store_name = name;
1689 Py_INCREF(store_name);
1690 }
1691 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1692 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1693 Py_DECREF(store_name);
1694 return r;
1695 }
1696 else {
1697 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001698 int lineno = st->st_cur->ste_lineno;
1699 int col_offset = st->st_cur->ste_col_offset;
1700 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001701 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001702 Py_DECREF(store_name);
1703 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 Py_DECREF(store_name);
1706 return 1;
1707 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708}
1709
1710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 VISIT(st, expr, lc->target);
1715 VISIT(st, expr, lc->iter);
1716 VISIT_SEQ(st, expr, lc->ifs);
1717 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718}
1719
1720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722symtable_visit_keyword(struct symtable *st, keyword_ty k)
1723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 VISIT(st, expr, k->value);
1725 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726}
1727
1728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730symtable_visit_slice(struct symtable *st, slice_ty s)
1731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 switch (s->kind) {
1733 case Slice_kind:
1734 if (s->v.Slice.lower)
1735 VISIT(st, expr, s->v.Slice.lower)
1736 if (s->v.Slice.upper)
1737 VISIT(st, expr, s->v.Slice.upper)
1738 if (s->v.Slice.step)
1739 VISIT(st, expr, s->v.Slice.step)
1740 break;
1741 case ExtSlice_kind:
1742 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1743 break;
1744 case Index_kind:
1745 VISIT(st, expr, s->v.Index.value)
1746 break;
1747 }
1748 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749}
1750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001752symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001753 identifier scope_name, asdl_seq *generators,
1754 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 int is_generator = (e->kind == GeneratorExp_kind);
1757 int needs_tmp = !is_generator;
1758 comprehension_ty outermost = ((comprehension_ty)
1759 asdl_seq_GET(generators, 0));
1760 /* Outermost iterator is evaluated in current scope */
1761 VISIT(st, expr, outermost->iter);
1762 /* Create comprehension scope for the rest */
1763 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001764 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1765 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766 return 0;
1767 }
1768 st->st_cur->ste_generator = is_generator;
1769 /* Outermost iter is received as an argument */
1770 if (!symtable_implicit_arg(st, 0)) {
1771 symtable_exit_block(st, (void *)e);
1772 return 0;
1773 }
1774 /* Allocate temporary name if needed */
1775 if (needs_tmp && !symtable_new_tmpname(st)) {
1776 symtable_exit_block(st, (void *)e);
1777 return 0;
1778 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001779 VISIT(st, expr, outermost->target);
1780 VISIT_SEQ(st, expr, outermost->ifs);
1781 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001783 VISIT(st, expr, value);
1784 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001789symtable_visit_genexp(struct symtable *st, expr_ty e)
1790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1792 e->v.GeneratorExp.generators,
1793 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001794}
1795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001797symtable_visit_listcomp(struct symtable *st, expr_ty e)
1798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1800 e->v.ListComp.generators,
1801 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001802}
1803
1804static int
1805symtable_visit_setcomp(struct symtable *st, expr_ty e)
1806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1808 e->v.SetComp.generators,
1809 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001810}
1811
1812static int
1813symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1816 e->v.DictComp.generators,
1817 e->v.DictComp.key,
1818 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001819}