blob: 6165cfe162eac0690ad68795f70babf05c0a2397 [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 \
Guido van Rossum6cff8742016-09-09 09:36:26 -07009"name '%U' 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 \
Guido van Rossum6cff8742016-09-09 09:36:26 -070012"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070015"name '%U' 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 \
Guido van Rossum6cff8742016-09-09 09:36:26 -070018"name '%U' is used prior to nonlocal declaration"
19
20#define GLOBAL_ANNOT \
21"annotated name '%U' can't be global"
22
23#define NONLOCAL_ANNOT \
24"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000025
Neal Norwitz5d0ad502005-12-19 04:27:42 +000026#define IMPORT_STAR_WARNING "import * only allowed at module level"
27
Neal Norwitz090b3dd2006-02-28 22:36:46 +000028static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000029ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000030 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000031{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020033 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 k = PyLong_FromVoidPtr(key);
36 if (k == NULL)
37 goto fail;
38 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020039 if (ste == NULL) {
40 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020042 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020044 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020047 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 ste->ste_symbols = NULL;
50 ste->ste_varnames = NULL;
51 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000052
Benjamin Petersond9c87022012-10-31 20:26:20 -040053 ste->ste_directives = NULL;
54
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 ste->ste_nested = 0;
57 ste->ste_free = 0;
58 ste->ste_varargs = 0;
59 ste->ste_varkeywords = 0;
60 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000061 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000062 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000064 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 if (st->st_cur != NULL &&
67 (st->st_cur->ste_nested ||
68 st->st_cur->ste_type == FunctionBlock))
69 ste->ste_nested = 1;
70 ste->ste_child_free = 0;
71 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070072 ste->ste_coroutine = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050074 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000075
Victor Stinner9a4fb662013-07-11 22:49:00 +020076 ste->ste_symbols = PyDict_New();
77 ste->ste_varnames = PyList_New(0);
78 ste->ste_children = PyList_New(0);
79 if (ste->ste_symbols == NULL
80 || ste->ste_varnames == NULL
81 || ste->ste_children == NULL)
82 goto fail;
83
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
85 goto fail;
86
87 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 Py_XDECREF(ste);
90 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000091}
92
93static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000094ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
97 ste->ste_name,
98 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000099}
100
101static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 ste->ste_table = NULL;
105 Py_XDECREF(ste->ste_id);
106 Py_XDECREF(ste->ste_name);
107 Py_XDECREF(ste->ste_symbols);
108 Py_XDECREF(ste->ste_varnames);
109 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400110 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000112}
113
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000115
Guido van Rossum6f799372001-09-20 20:46:19 +0000116static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 {"id", T_OBJECT, OFF(ste_id), READONLY},
118 {"name", T_OBJECT, OFF(ste_name), READONLY},
119 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
120 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
121 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 {"nested", T_INT, OFF(ste_nested), READONLY},
123 {"type", T_INT, OFF(ste_type), READONLY},
124 {"lineno", T_INT, OFF(ste_lineno), READONLY},
125 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000126};
127
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000128PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 PyVarObject_HEAD_INIT(&PyType_Type, 0)
130 "symtable entry",
131 sizeof(PySTEntryObject),
132 0,
133 (destructor)ste_dealloc, /* tp_dealloc */
134 0, /* tp_print */
135 0, /* tp_getattr */
136 0, /* tp_setattr */
137 0, /* tp_reserved */
138 (reprfunc)ste_repr, /* tp_repr */
139 0, /* tp_as_number */
140 0, /* tp_as_sequence */
141 0, /* tp_as_mapping */
142 0, /* tp_hash */
143 0, /* tp_call */
144 0, /* tp_str */
145 PyObject_GenericGetAttr, /* tp_getattro */
146 0, /* tp_setattro */
147 0, /* tp_as_buffer */
148 Py_TPFLAGS_DEFAULT, /* tp_flags */
149 0, /* tp_doc */
150 0, /* tp_traverse */
151 0, /* tp_clear */
152 0, /* tp_richcompare */
153 0, /* tp_weaklistoffset */
154 0, /* tp_iter */
155 0, /* tp_iternext */
156 0, /* tp_methods */
157 ste_memberlist, /* tp_members */
158 0, /* tp_getset */
159 0, /* tp_base */
160 0, /* tp_dict */
161 0, /* tp_descr_get */
162 0, /* tp_descr_set */
163 0, /* tp_dictoffset */
164 0, /* tp_init */
165 0, /* tp_alloc */
166 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000167};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168
169static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000171 _Py_block_ty block, void *ast, int lineno,
172 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173static int symtable_exit_block(struct symtable *st, void *ast);
174static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
175static int symtable_visit_expr(struct symtable *st, expr_ty s);
176static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000177static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
178static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000179static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180static int symtable_visit_arguments(struct symtable *st, arguments_ty);
181static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
182static int symtable_visit_alias(struct symtable *st, alias_ty);
183static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
184static int symtable_visit_keyword(struct symtable *st, keyword_ty);
185static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000186static int symtable_visit_params(struct symtable *st, asdl_seq *args);
187static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188static int symtable_implicit_arg(struct symtable *st, int pos);
Yury Selivanov75445082015-05-11 22:57:16 -0400189static int symtable_visit_annotations(struct symtable *st, stmt_ty s, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500190static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
192
Nick Coghlan650f0d02007-04-15 12:05:43 +0000193static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500195 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
197#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
200#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000201"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202
203static struct symtable *
204symtable_new(void)
205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
209 if (st == NULL)
210 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 st->st_filename = NULL;
213 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 if ((st->st_stack = PyList_New(0)) == NULL)
216 goto fail;
217 if ((st->st_blocks = PyDict_New()) == NULL)
218 goto fail;
219 st->st_cur = NULL;
220 st->st_private = NULL;
221 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 PySymtable_Free(st);
224 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225}
226
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000227/* When compiling the use of C stack is probably going to be a lot
228 lighter than when executing Python code but still can overflow
229 and causing a Python crash if not checked (e.g. eval("()"*300000)).
230 Using the current recursion limit for the compiler seems too
231 restrictive (it caused at least one test to fail) so a factor is
232 used to allow deeper recursion when compiling an expression.
233
234 Using a scaling factor means this should automatically adjust when
235 the recursion limit is adjusted for small or large C stack allocations.
236*/
237#define COMPILER_STACK_FRAME_SCALE 3
238
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200240PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000242 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 asdl_seq *seq;
244 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000245 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400246 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200249 return NULL;
250 if (filename == NULL) {
251 PySymtable_Free(st);
252 return NULL;
253 }
254 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 st->st_filename = filename;
256 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000257
258 /* Setup recursion depth check counters */
259 tstate = PyThreadState_GET();
260 if (!tstate) {
261 PySymtable_Free(st);
262 return NULL;
263 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400264 /* Be careful here to prevent overflow. */
265 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
266 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
267 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
268 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 /* Make the initial symbol information gathering pass */
271 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000272 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 PySymtable_Free(st);
274 return NULL;
275 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 switch (mod->kind) {
279 case Module_kind:
280 seq = mod->v.Module.body;
281 for (i = 0; i < asdl_seq_LEN(seq); i++)
282 if (!symtable_visit_stmt(st,
283 (stmt_ty)asdl_seq_GET(seq, i)))
284 goto error;
285 break;
286 case Expression_kind:
287 if (!symtable_visit_expr(st, mod->v.Expression.body))
288 goto error;
289 break;
290 case Interactive_kind:
291 seq = mod->v.Interactive.body;
292 for (i = 0; i < asdl_seq_LEN(seq); i++)
293 if (!symtable_visit_stmt(st,
294 (stmt_ty)asdl_seq_GET(seq, i)))
295 goto error;
296 break;
297 case Suite_kind:
298 PyErr_SetString(PyExc_RuntimeError,
299 "this compiler does not handle Suites");
300 goto error;
301 }
302 if (!symtable_exit_block(st, (void *)mod)) {
303 PySymtable_Free(st);
304 return NULL;
305 }
306 /* Make the second symbol analysis pass */
307 if (symtable_analyze(st))
308 return st;
309 PySymtable_Free(st);
310 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 (void) symtable_exit_block(st, (void *)mod);
313 PySymtable_Free(st);
314 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315}
316
Victor Stinner14e461d2013-08-26 22:28:21 +0200317struct symtable *
318PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
319{
320 PyObject *filename;
321 struct symtable *st;
322 filename = PyUnicode_DecodeFSDefault(filename_str);
323 if (filename == NULL)
324 return NULL;
325 st = PySymtable_BuildObject(mod, filename, future);
326 Py_DECREF(filename);
327 return st;
328}
329
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330void
331PySymtable_Free(struct symtable *st)
332{
Victor Stinner14e461d2013-08-26 22:28:21 +0200333 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 Py_XDECREF(st->st_blocks);
335 Py_XDECREF(st->st_stack);
336 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337}
338
339PySTEntryObject *
340PySymtable_Lookup(struct symtable *st, void *key)
341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 k = PyLong_FromVoidPtr(key);
345 if (k == NULL)
346 return NULL;
347 v = PyDict_GetItem(st->st_blocks, k);
348 if (v) {
349 assert(PySTEntry_Check(v));
350 Py_INCREF(v);
351 }
352 else {
353 PyErr_SetString(PyExc_KeyError,
354 "unknown symbol table entry");
355 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 Py_DECREF(k);
358 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359}
360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362PyST_GetScope(PySTEntryObject *ste, PyObject *name)
363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
365 if (!v)
366 return 0;
367 assert(PyLong_Check(v));
368 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369}
370
Benjamin Petersond9c87022012-10-31 20:26:20 -0400371static int
372error_at_directive(PySTEntryObject *ste, PyObject *name)
373{
374 Py_ssize_t i;
375 PyObject *data;
376 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600377 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400378 data = PyList_GET_ITEM(ste->ste_directives, i);
379 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600380 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
381 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
382 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
383 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
384 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
385
386 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700387 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400388 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600389 PyErr_SetString(PyExc_RuntimeError,
390 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400391 return 0;
392}
393
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394
395/* Analyze raw symbol information to determine scope of each name.
396
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000397 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000401
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403 explicit global is declared with the global statement. An implicit
404 global is a free variable for which the compiler has found no binding
405 in an enclosing function scope. The implicit global is either a global
406 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
407 to handle these names to implement slightly odd semantics. In such a
408 block, the name is treated as global until it is assigned to; then it
409 is treated as a local.
410
411 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000412 The first pass collects raw facts from the AST via the symtable_visit_*
413 functions: the name is a parameter here, the name is used but not defined
414 here, etc. The second pass analyzes these facts during a pass over the
415 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000416
417 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000419 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000420 Names which are explicitly declared nonlocal must exist in this set of
421 visible names - if they do not, a syntax error is raised. After doing
422 the local analysis, it analyzes each of its child blocks using an
423 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000424
Nick Coghlan650f0d02007-04-15 12:05:43 +0000425 The children update the free variable set. If a local variable is added to
426 the free variable set by the child, the variable is marked as a cell. The
427 function object being defined must provide runtime storage for the variable
428 that may outlive the function's frame. Cell variables are removed from the
429 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000430
Nick Coghlan650f0d02007-04-15 12:05:43 +0000431 During analysis, the names are:
432 symbols: dict mapping from symbol names to flag values (including offset scope values)
433 scopes: dict mapping from symbol names to scope values (no offset)
434 local: set of all symbol names local to the current scope
435 bound: set of all symbol names local to a containing function scope
436 free: set of all symbol names referenced but not bound in child scopes
437 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000438*/
439
440#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 PyObject *o = PyLong_FromLong(I); \
442 if (!o) \
443 return 0; \
444 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
445 Py_DECREF(o); \
446 return 0; \
447 } \
448 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449}
450
451/* Decide on scope of name, given flags.
452
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000453 The namespace dictionaries may be modified to record information
454 about the new name. For example, a new global will add an entry to
455 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000456*/
457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000459analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 PyObject *bound, PyObject *local, PyObject *free,
461 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (flags & DEF_GLOBAL) {
464 if (flags & DEF_PARAM) {
465 PyErr_Format(PyExc_SyntaxError,
466 "name '%U' is parameter and global",
467 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400468 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000469 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (flags & DEF_NONLOCAL) {
471 PyErr_Format(PyExc_SyntaxError,
472 "name '%U' is nonlocal and global",
473 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400474 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 }
476 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
477 if (PySet_Add(global, name) < 0)
478 return 0;
479 if (bound && (PySet_Discard(bound, name) < 0))
480 return 0;
481 return 1;
482 }
483 if (flags & DEF_NONLOCAL) {
484 if (flags & DEF_PARAM) {
485 PyErr_Format(PyExc_SyntaxError,
486 "name '%U' is parameter and nonlocal",
487 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400488 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 }
490 if (!bound) {
491 PyErr_Format(PyExc_SyntaxError,
492 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400493 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 }
495 if (!PySet_Contains(bound, name)) {
496 PyErr_Format(PyExc_SyntaxError,
497 "no binding for nonlocal '%U' found",
498 name);
499
Benjamin Petersond9c87022012-10-31 20:26:20 -0400500 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 }
502 SET_SCOPE(scopes, name, FREE);
503 ste->ste_free = 1;
504 return PySet_Add(free, name) >= 0;
505 }
506 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000507 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 if (PySet_Add(local, name) < 0)
509 return 0;
510 if (PySet_Discard(global, name) < 0)
511 return 0;
512 return 1;
513 }
514 /* If an enclosing block has a binding for this name, it
515 is a free variable rather than a global variable.
516 Note that having a non-NULL bound implies that the block
517 is nested.
518 */
519 if (bound && PySet_Contains(bound, name)) {
520 SET_SCOPE(scopes, name, FREE);
521 ste->ste_free = 1;
522 return PySet_Add(free, name) >= 0;
523 }
524 /* If a parent has a global statement, then call it global
525 explicit? It could also be global implicit.
526 */
527 if (global && PySet_Contains(global, name)) {
528 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
529 return 1;
530 }
531 if (ste->ste_nested)
532 ste->ste_free = 1;
533 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
534 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535}
536
537#undef SET_SCOPE
538
539/* If a name is defined in free and also in locals, then this block
540 provides the binding for the free variable. The name should be
541 marked CELL in this block and removed from the free list.
542
543 Note that the current block's free variables are included in free.
544 That's safe because no name can be free and local in the same scope.
545*/
546
547static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500548analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 PyObject *name, *v, *v_cell;
551 int success = 0;
552 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 v_cell = PyLong_FromLong(CELL);
555 if (!v_cell)
556 return 0;
557 while (PyDict_Next(scopes, &pos, &name, &v)) {
558 long scope;
559 assert(PyLong_Check(v));
560 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000561 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 continue;
563 if (!PySet_Contains(free, name))
564 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 /* Replace LOCAL with CELL for this name, and remove
566 from free. It is safe to replace the value of name
567 in the dict, because it will not cause a resize.
568 */
569 if (PyDict_SetItem(scopes, name, v_cell) < 0)
570 goto error;
571 if (PySet_Discard(free, name) < 0)
572 goto error;
573 }
574 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 Py_DECREF(v_cell);
577 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578}
579
Benjamin Peterson312595c2013-05-15 15:26:42 -0500580static int
581drop_class_free(PySTEntryObject *ste, PyObject *free)
582{
583 int res;
584 if (!GET_IDENTIFIER(__class__))
585 return 0;
586 res = PySet_Discard(free, __class__);
587 if (res < 0)
588 return 0;
589 if (res)
590 ste->ste_needs_class_closure = 1;
591 return 1;
592}
593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594/* Enter the final scope information into the ste_symbols dict.
595 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596 * All arguments are dicts. Modifies symbols, others are read-only.
597*/
598static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000600 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 PyObject *name = NULL, *itr = NULL;
603 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
604 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 /* Update scope information for all symbols in this scope */
607 while (PyDict_Next(symbols, &pos, &name, &v)) {
608 long scope, flags;
609 assert(PyLong_Check(v));
610 flags = PyLong_AS_LONG(v);
611 v_scope = PyDict_GetItem(scopes, name);
612 assert(v_scope && PyLong_Check(v_scope));
613 scope = PyLong_AS_LONG(v_scope);
614 flags |= (scope << SCOPE_OFFSET);
615 v_new = PyLong_FromLong(flags);
616 if (!v_new)
617 return 0;
618 if (PyDict_SetItem(symbols, name, v_new) < 0) {
619 Py_DECREF(v_new);
620 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 Py_DECREF(v_new);
623 }
624
625 /* Record not yet resolved free variables from children (if any) */
626 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
627 if (!v_free)
628 return 0;
629
630 itr = PyObject_GetIter(free);
631 if (!itr)
632 goto error;
633
634 while ((name = PyIter_Next(itr))) {
635 v = PyDict_GetItem(symbols, name);
636
637 /* Handle symbol that already exists in this scope */
638 if (v) {
639 /* Handle a free variable in a method of
640 the class that has the same name as a local
641 or global in the class scope.
642 */
643 if (classflag &&
644 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
645 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
646 v_new = PyLong_FromLong(flags);
647 if (!v_new) {
648 goto error;
649 }
650 if (PyDict_SetItem(symbols, name, v_new) < 0) {
651 Py_DECREF(v_new);
652 goto error;
653 }
654 Py_DECREF(v_new);
655 }
656 /* It's a cell, or already free in this scope */
657 Py_DECREF(name);
658 continue;
659 }
660 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200661 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 Py_DECREF(name);
663 continue; /* it's a global */
664 }
665 /* Propagate new free symbol up the lexical stack */
666 if (PyDict_SetItem(symbols, name, v_free) < 0) {
667 goto error;
668 }
669 Py_DECREF(name);
670 }
671 Py_DECREF(itr);
672 Py_DECREF(v_free);
673 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000674error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 Py_XDECREF(v_free);
676 Py_XDECREF(itr);
677 Py_XDECREF(name);
678 return 0;
679}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680
681/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683 Arguments:
684 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000685 bound -- set of variables bound in enclosing scopes (input). bound
686 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 free -- set of free variables in enclosed scopes (output)
688 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000689
690 The implementation uses two mutually recursive functions,
691 analyze_block() and analyze_child_block(). analyze_block() is
692 responsible for analyzing the individual names defined in a block.
693 analyze_child_block() prepares temporary namespace dictionaries
694 used to evaluated nested blocks.
695
696 The two functions exist because a child block should see the name
697 bindings of its enclosing blocks, but those bindings should not
698 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699*/
700
701static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
703 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000704
705static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
707 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
710 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
711 PyObject *temp;
712 int i, success = 0;
713 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 local = PySet_New(NULL); /* collect new names bound in block */
716 if (!local)
717 goto error;
718 scopes = PyDict_New(); /* collect scopes defined for each name */
719 if (!scopes)
720 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 /* Allocate new global and bound variable dictionaries. These
723 dictionaries hold the names visible in nested blocks. For
724 ClassBlocks, the bound and global names are initialized
725 before analyzing names, because class bindings aren't
726 visible in methods. For other blocks, they are initialized
727 after names are analyzed.
728 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 /* TODO(jhylton): Package these dicts in a struct so that we
731 can write reasonable helper functions?
732 */
733 newglobal = PySet_New(NULL);
734 if (!newglobal)
735 goto error;
736 newfree = PySet_New(NULL);
737 if (!newfree)
738 goto error;
739 newbound = PySet_New(NULL);
740 if (!newbound)
741 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 /* Class namespace has no effect on names visible in
744 nested functions, so populate the global and bound
745 sets to be passed to child blocks before analyzing
746 this one.
747 */
748 if (ste->ste_type == ClassBlock) {
749 /* Pass down known globals */
750 temp = PyNumber_InPlaceOr(newglobal, global);
751 if (!temp)
752 goto error;
753 Py_DECREF(temp);
754 /* Pass down previously bound symbols */
755 if (bound) {
756 temp = PyNumber_InPlaceOr(newbound, bound);
757 if (!temp)
758 goto error;
759 Py_DECREF(temp);
760 }
761 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
764 long flags = PyLong_AS_LONG(v);
765 if (!analyze_name(ste, scopes, name, flags,
766 bound, local, free, global))
767 goto error;
768 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 /* Populate global and bound sets to be passed to children. */
771 if (ste->ste_type != ClassBlock) {
772 /* Add function locals to bound set */
773 if (ste->ste_type == FunctionBlock) {
774 temp = PyNumber_InPlaceOr(newbound, local);
775 if (!temp)
776 goto error;
777 Py_DECREF(temp);
778 }
779 /* Pass down previously bound symbols */
780 if (bound) {
781 temp = PyNumber_InPlaceOr(newbound, bound);
782 if (!temp)
783 goto error;
784 Py_DECREF(temp);
785 }
786 /* Pass down known globals */
787 temp = PyNumber_InPlaceOr(newglobal, global);
788 if (!temp)
789 goto error;
790 Py_DECREF(temp);
791 }
792 else {
793 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000794 if (!GET_IDENTIFIER(__class__))
795 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 if (PySet_Add(newbound, __class__) < 0)
797 goto error;
798 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300800 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 newbound, newglobal now contain the names visible in
803 nested blocks. The free variables in the children will
804 be collected in allfree.
805 */
806 allfree = PySet_New(NULL);
807 if (!allfree)
808 goto error;
809 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
810 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
811 PySTEntryObject* entry;
812 assert(c && PySTEntry_Check(c));
813 entry = (PySTEntryObject*)c;
814 if (!analyze_child_block(entry, newbound, newfree, newglobal,
815 allfree))
816 goto error;
817 /* Check if any children have free variables */
818 if (entry->ste_free || entry->ste_child_free)
819 ste->ste_child_free = 1;
820 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 temp = PyNumber_InPlaceOr(newfree, allfree);
823 if (!temp)
824 goto error;
825 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500828 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500830 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 goto error;
832 /* Records the results of the analysis in the symbol table entry */
833 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
834 ste->ste_type == ClassBlock))
835 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 temp = PyNumber_InPlaceOr(free, newfree);
838 if (!temp)
839 goto error;
840 Py_DECREF(temp);
841 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 Py_XDECREF(scopes);
844 Py_XDECREF(local);
845 Py_XDECREF(newbound);
846 Py_XDECREF(newglobal);
847 Py_XDECREF(newfree);
848 Py_XDECREF(allfree);
849 if (!success)
850 assert(PyErr_Occurred());
851 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852}
853
854static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
856 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
859 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000862
Martin Panter3ee62702016-06-04 04:57:19 +0000863 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 current block. The analyze_block() call modifies these
865 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 */
868 temp_bound = PySet_New(bound);
869 if (!temp_bound)
870 goto error;
871 temp_free = PySet_New(free);
872 if (!temp_free)
873 goto error;
874 temp_global = PySet_New(global);
875 if (!temp_global)
876 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
879 goto error;
880 temp = PyNumber_InPlaceOr(child_free, temp_free);
881 if (!temp)
882 goto error;
883 Py_DECREF(temp);
884 Py_DECREF(temp_bound);
885 Py_DECREF(temp_free);
886 Py_DECREF(temp_global);
887 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000888 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 Py_XDECREF(temp_bound);
890 Py_XDECREF(temp_free);
891 Py_XDECREF(temp_global);
892 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000893}
894
895static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896symtable_analyze(struct symtable *st)
897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 PyObject *free, *global;
899 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 free = PySet_New(NULL);
902 if (!free)
903 return 0;
904 global = PySet_New(NULL);
905 if (!global) {
906 Py_DECREF(free);
907 return 0;
908 }
909 r = analyze_block(st->st_top, NULL, free, global);
910 Py_DECREF(free);
911 Py_DECREF(global);
912 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913}
914
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000915/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916 This reference is released when the block is exited, via the DECREF
917 in symtable_exit_block().
918*/
919
920static int
921symtable_exit_block(struct symtable *st, void *ast)
922{
Benjamin Peterson609da582011-06-29 22:52:39 -0500923 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924
Benjamin Peterson609da582011-06-29 22:52:39 -0500925 st->st_cur = NULL;
926 size = PyList_GET_SIZE(st->st_stack);
927 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500928 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500930 if (--size)
931 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 }
933 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934}
935
936static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000938 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939{
Benjamin Peterson609da582011-06-29 22:52:39 -0500940 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941
Benjamin Peterson609da582011-06-29 22:52:39 -0500942 ste = ste_new(st, name, block, ast, lineno, col_offset);
943 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500945 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
946 Py_DECREF(ste);
947 return 0;
948 }
949 prev = st->st_cur;
950 /* The entry is owned by the stack. Borrow it for st_cur. */
951 Py_DECREF(ste);
952 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000953 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 st->st_global = st->st_cur->ste_symbols;
955 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500956 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 return 0;
958 }
959 }
960 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961}
962
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000963static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964symtable_lookup(struct symtable *st, PyObject *name)
965{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 PyObject *o;
967 PyObject *mangled = _Py_Mangle(st->st_private, name);
968 if (!mangled)
969 return 0;
970 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
971 Py_DECREF(mangled);
972 if (!o)
973 return 0;
974 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975}
976
977static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 PyObject *o;
981 PyObject *dict;
982 long val;
983 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984
Jeremy Hylton81e95022007-02-27 06:50:52 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 if (!mangled)
987 return 0;
988 dict = st->st_cur->ste_symbols;
989 if ((o = PyDict_GetItem(dict, mangled))) {
990 val = PyLong_AS_LONG(o);
991 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
992 /* Is it better to use 'mangled' or 'name' here? */
993 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +0200994 PyErr_SyntaxLocationObject(st->st_filename,
995 st->st_cur->ste_lineno,
996 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 goto error;
998 }
999 val |= flag;
1000 } else
1001 val = flag;
1002 o = PyLong_FromLong(val);
1003 if (o == NULL)
1004 goto error;
1005 if (PyDict_SetItem(dict, mangled, o) < 0) {
1006 Py_DECREF(o);
1007 goto error;
1008 }
1009 Py_DECREF(o);
1010
1011 if (flag & DEF_PARAM) {
1012 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1013 goto error;
1014 } else if (flag & DEF_GLOBAL) {
1015 /* XXX need to update DEF_GLOBAL for other flags too;
1016 perhaps only DEF_FREE_GLOBAL */
1017 val = flag;
1018 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1019 val |= PyLong_AS_LONG(o);
1020 }
1021 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 goto error;
1024 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1025 Py_DECREF(o);
1026 goto error;
1027 }
1028 Py_DECREF(o);
1029 }
1030 Py_DECREF(mangled);
1031 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001032
1033error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 Py_DECREF(mangled);
1035 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036}
1037
1038/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1039 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 function.
1041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1043 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001044
1045 VISIT_QUIT macro returns the specified value exiting from the function but
1046 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047*/
1048
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001049#define VISIT_QUIT(ST, X) \
1050 return --(ST)->recursion_depth,(X)
1051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001054 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 int i; \
1058 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1059 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1060 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1061 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001062 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 int i; \
1068 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1069 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1070 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1071 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001072 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001075
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001076#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001078 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001080 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001082 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001083 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001085}
1086
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001088symtable_new_tmpname(struct symtable *st)
1089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 char tmpname[256];
1091 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1094 ++st->st_cur->ste_tmpname);
1095 tmp = PyUnicode_InternFromString(tmpname);
1096 if (!tmp)
1097 return 0;
1098 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1099 return 0;
1100 Py_DECREF(tmp);
1101 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001102}
1103
Guido van Rossum4f72a782006-10-27 23:31:49 +00001104
Guido van Rossumc2e20742006-02-27 22:32:47 +00001105static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001106symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1107{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001108 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001109 int res;
1110 if (!st->st_cur->ste_directives) {
1111 st->st_cur->ste_directives = PyList_New(0);
1112 if (!st->st_cur->ste_directives)
1113 return 0;
1114 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001115 mangled = _Py_Mangle(st->st_private, name);
1116 if (!mangled)
1117 return 0;
1118 data = Py_BuildValue("(Nii)", mangled, s->lineno, s->col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001119 if (!data)
1120 return 0;
1121 res = PyList_Append(st->st_cur->ste_directives, data);
1122 Py_DECREF(data);
1123 return res == 0;
1124}
1125
1126
1127static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128symtable_visit_stmt(struct symtable *st, stmt_ty s)
1129{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001130 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001131 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001132 "maximum recursion depth exceeded during compilation");
1133 VISIT_QUIT(st, 0);
1134 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 switch (s->kind) {
1136 case FunctionDef_kind:
1137 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001138 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (s->v.FunctionDef.args->defaults)
1140 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1141 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001142 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001143 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1144 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001145 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (s->v.FunctionDef.decorator_list)
1147 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1148 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001149 FunctionBlock, (void *)s, s->lineno,
1150 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001151 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001152 VISIT(st, arguments, s->v.FunctionDef.args);
1153 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001155 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 break;
1157 case ClassDef_kind: {
1158 PyObject *tmp;
1159 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001160 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1162 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if (s->v.ClassDef.decorator_list)
1164 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1165 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001166 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001167 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 tmp = st->st_private;
1169 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001170 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 st->st_private = tmp;
1172 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001173 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 break;
1175 }
1176 case Return_kind:
1177 if (s->v.Return.value) {
1178 VISIT(st, expr, s->v.Return.value);
1179 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 }
1181 break;
1182 case Delete_kind:
1183 VISIT_SEQ(st, expr, s->v.Delete.targets);
1184 break;
1185 case Assign_kind:
1186 VISIT_SEQ(st, expr, s->v.Assign.targets);
1187 VISIT(st, expr, s->v.Assign.value);
1188 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001189 case AnnAssign_kind:
1190 if (s->v.AnnAssign.target->kind == Name_kind) {
1191 expr_ty e_name = s->v.AnnAssign.target;
1192 long cur = symtable_lookup(st, e_name->v.Name.id);
1193 if (cur < 0) {
1194 VISIT_QUIT(st, 0);
1195 }
1196 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
1197 && s->v.AnnAssign.simple) {
1198 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001199 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1200 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001201 PyErr_SyntaxLocationObject(st->st_filename,
1202 s->lineno,
1203 s->col_offset);
1204 VISIT_QUIT(st, 0);
1205 }
1206 if (s->v.AnnAssign.simple &&
1207 !symtable_add_def(st, e_name->v.Name.id,
1208 DEF_ANNOT | DEF_LOCAL)) {
1209 VISIT_QUIT(st, 0);
1210 }
1211 else {
1212 if (s->v.AnnAssign.value
1213 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1214 VISIT_QUIT(st, 0);
1215 }
1216 }
1217 }
1218 else {
1219 VISIT(st, expr, s->v.AnnAssign.target);
1220 }
1221 VISIT(st, expr, s->v.AnnAssign.annotation);
1222 if (s->v.AnnAssign.value) {
1223 VISIT(st, expr, s->v.AnnAssign.value);
1224 }
1225 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 case AugAssign_kind:
1227 VISIT(st, expr, s->v.AugAssign.target);
1228 VISIT(st, expr, s->v.AugAssign.value);
1229 break;
1230 case For_kind:
1231 VISIT(st, expr, s->v.For.target);
1232 VISIT(st, expr, s->v.For.iter);
1233 VISIT_SEQ(st, stmt, s->v.For.body);
1234 if (s->v.For.orelse)
1235 VISIT_SEQ(st, stmt, s->v.For.orelse);
1236 break;
1237 case While_kind:
1238 VISIT(st, expr, s->v.While.test);
1239 VISIT_SEQ(st, stmt, s->v.While.body);
1240 if (s->v.While.orelse)
1241 VISIT_SEQ(st, stmt, s->v.While.orelse);
1242 break;
1243 case If_kind:
1244 /* XXX if 0: and lookup_yield() hacks */
1245 VISIT(st, expr, s->v.If.test);
1246 VISIT_SEQ(st, stmt, s->v.If.body);
1247 if (s->v.If.orelse)
1248 VISIT_SEQ(st, stmt, s->v.If.orelse);
1249 break;
1250 case Raise_kind:
1251 if (s->v.Raise.exc) {
1252 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001253 if (s->v.Raise.cause) {
1254 VISIT(st, expr, s->v.Raise.cause);
1255 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 }
1257 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001258 case Try_kind:
1259 VISIT_SEQ(st, stmt, s->v.Try.body);
1260 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1261 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1262 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 break;
1264 case Assert_kind:
1265 VISIT(st, expr, s->v.Assert.test);
1266 if (s->v.Assert.msg)
1267 VISIT(st, expr, s->v.Assert.msg);
1268 break;
1269 case Import_kind:
1270 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 break;
1272 case ImportFrom_kind:
1273 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 break;
1275 case Global_kind: {
1276 int i;
1277 asdl_seq *seq = s->v.Global.names;
1278 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1279 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 long cur = symtable_lookup(st, name);
1281 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001282 VISIT_QUIT(st, 0);
Guido van Rossum6cff8742016-09-09 09:36:26 -07001283 if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) {
1284 char* msg;
Christian Heimes517507c2016-09-23 20:26:30 +02001285 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);
Guido van Rossum6cff8742016-09-09 09:36:26 -07001314 if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) {
1315 char* msg;
Christian Heimes517507c2016-09-23 20:26:30 +02001316 if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001317 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001318 } else if (cur & DEF_ANNOT) {
1319 msg = NONLOCAL_ANNOT;
1320 } else { /* DEF_LOCAL */
1321 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001322 }
1323 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001324 PyErr_SyntaxLocationObject(st->st_filename,
1325 s->lineno,
1326 s->col_offset);
1327 VISIT_QUIT(st, 0);
1328 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001330 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001331 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001332 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 }
1334 break;
1335 }
1336 case Expr_kind:
1337 VISIT(st, expr, s->v.Expr.value);
1338 break;
1339 case Pass_kind:
1340 case Break_kind:
1341 case Continue_kind:
1342 /* nothing to do here */
1343 break;
1344 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001345 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 VISIT_SEQ(st, stmt, s->v.With.body);
1347 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001348 case AsyncFunctionDef_kind:
1349 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1350 VISIT_QUIT(st, 0);
1351 if (s->v.AsyncFunctionDef.args->defaults)
1352 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1353 if (s->v.AsyncFunctionDef.args->kw_defaults)
1354 VISIT_SEQ_WITH_NULL(st, expr,
1355 s->v.AsyncFunctionDef.args->kw_defaults);
1356 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1357 s->v.AsyncFunctionDef.returns))
1358 VISIT_QUIT(st, 0);
1359 if (s->v.AsyncFunctionDef.decorator_list)
1360 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1361 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1362 FunctionBlock, (void *)s, s->lineno,
1363 s->col_offset))
1364 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001365 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001366 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1367 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1368 if (!symtable_exit_block(st, s))
1369 VISIT_QUIT(st, 0);
1370 break;
1371 case AsyncWith_kind:
1372 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1373 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1374 break;
1375 case AsyncFor_kind:
1376 VISIT(st, expr, s->v.AsyncFor.target);
1377 VISIT(st, expr, s->v.AsyncFor.iter);
1378 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1379 if (s->v.AsyncFor.orelse)
1380 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1381 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001383 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384}
1385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387symtable_visit_expr(struct symtable *st, expr_ty e)
1388{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001389 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001390 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001391 "maximum recursion depth exceeded during compilation");
1392 VISIT_QUIT(st, 0);
1393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 switch (e->kind) {
1395 case BoolOp_kind:
1396 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1397 break;
1398 case BinOp_kind:
1399 VISIT(st, expr, e->v.BinOp.left);
1400 VISIT(st, expr, e->v.BinOp.right);
1401 break;
1402 case UnaryOp_kind:
1403 VISIT(st, expr, e->v.UnaryOp.operand);
1404 break;
1405 case Lambda_kind: {
1406 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001407 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 if (e->v.Lambda.args->defaults)
1409 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001410 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001411 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001413 FunctionBlock, (void *)e, e->lineno,
1414 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001415 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001416 VISIT(st, arguments, e->v.Lambda.args);
1417 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001419 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 break;
1421 }
1422 case IfExp_kind:
1423 VISIT(st, expr, e->v.IfExp.test);
1424 VISIT(st, expr, e->v.IfExp.body);
1425 VISIT(st, expr, e->v.IfExp.orelse);
1426 break;
1427 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001428 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 VISIT_SEQ(st, expr, e->v.Dict.values);
1430 break;
1431 case Set_kind:
1432 VISIT_SEQ(st, expr, e->v.Set.elts);
1433 break;
1434 case GeneratorExp_kind:
1435 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001436 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 break;
1438 case ListComp_kind:
1439 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001440 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 break;
1442 case SetComp_kind:
1443 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001444 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 break;
1446 case DictComp_kind:
1447 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001448 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 break;
1450 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001451 if (e->v.Yield.value)
1452 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001455 case YieldFrom_kind:
1456 VISIT(st, expr, e->v.YieldFrom.value);
1457 st->st_cur->ste_generator = 1;
1458 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001459 case Await_kind:
1460 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001461 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001462 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 case Compare_kind:
1464 VISIT(st, expr, e->v.Compare.left);
1465 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1466 break;
1467 case Call_kind:
1468 VISIT(st, expr, e->v.Call.func);
1469 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001470 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001472 case FormattedValue_kind:
1473 VISIT(st, expr, e->v.FormattedValue.value);
1474 if (e->v.FormattedValue.format_spec)
1475 VISIT(st, expr, e->v.FormattedValue.format_spec);
1476 break;
1477 case JoinedStr_kind:
1478 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1479 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001480 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 case Num_kind:
1482 case Str_kind:
1483 case Bytes_kind:
1484 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001485 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 /* Nothing to do here. */
1487 break;
1488 /* The following exprs can be assignment targets. */
1489 case Attribute_kind:
1490 VISIT(st, expr, e->v.Attribute.value);
1491 break;
1492 case Subscript_kind:
1493 VISIT(st, expr, e->v.Subscript.value);
1494 VISIT(st, slice, e->v.Subscript.slice);
1495 break;
1496 case Starred_kind:
1497 VISIT(st, expr, e->v.Starred.value);
1498 break;
1499 case Name_kind:
1500 if (!symtable_add_def(st, e->v.Name.id,
1501 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001502 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 /* Special-case super: it counts as a use of __class__ */
1504 if (e->v.Name.ctx == Load &&
1505 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001506 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001507 if (!GET_IDENTIFIER(__class__) ||
1508 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001509 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 }
1511 break;
1512 /* child nodes of List and Tuple will have expr_context set */
1513 case List_kind:
1514 VISIT_SEQ(st, expr, e->v.List.elts);
1515 break;
1516 case Tuple_kind:
1517 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1518 break;
1519 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001520 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521}
1522
1523static int
1524symtable_implicit_arg(struct symtable *st, int pos)
1525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1527 if (id == NULL)
1528 return 0;
1529 if (!symtable_add_def(st, id, DEF_PARAM)) {
1530 Py_DECREF(id);
1531 return 0;
1532 }
1533 Py_DECREF(id);
1534 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535}
1536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001538symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 if (!args)
1543 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 for (i = 0; i < asdl_seq_LEN(args); i++) {
1546 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1547 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1548 return 0;
1549 }
1550
1551 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552}
1553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001555symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 if (!args)
1560 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 for (i = 0; i < asdl_seq_LEN(args); i++) {
1563 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1564 if (arg->annotation)
1565 VISIT(st, expr, arg->annotation);
1566 }
1567
1568 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001569}
1570
Neal Norwitzc1505362006-12-28 06:47:50 +00001571static int
Yury Selivanov75445082015-05-11 22:57:16 -04001572symtable_visit_annotations(struct symtable *st, stmt_ty s,
1573 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 if (a->args && !symtable_visit_argannotations(st, a->args))
1576 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001577 if (a->vararg && a->vararg->annotation)
1578 VISIT(st, expr, a->vararg->annotation);
1579 if (a->kwarg && a->kwarg->annotation)
1580 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1582 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001583 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001584 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586}
1587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589symtable_visit_arguments(struct symtable *st, arguments_ty a)
1590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 /* skip default arguments inside function block
1592 XXX should ast be different?
1593 */
1594 if (a->args && !symtable_visit_params(st, a->args))
1595 return 0;
1596 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1597 return 0;
1598 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001599 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 return 0;
1601 st->st_cur->ste_varargs = 1;
1602 }
1603 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001604 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 return 0;
1606 st->st_cur->ste_varkeywords = 1;
1607 }
1608 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609}
1610
1611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 if (eh->v.ExceptHandler.type)
1616 VISIT(st, expr, eh->v.ExceptHandler.type);
1617 if (eh->v.ExceptHandler.name)
1618 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1619 return 0;
1620 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1621 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622}
1623
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001624static int
1625symtable_visit_withitem(struct symtable *st, withitem_ty item)
1626{
1627 VISIT(st, expr, item->context_expr);
1628 if (item->optional_vars) {
1629 VISIT(st, expr, item->optional_vars);
1630 }
1631 return 1;
1632}
1633
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636symtable_visit_alias(struct symtable *st, alias_ty a)
1637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001639 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 dotted package name (e.g. spam.eggs)
1641 */
1642 PyObject *store_name;
1643 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001644 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1645 PyUnicode_GET_LENGTH(name), 1);
1646 if (dot != -1) {
1647 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 if (!store_name)
1649 return 0;
1650 }
1651 else {
1652 store_name = name;
1653 Py_INCREF(store_name);
1654 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001655 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1657 Py_DECREF(store_name);
1658 return r;
1659 }
1660 else {
1661 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001662 int lineno = st->st_cur->ste_lineno;
1663 int col_offset = st->st_cur->ste_col_offset;
1664 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001665 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001666 Py_DECREF(store_name);
1667 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 Py_DECREF(store_name);
1670 return 1;
1671 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672}
1673
1674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 VISIT(st, expr, lc->target);
1679 VISIT(st, expr, lc->iter);
1680 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001681 if (lc->is_async) {
1682 st->st_cur->ste_coroutine = 1;
1683 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685}
1686
1687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689symtable_visit_keyword(struct symtable *st, keyword_ty k)
1690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 VISIT(st, expr, k->value);
1692 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693}
1694
1695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697symtable_visit_slice(struct symtable *st, slice_ty s)
1698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 switch (s->kind) {
1700 case Slice_kind:
1701 if (s->v.Slice.lower)
1702 VISIT(st, expr, s->v.Slice.lower)
1703 if (s->v.Slice.upper)
1704 VISIT(st, expr, s->v.Slice.upper)
1705 if (s->v.Slice.step)
1706 VISIT(st, expr, s->v.Slice.step)
1707 break;
1708 case ExtSlice_kind:
1709 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1710 break;
1711 case Index_kind:
1712 VISIT(st, expr, s->v.Index.value)
1713 break;
1714 }
1715 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716}
1717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001719symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001720 identifier scope_name, asdl_seq *generators,
1721 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 int is_generator = (e->kind == GeneratorExp_kind);
1724 int needs_tmp = !is_generator;
1725 comprehension_ty outermost = ((comprehension_ty)
1726 asdl_seq_GET(generators, 0));
1727 /* Outermost iterator is evaluated in current scope */
1728 VISIT(st, expr, outermost->iter);
1729 /* Create comprehension scope for the rest */
1730 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001731 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1732 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 return 0;
1734 }
1735 st->st_cur->ste_generator = is_generator;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001736 if (outermost->is_async) {
1737 st->st_cur->ste_coroutine = 1;
1738 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 /* Outermost iter is received as an argument */
1740 if (!symtable_implicit_arg(st, 0)) {
1741 symtable_exit_block(st, (void *)e);
1742 return 0;
1743 }
1744 /* Allocate temporary name if needed */
1745 if (needs_tmp && !symtable_new_tmpname(st)) {
1746 symtable_exit_block(st, (void *)e);
1747 return 0;
1748 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001749 VISIT(st, expr, outermost->target);
1750 VISIT_SEQ(st, expr, outermost->ifs);
1751 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001753 VISIT(st, expr, value);
1754 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001759symtable_visit_genexp(struct symtable *st, expr_ty e)
1760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1762 e->v.GeneratorExp.generators,
1763 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001764}
1765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001767symtable_visit_listcomp(struct symtable *st, expr_ty e)
1768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1770 e->v.ListComp.generators,
1771 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001772}
1773
1774static int
1775symtable_visit_setcomp(struct symtable *st, expr_ty e)
1776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1778 e->v.SetComp.generators,
1779 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001780}
1781
1782static int
1783symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1786 e->v.DictComp.generators,
1787 e->v.DictComp.key,
1788 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001789}