blob: ac14058fefd238d51b900d803528a34c7dcb1e18 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002#include "internal/pystate.h"
3#ifdef Yield
4#undef Yield /* undefine conflicting macro from winbase.h */
5#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
7#include "code.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00008#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00009#include "structmember.h"
10
Neal Norwitz5d0ad502005-12-19 04:27:42 +000011/* error strings used for warnings */
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +020012#define GLOBAL_PARAM \
13"name '%U' is parameter and global"
14
15#define NONLOCAL_PARAM \
16"name '%U' is parameter and nonlocal"
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070019"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000020
Jeremy Hylton81e95022007-02-27 06:50:52 +000021#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070022"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000023
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070025"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000026
Jeremy Hylton81e95022007-02-27 06:50:52 +000027#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070028"name '%U' is used prior to nonlocal declaration"
29
30#define GLOBAL_ANNOT \
31"annotated name '%U' can't be global"
32
33#define NONLOCAL_ANNOT \
34"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000035
Neal Norwitz5d0ad502005-12-19 04:27:42 +000036#define IMPORT_STAR_WARNING "import * only allowed at module level"
37
Neal Norwitz090b3dd2006-02-28 22:36:46 +000038static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000039ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000040 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020043 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 k = PyLong_FromVoidPtr(key);
46 if (k == NULL)
47 goto fail;
48 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020049 if (ste == NULL) {
50 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020052 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020054 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020057 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 ste->ste_symbols = NULL;
60 ste->ste_varnames = NULL;
61 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000062
Benjamin Petersond9c87022012-10-31 20:26:20 -040063 ste->ste_directives = NULL;
64
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 ste->ste_nested = 0;
67 ste->ste_free = 0;
68 ste->ste_varargs = 0;
69 ste->ste_varkeywords = 0;
70 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000071 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000072 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000074 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 if (st->st_cur != NULL &&
77 (st->st_cur->ste_nested ||
78 st->st_cur->ste_type == FunctionBlock))
79 ste->ste_nested = 1;
80 ste->ste_child_free = 0;
81 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070082 ste->ste_coroutine = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050084 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085
Victor Stinner9a4fb662013-07-11 22:49:00 +020086 ste->ste_symbols = PyDict_New();
87 ste->ste_varnames = PyList_New(0);
88 ste->ste_children = PyList_New(0);
89 if (ste->ste_symbols == NULL
90 || ste->ste_varnames == NULL
91 || ste->ste_children == NULL)
92 goto fail;
93
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
95 goto fail;
96
97 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000098 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 Py_XDECREF(ste);
100 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000101}
102
103static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000104ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
107 ste->ste_name,
108 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109}
110
111static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 ste->ste_table = NULL;
115 Py_XDECREF(ste->ste_id);
116 Py_XDECREF(ste->ste_name);
117 Py_XDECREF(ste->ste_symbols);
118 Py_XDECREF(ste->ste_varnames);
119 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400120 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000122}
123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000125
Guido van Rossum6f799372001-09-20 20:46:19 +0000126static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 {"id", T_OBJECT, OFF(ste_id), READONLY},
128 {"name", T_OBJECT, OFF(ste_name), READONLY},
129 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
130 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
131 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 {"nested", T_INT, OFF(ste_nested), READONLY},
133 {"type", T_INT, OFF(ste_type), READONLY},
134 {"lineno", T_INT, OFF(ste_lineno), READONLY},
135 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000136};
137
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000138PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 PyVarObject_HEAD_INIT(&PyType_Type, 0)
140 "symtable entry",
141 sizeof(PySTEntryObject),
142 0,
143 (destructor)ste_dealloc, /* tp_dealloc */
144 0, /* tp_print */
145 0, /* tp_getattr */
146 0, /* tp_setattr */
147 0, /* tp_reserved */
148 (reprfunc)ste_repr, /* tp_repr */
149 0, /* tp_as_number */
150 0, /* tp_as_sequence */
151 0, /* tp_as_mapping */
152 0, /* tp_hash */
153 0, /* tp_call */
154 0, /* tp_str */
155 PyObject_GenericGetAttr, /* tp_getattro */
156 0, /* tp_setattro */
157 0, /* tp_as_buffer */
158 Py_TPFLAGS_DEFAULT, /* tp_flags */
159 0, /* tp_doc */
160 0, /* tp_traverse */
161 0, /* tp_clear */
162 0, /* tp_richcompare */
163 0, /* tp_weaklistoffset */
164 0, /* tp_iter */
165 0, /* tp_iternext */
166 0, /* tp_methods */
167 ste_memberlist, /* tp_members */
168 0, /* tp_getset */
169 0, /* tp_base */
170 0, /* tp_dict */
171 0, /* tp_descr_get */
172 0, /* tp_descr_set */
173 0, /* tp_dictoffset */
174 0, /* tp_init */
175 0, /* tp_alloc */
176 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000177};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178
179static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000181 _Py_block_ty block, void *ast, int lineno,
182 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183static int symtable_exit_block(struct symtable *st, void *ast);
184static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
185static int symtable_visit_expr(struct symtable *st, expr_ty s);
186static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000187static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
188static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000189static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190static int symtable_visit_arguments(struct symtable *st, arguments_ty);
191static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
192static int symtable_visit_alias(struct symtable *st, alias_ty);
193static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
194static int symtable_visit_keyword(struct symtable *st, keyword_ty);
195static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000196static int symtable_visit_params(struct symtable *st, asdl_seq *args);
197static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198static int symtable_implicit_arg(struct symtable *st, int pos);
Yury Selivanov75445082015-05-11 22:57:16 -0400199static int symtable_visit_annotations(struct symtable *st, stmt_ty s, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500200static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
202
Nick Coghlan650f0d02007-04-15 12:05:43 +0000203static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500205 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206
207#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209
210#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000211"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212
213static struct symtable *
214symtable_new(void)
215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
219 if (st == NULL)
220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 st->st_filename = NULL;
223 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 if ((st->st_stack = PyList_New(0)) == NULL)
226 goto fail;
227 if ((st->st_blocks = PyDict_New()) == NULL)
228 goto fail;
229 st->st_cur = NULL;
230 st->st_private = NULL;
231 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 PySymtable_Free(st);
234 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235}
236
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000237/* When compiling the use of C stack is probably going to be a lot
238 lighter than when executing Python code but still can overflow
239 and causing a Python crash if not checked (e.g. eval("()"*300000)).
240 Using the current recursion limit for the compiler seems too
241 restrictive (it caused at least one test to fail) so a factor is
242 used to allow deeper recursion when compiling an expression.
243
244 Using a scaling factor means this should automatically adjust when
245 the recursion limit is adjusted for small or large C stack allocations.
246*/
247#define COMPILER_STACK_FRAME_SCALE 3
248
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200250PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000251{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000252 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 asdl_seq *seq;
254 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000255 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400256 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200259 return NULL;
260 if (filename == NULL) {
261 PySymtable_Free(st);
262 return NULL;
263 }
264 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 st->st_filename = filename;
266 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000267
268 /* Setup recursion depth check counters */
269 tstate = PyThreadState_GET();
270 if (!tstate) {
271 PySymtable_Free(st);
272 return NULL;
273 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400274 /* Be careful here to prevent overflow. */
275 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
276 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
277 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
278 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 /* Make the initial symbol information gathering pass */
281 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000282 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 PySymtable_Free(st);
284 return NULL;
285 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 switch (mod->kind) {
289 case Module_kind:
290 seq = mod->v.Module.body;
291 for (i = 0; i < asdl_seq_LEN(seq); i++)
292 if (!symtable_visit_stmt(st,
293 (stmt_ty)asdl_seq_GET(seq, i)))
294 goto error;
295 break;
296 case Expression_kind:
297 if (!symtable_visit_expr(st, mod->v.Expression.body))
298 goto error;
299 break;
300 case Interactive_kind:
301 seq = mod->v.Interactive.body;
302 for (i = 0; i < asdl_seq_LEN(seq); i++)
303 if (!symtable_visit_stmt(st,
304 (stmt_ty)asdl_seq_GET(seq, i)))
305 goto error;
306 break;
307 case Suite_kind:
308 PyErr_SetString(PyExc_RuntimeError,
309 "this compiler does not handle Suites");
310 goto error;
311 }
312 if (!symtable_exit_block(st, (void *)mod)) {
313 PySymtable_Free(st);
314 return NULL;
315 }
316 /* Make the second symbol analysis pass */
317 if (symtable_analyze(st))
318 return st;
319 PySymtable_Free(st);
320 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 (void) symtable_exit_block(st, (void *)mod);
323 PySymtable_Free(st);
324 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325}
326
Victor Stinner14e461d2013-08-26 22:28:21 +0200327struct symtable *
328PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
329{
330 PyObject *filename;
331 struct symtable *st;
332 filename = PyUnicode_DecodeFSDefault(filename_str);
333 if (filename == NULL)
334 return NULL;
335 st = PySymtable_BuildObject(mod, filename, future);
336 Py_DECREF(filename);
337 return st;
338}
339
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340void
341PySymtable_Free(struct symtable *st)
342{
Victor Stinner14e461d2013-08-26 22:28:21 +0200343 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 Py_XDECREF(st->st_blocks);
345 Py_XDECREF(st->st_stack);
346 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347}
348
349PySTEntryObject *
350PySymtable_Lookup(struct symtable *st, void *key)
351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 k = PyLong_FromVoidPtr(key);
355 if (k == NULL)
356 return NULL;
357 v = PyDict_GetItem(st->st_blocks, k);
358 if (v) {
359 assert(PySTEntry_Check(v));
360 Py_INCREF(v);
361 }
362 else {
363 PyErr_SetString(PyExc_KeyError,
364 "unknown symbol table entry");
365 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 Py_DECREF(k);
368 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369}
370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372PyST_GetScope(PySTEntryObject *ste, PyObject *name)
373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
375 if (!v)
376 return 0;
377 assert(PyLong_Check(v));
378 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379}
380
Benjamin Petersond9c87022012-10-31 20:26:20 -0400381static int
382error_at_directive(PySTEntryObject *ste, PyObject *name)
383{
384 Py_ssize_t i;
385 PyObject *data;
386 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600387 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400388 data = PyList_GET_ITEM(ste->ste_directives, i);
389 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600390 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
391 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
392 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
393 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
394 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
395
396 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700397 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400398 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600399 PyErr_SetString(PyExc_RuntimeError,
400 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400401 return 0;
402}
403
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404
405/* Analyze raw symbol information to determine scope of each name.
406
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000407 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000408 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000413 explicit global is declared with the global statement. An implicit
414 global is a free variable for which the compiler has found no binding
415 in an enclosing function scope. The implicit global is either a global
416 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
417 to handle these names to implement slightly odd semantics. In such a
418 block, the name is treated as global until it is assigned to; then it
419 is treated as a local.
420
421 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000422 The first pass collects raw facts from the AST via the symtable_visit_*
423 functions: the name is a parameter here, the name is used but not defined
424 here, etc. The second pass analyzes these facts during a pass over the
425 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426
427 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000429 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000430 Names which are explicitly declared nonlocal must exist in this set of
431 visible names - if they do not, a syntax error is raised. After doing
432 the local analysis, it analyzes each of its child blocks using an
433 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434
Nick Coghlan650f0d02007-04-15 12:05:43 +0000435 The children update the free variable set. If a local variable is added to
436 the free variable set by the child, the variable is marked as a cell. The
437 function object being defined must provide runtime storage for the variable
438 that may outlive the function's frame. Cell variables are removed from the
439 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000440
Nick Coghlan650f0d02007-04-15 12:05:43 +0000441 During analysis, the names are:
442 symbols: dict mapping from symbol names to flag values (including offset scope values)
443 scopes: dict mapping from symbol names to scope values (no offset)
444 local: set of all symbol names local to the current scope
445 bound: set of all symbol names local to a containing function scope
446 free: set of all symbol names referenced but not bound in child scopes
447 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000448*/
449
450#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 PyObject *o = PyLong_FromLong(I); \
452 if (!o) \
453 return 0; \
454 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
455 Py_DECREF(o); \
456 return 0; \
457 } \
458 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000459}
460
461/* Decide on scope of name, given flags.
462
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000463 The namespace dictionaries may be modified to record information
464 about the new name. For example, a new global will add an entry to
465 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466*/
467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000469analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyObject *bound, PyObject *local, PyObject *free,
471 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (flags & DEF_NONLOCAL) {
475 PyErr_Format(PyExc_SyntaxError,
476 "name '%U' is nonlocal and global",
477 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400478 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 }
480 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
481 if (PySet_Add(global, name) < 0)
482 return 0;
483 if (bound && (PySet_Discard(bound, name) < 0))
484 return 0;
485 return 1;
486 }
487 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (!bound) {
489 PyErr_Format(PyExc_SyntaxError,
490 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400491 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 }
493 if (!PySet_Contains(bound, name)) {
494 PyErr_Format(PyExc_SyntaxError,
495 "no binding for nonlocal '%U' found",
496 name);
497
Benjamin Petersond9c87022012-10-31 20:26:20 -0400498 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 }
500 SET_SCOPE(scopes, name, FREE);
501 ste->ste_free = 1;
502 return PySet_Add(free, name) >= 0;
503 }
504 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000505 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 if (PySet_Add(local, name) < 0)
507 return 0;
508 if (PySet_Discard(global, name) < 0)
509 return 0;
510 return 1;
511 }
512 /* If an enclosing block has a binding for this name, it
513 is a free variable rather than a global variable.
514 Note that having a non-NULL bound implies that the block
515 is nested.
516 */
517 if (bound && PySet_Contains(bound, name)) {
518 SET_SCOPE(scopes, name, FREE);
519 ste->ste_free = 1;
520 return PySet_Add(free, name) >= 0;
521 }
522 /* If a parent has a global statement, then call it global
523 explicit? It could also be global implicit.
524 */
525 if (global && PySet_Contains(global, name)) {
526 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
527 return 1;
528 }
529 if (ste->ste_nested)
530 ste->ste_free = 1;
531 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
532 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533}
534
535#undef SET_SCOPE
536
537/* If a name is defined in free and also in locals, then this block
538 provides the binding for the free variable. The name should be
539 marked CELL in this block and removed from the free list.
540
541 Note that the current block's free variables are included in free.
542 That's safe because no name can be free and local in the same scope.
543*/
544
545static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500546analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 PyObject *name, *v, *v_cell;
549 int success = 0;
550 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 v_cell = PyLong_FromLong(CELL);
553 if (!v_cell)
554 return 0;
555 while (PyDict_Next(scopes, &pos, &name, &v)) {
556 long scope;
557 assert(PyLong_Check(v));
558 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000559 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 continue;
561 if (!PySet_Contains(free, name))
562 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 /* Replace LOCAL with CELL for this name, and remove
564 from free. It is safe to replace the value of name
565 in the dict, because it will not cause a resize.
566 */
567 if (PyDict_SetItem(scopes, name, v_cell) < 0)
568 goto error;
569 if (PySet_Discard(free, name) < 0)
570 goto error;
571 }
572 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 Py_DECREF(v_cell);
575 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576}
577
Benjamin Peterson312595c2013-05-15 15:26:42 -0500578static int
579drop_class_free(PySTEntryObject *ste, PyObject *free)
580{
581 int res;
582 if (!GET_IDENTIFIER(__class__))
583 return 0;
584 res = PySet_Discard(free, __class__);
585 if (res < 0)
586 return 0;
587 if (res)
588 ste->ste_needs_class_closure = 1;
589 return 1;
590}
591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592/* Enter the final scope information into the ste_symbols dict.
593 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594 * All arguments are dicts. Modifies symbols, others are read-only.
595*/
596static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000598 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 PyObject *name = NULL, *itr = NULL;
601 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
602 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 /* Update scope information for all symbols in this scope */
605 while (PyDict_Next(symbols, &pos, &name, &v)) {
606 long scope, flags;
607 assert(PyLong_Check(v));
608 flags = PyLong_AS_LONG(v);
609 v_scope = PyDict_GetItem(scopes, name);
610 assert(v_scope && PyLong_Check(v_scope));
611 scope = PyLong_AS_LONG(v_scope);
612 flags |= (scope << SCOPE_OFFSET);
613 v_new = PyLong_FromLong(flags);
614 if (!v_new)
615 return 0;
616 if (PyDict_SetItem(symbols, name, v_new) < 0) {
617 Py_DECREF(v_new);
618 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 Py_DECREF(v_new);
621 }
622
623 /* Record not yet resolved free variables from children (if any) */
624 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
625 if (!v_free)
626 return 0;
627
628 itr = PyObject_GetIter(free);
629 if (!itr)
630 goto error;
631
632 while ((name = PyIter_Next(itr))) {
633 v = PyDict_GetItem(symbols, name);
634
635 /* Handle symbol that already exists in this scope */
636 if (v) {
637 /* Handle a free variable in a method of
638 the class that has the same name as a local
639 or global in the class scope.
640 */
641 if (classflag &&
642 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
643 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
644 v_new = PyLong_FromLong(flags);
645 if (!v_new) {
646 goto error;
647 }
648 if (PyDict_SetItem(symbols, name, v_new) < 0) {
649 Py_DECREF(v_new);
650 goto error;
651 }
652 Py_DECREF(v_new);
653 }
654 /* It's a cell, or already free in this scope */
655 Py_DECREF(name);
656 continue;
657 }
658 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200659 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 Py_DECREF(name);
661 continue; /* it's a global */
662 }
663 /* Propagate new free symbol up the lexical stack */
664 if (PyDict_SetItem(symbols, name, v_free) < 0) {
665 goto error;
666 }
667 Py_DECREF(name);
668 }
669 Py_DECREF(itr);
670 Py_DECREF(v_free);
671 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000672error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 Py_XDECREF(v_free);
674 Py_XDECREF(itr);
675 Py_XDECREF(name);
676 return 0;
677}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678
679/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000680
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681 Arguments:
682 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000683 bound -- set of variables bound in enclosing scopes (input). bound
684 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685 free -- set of free variables in enclosed scopes (output)
686 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000687
688 The implementation uses two mutually recursive functions,
689 analyze_block() and analyze_child_block(). analyze_block() is
690 responsible for analyzing the individual names defined in a block.
691 analyze_child_block() prepares temporary namespace dictionaries
692 used to evaluated nested blocks.
693
694 The two functions exist because a child block should see the name
695 bindings of its enclosing blocks, but those bindings should not
696 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697*/
698
699static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
701 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000702
703static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
705 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
708 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
709 PyObject *temp;
710 int i, success = 0;
711 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 local = PySet_New(NULL); /* collect new names bound in block */
714 if (!local)
715 goto error;
716 scopes = PyDict_New(); /* collect scopes defined for each name */
717 if (!scopes)
718 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 /* Allocate new global and bound variable dictionaries. These
721 dictionaries hold the names visible in nested blocks. For
722 ClassBlocks, the bound and global names are initialized
723 before analyzing names, because class bindings aren't
724 visible in methods. For other blocks, they are initialized
725 after names are analyzed.
726 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 /* TODO(jhylton): Package these dicts in a struct so that we
729 can write reasonable helper functions?
730 */
731 newglobal = PySet_New(NULL);
732 if (!newglobal)
733 goto error;
734 newfree = PySet_New(NULL);
735 if (!newfree)
736 goto error;
737 newbound = PySet_New(NULL);
738 if (!newbound)
739 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 /* Class namespace has no effect on names visible in
742 nested functions, so populate the global and bound
743 sets to be passed to child blocks before analyzing
744 this one.
745 */
746 if (ste->ste_type == ClassBlock) {
747 /* Pass down known globals */
748 temp = PyNumber_InPlaceOr(newglobal, global);
749 if (!temp)
750 goto error;
751 Py_DECREF(temp);
752 /* Pass down previously bound symbols */
753 if (bound) {
754 temp = PyNumber_InPlaceOr(newbound, bound);
755 if (!temp)
756 goto error;
757 Py_DECREF(temp);
758 }
759 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
762 long flags = PyLong_AS_LONG(v);
763 if (!analyze_name(ste, scopes, name, flags,
764 bound, local, free, global))
765 goto error;
766 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 /* Populate global and bound sets to be passed to children. */
769 if (ste->ste_type != ClassBlock) {
770 /* Add function locals to bound set */
771 if (ste->ste_type == FunctionBlock) {
772 temp = PyNumber_InPlaceOr(newbound, local);
773 if (!temp)
774 goto error;
775 Py_DECREF(temp);
776 }
777 /* Pass down previously bound symbols */
778 if (bound) {
779 temp = PyNumber_InPlaceOr(newbound, bound);
780 if (!temp)
781 goto error;
782 Py_DECREF(temp);
783 }
784 /* Pass down known globals */
785 temp = PyNumber_InPlaceOr(newglobal, global);
786 if (!temp)
787 goto error;
788 Py_DECREF(temp);
789 }
790 else {
791 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000792 if (!GET_IDENTIFIER(__class__))
793 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 if (PySet_Add(newbound, __class__) < 0)
795 goto error;
796 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300798 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 newbound, newglobal now contain the names visible in
801 nested blocks. The free variables in the children will
802 be collected in allfree.
803 */
804 allfree = PySet_New(NULL);
805 if (!allfree)
806 goto error;
807 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
808 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
809 PySTEntryObject* entry;
810 assert(c && PySTEntry_Check(c));
811 entry = (PySTEntryObject*)c;
812 if (!analyze_child_block(entry, newbound, newfree, newglobal,
813 allfree))
814 goto error;
815 /* Check if any children have free variables */
816 if (entry->ste_free || entry->ste_child_free)
817 ste->ste_child_free = 1;
818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 temp = PyNumber_InPlaceOr(newfree, allfree);
821 if (!temp)
822 goto error;
823 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500826 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500828 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 goto error;
830 /* Records the results of the analysis in the symbol table entry */
831 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
832 ste->ste_type == ClassBlock))
833 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 temp = PyNumber_InPlaceOr(free, newfree);
836 if (!temp)
837 goto error;
838 Py_DECREF(temp);
839 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 Py_XDECREF(scopes);
842 Py_XDECREF(local);
843 Py_XDECREF(newbound);
844 Py_XDECREF(newglobal);
845 Py_XDECREF(newfree);
846 Py_XDECREF(allfree);
847 if (!success)
848 assert(PyErr_Occurred());
849 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850}
851
852static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
854 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
857 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000860
Martin Panter3ee62702016-06-04 04:57:19 +0000861 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 current block. The analyze_block() call modifies these
863 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 */
866 temp_bound = PySet_New(bound);
867 if (!temp_bound)
868 goto error;
869 temp_free = PySet_New(free);
870 if (!temp_free)
871 goto error;
872 temp_global = PySet_New(global);
873 if (!temp_global)
874 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
877 goto error;
878 temp = PyNumber_InPlaceOr(child_free, temp_free);
879 if (!temp)
880 goto error;
881 Py_DECREF(temp);
882 Py_DECREF(temp_bound);
883 Py_DECREF(temp_free);
884 Py_DECREF(temp_global);
885 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000886 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 Py_XDECREF(temp_bound);
888 Py_XDECREF(temp_free);
889 Py_XDECREF(temp_global);
890 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000891}
892
893static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894symtable_analyze(struct symtable *st)
895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 PyObject *free, *global;
897 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 free = PySet_New(NULL);
900 if (!free)
901 return 0;
902 global = PySet_New(NULL);
903 if (!global) {
904 Py_DECREF(free);
905 return 0;
906 }
907 r = analyze_block(st->st_top, NULL, free, global);
908 Py_DECREF(free);
909 Py_DECREF(global);
910 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911}
912
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000913/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 This reference is released when the block is exited, via the DECREF
915 in symtable_exit_block().
916*/
917
918static int
919symtable_exit_block(struct symtable *st, void *ast)
920{
Benjamin Peterson609da582011-06-29 22:52:39 -0500921 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922
Benjamin Peterson609da582011-06-29 22:52:39 -0500923 st->st_cur = NULL;
924 size = PyList_GET_SIZE(st->st_stack);
925 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500926 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500928 if (--size)
929 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 }
931 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932}
933
934static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000936 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937{
Benjamin Peterson609da582011-06-29 22:52:39 -0500938 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939
Benjamin Peterson609da582011-06-29 22:52:39 -0500940 ste = ste_new(st, name, block, ast, lineno, col_offset);
941 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500943 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
944 Py_DECREF(ste);
945 return 0;
946 }
947 prev = st->st_cur;
948 /* The entry is owned by the stack. Borrow it for st_cur. */
949 Py_DECREF(ste);
950 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000951 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 st->st_global = st->st_cur->ste_symbols;
953 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500954 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 return 0;
956 }
957 }
958 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959}
960
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000961static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962symtable_lookup(struct symtable *st, PyObject *name)
963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 PyObject *o;
965 PyObject *mangled = _Py_Mangle(st->st_private, name);
966 if (!mangled)
967 return 0;
968 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
969 Py_DECREF(mangled);
970 if (!o)
971 return 0;
972 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973}
974
975static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 PyObject *o;
979 PyObject *dict;
980 long val;
981 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982
Jeremy Hylton81e95022007-02-27 06:50:52 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 if (!mangled)
985 return 0;
986 dict = st->st_cur->ste_symbols;
987 if ((o = PyDict_GetItem(dict, mangled))) {
988 val = PyLong_AS_LONG(o);
989 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
990 /* Is it better to use 'mangled' or 'name' here? */
991 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +0200992 PyErr_SyntaxLocationObject(st->st_filename,
993 st->st_cur->ste_lineno,
994 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 goto error;
996 }
997 val |= flag;
998 } else
999 val = flag;
1000 o = PyLong_FromLong(val);
1001 if (o == NULL)
1002 goto error;
1003 if (PyDict_SetItem(dict, mangled, o) < 0) {
1004 Py_DECREF(o);
1005 goto error;
1006 }
1007 Py_DECREF(o);
1008
1009 if (flag & DEF_PARAM) {
1010 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1011 goto error;
1012 } else if (flag & DEF_GLOBAL) {
1013 /* XXX need to update DEF_GLOBAL for other flags too;
1014 perhaps only DEF_FREE_GLOBAL */
1015 val = flag;
1016 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1017 val |= PyLong_AS_LONG(o);
1018 }
1019 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 goto error;
1022 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1023 Py_DECREF(o);
1024 goto error;
1025 }
1026 Py_DECREF(o);
1027 }
1028 Py_DECREF(mangled);
1029 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001030
1031error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 Py_DECREF(mangled);
1033 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034}
1035
1036/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1037 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 function.
1039
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1041 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001042
1043 VISIT_QUIT macro returns the specified value exiting from the function but
1044 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045*/
1046
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001047#define VISIT_QUIT(ST, X) \
1048 return --(ST)->recursion_depth,(X)
1049
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001052 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001053
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 int i; \
1056 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1057 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1058 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1059 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001060 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001063
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 int i; \
1066 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1067 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1068 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1069 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001070 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001073
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001074#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001076 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001078 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001080 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001081 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001083}
1084
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001086symtable_new_tmpname(struct symtable *st)
1087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 char tmpname[256];
1089 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1092 ++st->st_cur->ste_tmpname);
1093 tmp = PyUnicode_InternFromString(tmpname);
1094 if (!tmp)
1095 return 0;
1096 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1097 return 0;
1098 Py_DECREF(tmp);
1099 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001100}
1101
Guido van Rossum4f72a782006-10-27 23:31:49 +00001102
Guido van Rossumc2e20742006-02-27 22:32:47 +00001103static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001104symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1105{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001106 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001107 int res;
1108 if (!st->st_cur->ste_directives) {
1109 st->st_cur->ste_directives = PyList_New(0);
1110 if (!st->st_cur->ste_directives)
1111 return 0;
1112 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001113 mangled = _Py_Mangle(st->st_private, name);
1114 if (!mangled)
1115 return 0;
1116 data = Py_BuildValue("(Nii)", mangled, s->lineno, s->col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001117 if (!data)
1118 return 0;
1119 res = PyList_Append(st->st_cur->ste_directives, data);
1120 Py_DECREF(data);
1121 return res == 0;
1122}
1123
1124
1125static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126symtable_visit_stmt(struct symtable *st, stmt_ty s)
1127{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001128 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001129 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001130 "maximum recursion depth exceeded during compilation");
1131 VISIT_QUIT(st, 0);
1132 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 switch (s->kind) {
1134 case FunctionDef_kind:
1135 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001136 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 if (s->v.FunctionDef.args->defaults)
1138 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1139 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001140 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001141 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1142 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001143 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 if (s->v.FunctionDef.decorator_list)
1145 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1146 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001147 FunctionBlock, (void *)s, s->lineno,
1148 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001149 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001150 VISIT(st, arguments, s->v.FunctionDef.args);
1151 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001153 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 break;
1155 case ClassDef_kind: {
1156 PyObject *tmp;
1157 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001158 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1160 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (s->v.ClassDef.decorator_list)
1162 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1163 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001164 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001165 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 tmp = st->st_private;
1167 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001168 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 st->st_private = tmp;
1170 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 }
1174 case Return_kind:
1175 if (s->v.Return.value) {
1176 VISIT(st, expr, s->v.Return.value);
1177 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 }
1179 break;
1180 case Delete_kind:
1181 VISIT_SEQ(st, expr, s->v.Delete.targets);
1182 break;
1183 case Assign_kind:
1184 VISIT_SEQ(st, expr, s->v.Assign.targets);
1185 VISIT(st, expr, s->v.Assign.value);
1186 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001187 case AnnAssign_kind:
1188 if (s->v.AnnAssign.target->kind == Name_kind) {
1189 expr_ty e_name = s->v.AnnAssign.target;
1190 long cur = symtable_lookup(st, e_name->v.Name.id);
1191 if (cur < 0) {
1192 VISIT_QUIT(st, 0);
1193 }
1194 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
1195 && s->v.AnnAssign.simple) {
1196 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001197 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1198 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001199 PyErr_SyntaxLocationObject(st->st_filename,
1200 s->lineno,
1201 s->col_offset);
1202 VISIT_QUIT(st, 0);
1203 }
1204 if (s->v.AnnAssign.simple &&
1205 !symtable_add_def(st, e_name->v.Name.id,
1206 DEF_ANNOT | DEF_LOCAL)) {
1207 VISIT_QUIT(st, 0);
1208 }
1209 else {
1210 if (s->v.AnnAssign.value
1211 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1212 VISIT_QUIT(st, 0);
1213 }
1214 }
1215 }
1216 else {
1217 VISIT(st, expr, s->v.AnnAssign.target);
1218 }
1219 VISIT(st, expr, s->v.AnnAssign.annotation);
1220 if (s->v.AnnAssign.value) {
1221 VISIT(st, expr, s->v.AnnAssign.value);
1222 }
1223 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 case AugAssign_kind:
1225 VISIT(st, expr, s->v.AugAssign.target);
1226 VISIT(st, expr, s->v.AugAssign.value);
1227 break;
1228 case For_kind:
1229 VISIT(st, expr, s->v.For.target);
1230 VISIT(st, expr, s->v.For.iter);
1231 VISIT_SEQ(st, stmt, s->v.For.body);
1232 if (s->v.For.orelse)
1233 VISIT_SEQ(st, stmt, s->v.For.orelse);
1234 break;
1235 case While_kind:
1236 VISIT(st, expr, s->v.While.test);
1237 VISIT_SEQ(st, stmt, s->v.While.body);
1238 if (s->v.While.orelse)
1239 VISIT_SEQ(st, stmt, s->v.While.orelse);
1240 break;
1241 case If_kind:
1242 /* XXX if 0: and lookup_yield() hacks */
1243 VISIT(st, expr, s->v.If.test);
1244 VISIT_SEQ(st, stmt, s->v.If.body);
1245 if (s->v.If.orelse)
1246 VISIT_SEQ(st, stmt, s->v.If.orelse);
1247 break;
1248 case Raise_kind:
1249 if (s->v.Raise.exc) {
1250 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001251 if (s->v.Raise.cause) {
1252 VISIT(st, expr, s->v.Raise.cause);
1253 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 }
1255 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001256 case Try_kind:
1257 VISIT_SEQ(st, stmt, s->v.Try.body);
1258 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1259 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1260 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 break;
1262 case Assert_kind:
1263 VISIT(st, expr, s->v.Assert.test);
1264 if (s->v.Assert.msg)
1265 VISIT(st, expr, s->v.Assert.msg);
1266 break;
1267 case Import_kind:
1268 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 break;
1270 case ImportFrom_kind:
1271 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 break;
1273 case Global_kind: {
1274 int i;
1275 asdl_seq *seq = s->v.Global.names;
1276 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1277 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 long cur = symtable_lookup(st, name);
1279 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001280 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001281 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1282 const char* msg;
1283 if (cur & DEF_PARAM) {
1284 msg = GLOBAL_PARAM;
1285 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001286 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001287 } else if (cur & DEF_ANNOT) {
1288 msg = GLOBAL_ANNOT;
1289 } else { /* DEF_LOCAL */
1290 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001291 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001292 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001293 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001294 PyErr_SyntaxLocationObject(st->st_filename,
1295 s->lineno,
1296 s->col_offset);
1297 VISIT_QUIT(st, 0);
1298 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001300 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001301 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001302 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 }
1304 break;
1305 }
1306 case Nonlocal_kind: {
1307 int i;
1308 asdl_seq *seq = s->v.Nonlocal.names;
1309 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1310 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 long cur = symtable_lookup(st, name);
1312 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001313 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001314 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1315 const char* msg;
1316 if (cur & DEF_PARAM) {
1317 msg = NONLOCAL_PARAM;
1318 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001319 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001320 } else if (cur & DEF_ANNOT) {
1321 msg = NONLOCAL_ANNOT;
1322 } else { /* DEF_LOCAL */
1323 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001324 }
1325 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001326 PyErr_SyntaxLocationObject(st->st_filename,
1327 s->lineno,
1328 s->col_offset);
1329 VISIT_QUIT(st, 0);
1330 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001332 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001333 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001334 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 }
1336 break;
1337 }
1338 case Expr_kind:
1339 VISIT(st, expr, s->v.Expr.value);
1340 break;
1341 case Pass_kind:
1342 case Break_kind:
1343 case Continue_kind:
1344 /* nothing to do here */
1345 break;
1346 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001347 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 VISIT_SEQ(st, stmt, s->v.With.body);
1349 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001350 case AsyncFunctionDef_kind:
1351 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1352 VISIT_QUIT(st, 0);
1353 if (s->v.AsyncFunctionDef.args->defaults)
1354 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1355 if (s->v.AsyncFunctionDef.args->kw_defaults)
1356 VISIT_SEQ_WITH_NULL(st, expr,
1357 s->v.AsyncFunctionDef.args->kw_defaults);
1358 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1359 s->v.AsyncFunctionDef.returns))
1360 VISIT_QUIT(st, 0);
1361 if (s->v.AsyncFunctionDef.decorator_list)
1362 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1363 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1364 FunctionBlock, (void *)s, s->lineno,
1365 s->col_offset))
1366 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001367 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001368 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1369 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1370 if (!symtable_exit_block(st, s))
1371 VISIT_QUIT(st, 0);
1372 break;
1373 case AsyncWith_kind:
1374 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1375 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1376 break;
1377 case AsyncFor_kind:
1378 VISIT(st, expr, s->v.AsyncFor.target);
1379 VISIT(st, expr, s->v.AsyncFor.iter);
1380 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1381 if (s->v.AsyncFor.orelse)
1382 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1383 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001385 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386}
1387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389symtable_visit_expr(struct symtable *st, expr_ty e)
1390{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001391 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001392 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001393 "maximum recursion depth exceeded during compilation");
1394 VISIT_QUIT(st, 0);
1395 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 switch (e->kind) {
1397 case BoolOp_kind:
1398 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1399 break;
1400 case BinOp_kind:
1401 VISIT(st, expr, e->v.BinOp.left);
1402 VISIT(st, expr, e->v.BinOp.right);
1403 break;
1404 case UnaryOp_kind:
1405 VISIT(st, expr, e->v.UnaryOp.operand);
1406 break;
1407 case Lambda_kind: {
1408 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001409 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 if (e->v.Lambda.args->defaults)
1411 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001412 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001413 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001415 FunctionBlock, (void *)e, e->lineno,
1416 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001417 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001418 VISIT(st, arguments, e->v.Lambda.args);
1419 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001421 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 break;
1423 }
1424 case IfExp_kind:
1425 VISIT(st, expr, e->v.IfExp.test);
1426 VISIT(st, expr, e->v.IfExp.body);
1427 VISIT(st, expr, e->v.IfExp.orelse);
1428 break;
1429 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001430 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 VISIT_SEQ(st, expr, e->v.Dict.values);
1432 break;
1433 case Set_kind:
1434 VISIT_SEQ(st, expr, e->v.Set.elts);
1435 break;
1436 case GeneratorExp_kind:
1437 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001438 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 break;
1440 case ListComp_kind:
1441 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001442 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 break;
1444 case SetComp_kind:
1445 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001446 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 break;
1448 case DictComp_kind:
1449 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001450 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 break;
1452 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001453 if (e->v.Yield.value)
1454 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001457 case YieldFrom_kind:
1458 VISIT(st, expr, e->v.YieldFrom.value);
1459 st->st_cur->ste_generator = 1;
1460 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001461 case Await_kind:
1462 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001463 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001464 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 case Compare_kind:
1466 VISIT(st, expr, e->v.Compare.left);
1467 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1468 break;
1469 case Call_kind:
1470 VISIT(st, expr, e->v.Call.func);
1471 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001472 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001474 case FormattedValue_kind:
1475 VISIT(st, expr, e->v.FormattedValue.value);
1476 if (e->v.FormattedValue.format_spec)
1477 VISIT(st, expr, e->v.FormattedValue.format_spec);
1478 break;
1479 case JoinedStr_kind:
1480 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1481 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001482 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001483 case Num_kind:
1484 case Str_kind:
1485 case Bytes_kind:
1486 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001487 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 /* Nothing to do here. */
1489 break;
1490 /* The following exprs can be assignment targets. */
1491 case Attribute_kind:
1492 VISIT(st, expr, e->v.Attribute.value);
1493 break;
1494 case Subscript_kind:
1495 VISIT(st, expr, e->v.Subscript.value);
1496 VISIT(st, slice, e->v.Subscript.slice);
1497 break;
1498 case Starred_kind:
1499 VISIT(st, expr, e->v.Starred.value);
1500 break;
1501 case Name_kind:
1502 if (!symtable_add_def(st, e->v.Name.id,
1503 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001504 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 /* Special-case super: it counts as a use of __class__ */
1506 if (e->v.Name.ctx == Load &&
1507 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001508 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001509 if (!GET_IDENTIFIER(__class__) ||
1510 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001511 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 }
1513 break;
1514 /* child nodes of List and Tuple will have expr_context set */
1515 case List_kind:
1516 VISIT_SEQ(st, expr, e->v.List.elts);
1517 break;
1518 case Tuple_kind:
1519 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1520 break;
1521 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001522 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523}
1524
1525static int
1526symtable_implicit_arg(struct symtable *st, int pos)
1527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1529 if (id == NULL)
1530 return 0;
1531 if (!symtable_add_def(st, id, DEF_PARAM)) {
1532 Py_DECREF(id);
1533 return 0;
1534 }
1535 Py_DECREF(id);
1536 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537}
1538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001540symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 if (!args)
1545 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 for (i = 0; i < asdl_seq_LEN(args); i++) {
1548 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1549 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1550 return 0;
1551 }
1552
1553 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554}
1555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001557symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 if (!args)
1562 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 for (i = 0; i < asdl_seq_LEN(args); i++) {
1565 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1566 if (arg->annotation)
1567 VISIT(st, expr, arg->annotation);
1568 }
1569
1570 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001571}
1572
Neal Norwitzc1505362006-12-28 06:47:50 +00001573static int
Yury Selivanov75445082015-05-11 22:57:16 -04001574symtable_visit_annotations(struct symtable *st, stmt_ty s,
1575 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 if (a->args && !symtable_visit_argannotations(st, a->args))
1578 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001579 if (a->vararg && a->vararg->annotation)
1580 VISIT(st, expr, a->vararg->annotation);
1581 if (a->kwarg && a->kwarg->annotation)
1582 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1584 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001585 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001586 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588}
1589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591symtable_visit_arguments(struct symtable *st, arguments_ty a)
1592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 /* skip default arguments inside function block
1594 XXX should ast be different?
1595 */
1596 if (a->args && !symtable_visit_params(st, a->args))
1597 return 0;
1598 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1599 return 0;
1600 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001601 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 return 0;
1603 st->st_cur->ste_varargs = 1;
1604 }
1605 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001606 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 return 0;
1608 st->st_cur->ste_varkeywords = 1;
1609 }
1610 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611}
1612
1613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 if (eh->v.ExceptHandler.type)
1618 VISIT(st, expr, eh->v.ExceptHandler.type);
1619 if (eh->v.ExceptHandler.name)
1620 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1621 return 0;
1622 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1623 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624}
1625
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001626static int
1627symtable_visit_withitem(struct symtable *st, withitem_ty item)
1628{
1629 VISIT(st, expr, item->context_expr);
1630 if (item->optional_vars) {
1631 VISIT(st, expr, item->optional_vars);
1632 }
1633 return 1;
1634}
1635
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638symtable_visit_alias(struct symtable *st, alias_ty a)
1639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001641 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 dotted package name (e.g. spam.eggs)
1643 */
1644 PyObject *store_name;
1645 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001646 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1647 PyUnicode_GET_LENGTH(name), 1);
1648 if (dot != -1) {
1649 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 if (!store_name)
1651 return 0;
1652 }
1653 else {
1654 store_name = name;
1655 Py_INCREF(store_name);
1656 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001657 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1659 Py_DECREF(store_name);
1660 return r;
1661 }
1662 else {
1663 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001664 int lineno = st->st_cur->ste_lineno;
1665 int col_offset = st->st_cur->ste_col_offset;
1666 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001667 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001668 Py_DECREF(store_name);
1669 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 Py_DECREF(store_name);
1672 return 1;
1673 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674}
1675
1676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 VISIT(st, expr, lc->target);
1681 VISIT(st, expr, lc->iter);
1682 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001683 if (lc->is_async) {
1684 st->st_cur->ste_coroutine = 1;
1685 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687}
1688
1689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691symtable_visit_keyword(struct symtable *st, keyword_ty k)
1692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 VISIT(st, expr, k->value);
1694 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695}
1696
1697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699symtable_visit_slice(struct symtable *st, slice_ty s)
1700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 switch (s->kind) {
1702 case Slice_kind:
1703 if (s->v.Slice.lower)
1704 VISIT(st, expr, s->v.Slice.lower)
1705 if (s->v.Slice.upper)
1706 VISIT(st, expr, s->v.Slice.upper)
1707 if (s->v.Slice.step)
1708 VISIT(st, expr, s->v.Slice.step)
1709 break;
1710 case ExtSlice_kind:
1711 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1712 break;
1713 case Index_kind:
1714 VISIT(st, expr, s->v.Index.value)
1715 break;
1716 }
1717 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718}
1719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001721symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001722 identifier scope_name, asdl_seq *generators,
1723 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 int is_generator = (e->kind == GeneratorExp_kind);
1726 int needs_tmp = !is_generator;
1727 comprehension_ty outermost = ((comprehension_ty)
1728 asdl_seq_GET(generators, 0));
1729 /* Outermost iterator is evaluated in current scope */
1730 VISIT(st, expr, outermost->iter);
1731 /* Create comprehension scope for the rest */
1732 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001733 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1734 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 return 0;
1736 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001737 if (outermost->is_async) {
1738 st->st_cur->ste_coroutine = 1;
1739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 /* Outermost iter is received as an argument */
1741 if (!symtable_implicit_arg(st, 0)) {
1742 symtable_exit_block(st, (void *)e);
1743 return 0;
1744 }
1745 /* Allocate temporary name if needed */
1746 if (needs_tmp && !symtable_new_tmpname(st)) {
1747 symtable_exit_block(st, (void *)e);
1748 return 0;
1749 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001750 VISIT(st, expr, outermost->target);
1751 VISIT_SEQ(st, expr, outermost->ifs);
1752 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001754 VISIT(st, expr, value);
1755 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001756 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001757 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001758 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1759 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1760 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1761 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001762 PyErr_SyntaxLocationObject(st->st_filename,
1763 st->st_cur->ste_lineno,
1764 st->st_cur->ste_col_offset);
1765 symtable_exit_block(st, (void *)e);
1766 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001767 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001768 st->st_cur->ste_generator = is_generator;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001773symtable_visit_genexp(struct symtable *st, expr_ty e)
1774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1776 e->v.GeneratorExp.generators,
1777 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001778}
1779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001781symtable_visit_listcomp(struct symtable *st, expr_ty e)
1782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1784 e->v.ListComp.generators,
1785 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001786}
1787
1788static int
1789symtable_visit_setcomp(struct symtable *st, expr_ty e)
1790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1792 e->v.SetComp.generators,
1793 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001794}
1795
1796static int
1797symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1800 e->v.DictComp.generators,
1801 e->v.DictComp.key,
1802 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001803}