blob: 2658b917d7a815ea5237561f5f411b476190929f [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 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070013"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000014
Jeremy Hylton81e95022007-02-27 06:50:52 +000015#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070016"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000017
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070019"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000020
Jeremy Hylton81e95022007-02-27 06:50:52 +000021#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070022"name '%U' is used prior to nonlocal declaration"
23
24#define GLOBAL_ANNOT \
25"annotated name '%U' can't be global"
26
27#define NONLOCAL_ANNOT \
28"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000029
Neal Norwitz5d0ad502005-12-19 04:27:42 +000030#define IMPORT_STAR_WARNING "import * only allowed at module level"
31
Neal Norwitz090b3dd2006-02-28 22:36:46 +000032static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000033ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000034 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020037 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 k = PyLong_FromVoidPtr(key);
40 if (k == NULL)
41 goto fail;
42 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020043 if (ste == NULL) {
44 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020046 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020048 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020051 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 ste->ste_symbols = NULL;
54 ste->ste_varnames = NULL;
55 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000056
Benjamin Petersond9c87022012-10-31 20:26:20 -040057 ste->ste_directives = NULL;
58
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 ste->ste_nested = 0;
61 ste->ste_free = 0;
62 ste->ste_varargs = 0;
63 ste->ste_varkeywords = 0;
64 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000065 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000066 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000068 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 if (st->st_cur != NULL &&
71 (st->st_cur->ste_nested ||
72 st->st_cur->ste_type == FunctionBlock))
73 ste->ste_nested = 1;
74 ste->ste_child_free = 0;
75 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070076 ste->ste_coroutine = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050078 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000079
Victor Stinner9a4fb662013-07-11 22:49:00 +020080 ste->ste_symbols = PyDict_New();
81 ste->ste_varnames = PyList_New(0);
82 ste->ste_children = PyList_New(0);
83 if (ste->ste_symbols == NULL
84 || ste->ste_varnames == NULL
85 || ste->ste_children == NULL)
86 goto fail;
87
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
89 goto fail;
90
91 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 Py_XDECREF(ste);
94 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000095}
96
97static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
101 ste->ste_name,
102 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000103}
104
105static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 ste->ste_table = NULL;
109 Py_XDECREF(ste->ste_id);
110 Py_XDECREF(ste->ste_name);
111 Py_XDECREF(ste->ste_symbols);
112 Py_XDECREF(ste->ste_varnames);
113 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400114 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000116}
117
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119
Guido van Rossum6f799372001-09-20 20:46:19 +0000120static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 {"id", T_OBJECT, OFF(ste_id), READONLY},
122 {"name", T_OBJECT, OFF(ste_name), READONLY},
123 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
124 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
125 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 {"nested", T_INT, OFF(ste_nested), READONLY},
127 {"type", T_INT, OFF(ste_type), READONLY},
128 {"lineno", T_INT, OFF(ste_lineno), READONLY},
129 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000130};
131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000132PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 PyVarObject_HEAD_INIT(&PyType_Type, 0)
134 "symtable entry",
135 sizeof(PySTEntryObject),
136 0,
137 (destructor)ste_dealloc, /* tp_dealloc */
138 0, /* tp_print */
139 0, /* tp_getattr */
140 0, /* tp_setattr */
141 0, /* tp_reserved */
142 (reprfunc)ste_repr, /* tp_repr */
143 0, /* tp_as_number */
144 0, /* tp_as_sequence */
145 0, /* tp_as_mapping */
146 0, /* tp_hash */
147 0, /* tp_call */
148 0, /* tp_str */
149 PyObject_GenericGetAttr, /* tp_getattro */
150 0, /* tp_setattro */
151 0, /* tp_as_buffer */
152 Py_TPFLAGS_DEFAULT, /* tp_flags */
153 0, /* tp_doc */
154 0, /* tp_traverse */
155 0, /* tp_clear */
156 0, /* tp_richcompare */
157 0, /* tp_weaklistoffset */
158 0, /* tp_iter */
159 0, /* tp_iternext */
160 0, /* tp_methods */
161 ste_memberlist, /* tp_members */
162 0, /* tp_getset */
163 0, /* tp_base */
164 0, /* tp_dict */
165 0, /* tp_descr_get */
166 0, /* tp_descr_set */
167 0, /* tp_dictoffset */
168 0, /* tp_init */
169 0, /* tp_alloc */
170 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000171};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000172
173static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000174static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000175 _Py_block_ty block, void *ast, int lineno,
176 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177static int symtable_exit_block(struct symtable *st, void *ast);
178static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
179static int symtable_visit_expr(struct symtable *st, expr_ty s);
180static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000181static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
182static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000183static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184static int symtable_visit_arguments(struct symtable *st, arguments_ty);
185static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
186static int symtable_visit_alias(struct symtable *st, alias_ty);
187static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
188static int symtable_visit_keyword(struct symtable *st, keyword_ty);
189static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000190static int symtable_visit_params(struct symtable *st, asdl_seq *args);
191static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192static int symtable_implicit_arg(struct symtable *st, int pos);
Yury Selivanov75445082015-05-11 22:57:16 -0400193static int symtable_visit_annotations(struct symtable *st, stmt_ty s, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500194static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196
Nick Coghlan650f0d02007-04-15 12:05:43 +0000197static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500199 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
201#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
204#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000205"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206
207static struct symtable *
208symtable_new(void)
209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
213 if (st == NULL)
214 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 st->st_filename = NULL;
217 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 if ((st->st_stack = PyList_New(0)) == NULL)
220 goto fail;
221 if ((st->st_blocks = PyDict_New()) == NULL)
222 goto fail;
223 st->st_cur = NULL;
224 st->st_private = NULL;
225 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 PySymtable_Free(st);
228 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229}
230
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000231/* When compiling the use of C stack is probably going to be a lot
232 lighter than when executing Python code but still can overflow
233 and causing a Python crash if not checked (e.g. eval("()"*300000)).
234 Using the current recursion limit for the compiler seems too
235 restrictive (it caused at least one test to fail) so a factor is
236 used to allow deeper recursion when compiling an expression.
237
238 Using a scaling factor means this should automatically adjust when
239 the recursion limit is adjusted for small or large C stack allocations.
240*/
241#define COMPILER_STACK_FRAME_SCALE 3
242
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200244PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000246 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 asdl_seq *seq;
248 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000249 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400250 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200253 return NULL;
254 if (filename == NULL) {
255 PySymtable_Free(st);
256 return NULL;
257 }
258 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 st->st_filename = filename;
260 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000261
262 /* Setup recursion depth check counters */
263 tstate = PyThreadState_GET();
264 if (!tstate) {
265 PySymtable_Free(st);
266 return NULL;
267 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400268 /* Be careful here to prevent overflow. */
269 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
270 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
271 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
272 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 /* Make the initial symbol information gathering pass */
275 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000276 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 PySymtable_Free(st);
278 return NULL;
279 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 switch (mod->kind) {
283 case Module_kind:
284 seq = mod->v.Module.body;
285 for (i = 0; i < asdl_seq_LEN(seq); i++)
286 if (!symtable_visit_stmt(st,
287 (stmt_ty)asdl_seq_GET(seq, i)))
288 goto error;
289 break;
290 case Expression_kind:
291 if (!symtable_visit_expr(st, mod->v.Expression.body))
292 goto error;
293 break;
294 case Interactive_kind:
295 seq = mod->v.Interactive.body;
296 for (i = 0; i < asdl_seq_LEN(seq); i++)
297 if (!symtable_visit_stmt(st,
298 (stmt_ty)asdl_seq_GET(seq, i)))
299 goto error;
300 break;
301 case Suite_kind:
302 PyErr_SetString(PyExc_RuntimeError,
303 "this compiler does not handle Suites");
304 goto error;
305 }
306 if (!symtable_exit_block(st, (void *)mod)) {
307 PySymtable_Free(st);
308 return NULL;
309 }
310 /* Make the second symbol analysis pass */
311 if (symtable_analyze(st))
312 return st;
313 PySymtable_Free(st);
314 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 (void) symtable_exit_block(st, (void *)mod);
317 PySymtable_Free(st);
318 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319}
320
Victor Stinner14e461d2013-08-26 22:28:21 +0200321struct symtable *
322PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
323{
324 PyObject *filename;
325 struct symtable *st;
326 filename = PyUnicode_DecodeFSDefault(filename_str);
327 if (filename == NULL)
328 return NULL;
329 st = PySymtable_BuildObject(mod, filename, future);
330 Py_DECREF(filename);
331 return st;
332}
333
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334void
335PySymtable_Free(struct symtable *st)
336{
Victor Stinner14e461d2013-08-26 22:28:21 +0200337 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 Py_XDECREF(st->st_blocks);
339 Py_XDECREF(st->st_stack);
340 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341}
342
343PySTEntryObject *
344PySymtable_Lookup(struct symtable *st, void *key)
345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 k = PyLong_FromVoidPtr(key);
349 if (k == NULL)
350 return NULL;
351 v = PyDict_GetItem(st->st_blocks, k);
352 if (v) {
353 assert(PySTEntry_Check(v));
354 Py_INCREF(v);
355 }
356 else {
357 PyErr_SetString(PyExc_KeyError,
358 "unknown symbol table entry");
359 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 Py_DECREF(k);
362 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363}
364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366PyST_GetScope(PySTEntryObject *ste, PyObject *name)
367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
369 if (!v)
370 return 0;
371 assert(PyLong_Check(v));
372 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373}
374
Benjamin Petersond9c87022012-10-31 20:26:20 -0400375static int
376error_at_directive(PySTEntryObject *ste, PyObject *name)
377{
378 Py_ssize_t i;
379 PyObject *data;
380 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600381 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400382 data = PyList_GET_ITEM(ste->ste_directives, i);
383 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600384 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
385 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
386 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
387 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
388 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
389
390 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700391 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400392 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600393 PyErr_SetString(PyExc_RuntimeError,
394 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400395 return 0;
396}
397
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398
399/* Analyze raw symbol information to determine scope of each name.
400
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000401 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407 explicit global is declared with the global statement. An implicit
408 global is a free variable for which the compiler has found no binding
409 in an enclosing function scope. The implicit global is either a global
410 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
411 to handle these names to implement slightly odd semantics. In such a
412 block, the name is treated as global until it is assigned to; then it
413 is treated as a local.
414
415 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000416 The first pass collects raw facts from the AST via the symtable_visit_*
417 functions: the name is a parameter here, the name is used but not defined
418 here, etc. The second pass analyzes these facts during a pass over the
419 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420
421 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000423 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000424 Names which are explicitly declared nonlocal must exist in this set of
425 visible names - if they do not, a syntax error is raised. After doing
426 the local analysis, it analyzes each of its child blocks using an
427 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428
Nick Coghlan650f0d02007-04-15 12:05:43 +0000429 The children update the free variable set. If a local variable is added to
430 the free variable set by the child, the variable is marked as a cell. The
431 function object being defined must provide runtime storage for the variable
432 that may outlive the function's frame. Cell variables are removed from the
433 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000434
Nick Coghlan650f0d02007-04-15 12:05:43 +0000435 During analysis, the names are:
436 symbols: dict mapping from symbol names to flag values (including offset scope values)
437 scopes: dict mapping from symbol names to scope values (no offset)
438 local: set of all symbol names local to the current scope
439 bound: set of all symbol names local to a containing function scope
440 free: set of all symbol names referenced but not bound in child scopes
441 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442*/
443
444#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyObject *o = PyLong_FromLong(I); \
446 if (!o) \
447 return 0; \
448 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
449 Py_DECREF(o); \
450 return 0; \
451 } \
452 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453}
454
455/* Decide on scope of name, given flags.
456
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000457 The namespace dictionaries may be modified to record information
458 about the new name. For example, a new global will add an entry to
459 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460*/
461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000463analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 PyObject *bound, PyObject *local, PyObject *free,
465 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (flags & DEF_GLOBAL) {
468 if (flags & DEF_PARAM) {
469 PyErr_Format(PyExc_SyntaxError,
470 "name '%U' is parameter and global",
471 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400472 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000473 }
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) {
488 if (flags & DEF_PARAM) {
489 PyErr_Format(PyExc_SyntaxError,
490 "name '%U' is parameter and nonlocal",
491 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400492 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 }
494 if (!bound) {
495 PyErr_Format(PyExc_SyntaxError,
496 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400497 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 }
499 if (!PySet_Contains(bound, name)) {
500 PyErr_Format(PyExc_SyntaxError,
501 "no binding for nonlocal '%U' found",
502 name);
503
Benjamin Petersond9c87022012-10-31 20:26:20 -0400504 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 }
506 SET_SCOPE(scopes, name, FREE);
507 ste->ste_free = 1;
508 return PySet_Add(free, name) >= 0;
509 }
510 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000511 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 if (PySet_Add(local, name) < 0)
513 return 0;
514 if (PySet_Discard(global, name) < 0)
515 return 0;
516 return 1;
517 }
518 /* If an enclosing block has a binding for this name, it
519 is a free variable rather than a global variable.
520 Note that having a non-NULL bound implies that the block
521 is nested.
522 */
523 if (bound && PySet_Contains(bound, name)) {
524 SET_SCOPE(scopes, name, FREE);
525 ste->ste_free = 1;
526 return PySet_Add(free, name) >= 0;
527 }
528 /* If a parent has a global statement, then call it global
529 explicit? It could also be global implicit.
530 */
531 if (global && PySet_Contains(global, name)) {
532 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
533 return 1;
534 }
535 if (ste->ste_nested)
536 ste->ste_free = 1;
537 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
538 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539}
540
541#undef SET_SCOPE
542
543/* If a name is defined in free and also in locals, then this block
544 provides the binding for the free variable. The name should be
545 marked CELL in this block and removed from the free list.
546
547 Note that the current block's free variables are included in free.
548 That's safe because no name can be free and local in the same scope.
549*/
550
551static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500552analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 PyObject *name, *v, *v_cell;
555 int success = 0;
556 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 v_cell = PyLong_FromLong(CELL);
559 if (!v_cell)
560 return 0;
561 while (PyDict_Next(scopes, &pos, &name, &v)) {
562 long scope;
563 assert(PyLong_Check(v));
564 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000565 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 continue;
567 if (!PySet_Contains(free, name))
568 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 /* Replace LOCAL with CELL for this name, and remove
570 from free. It is safe to replace the value of name
571 in the dict, because it will not cause a resize.
572 */
573 if (PyDict_SetItem(scopes, name, v_cell) < 0)
574 goto error;
575 if (PySet_Discard(free, name) < 0)
576 goto error;
577 }
578 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 Py_DECREF(v_cell);
581 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582}
583
Benjamin Peterson312595c2013-05-15 15:26:42 -0500584static int
585drop_class_free(PySTEntryObject *ste, PyObject *free)
586{
587 int res;
588 if (!GET_IDENTIFIER(__class__))
589 return 0;
590 res = PySet_Discard(free, __class__);
591 if (res < 0)
592 return 0;
593 if (res)
594 ste->ste_needs_class_closure = 1;
595 return 1;
596}
597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598/* Enter the final scope information into the ste_symbols dict.
599 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600 * All arguments are dicts. Modifies symbols, others are read-only.
601*/
602static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 PyObject *name = NULL, *itr = NULL;
607 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
608 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 /* Update scope information for all symbols in this scope */
611 while (PyDict_Next(symbols, &pos, &name, &v)) {
612 long scope, flags;
613 assert(PyLong_Check(v));
614 flags = PyLong_AS_LONG(v);
615 v_scope = PyDict_GetItem(scopes, name);
616 assert(v_scope && PyLong_Check(v_scope));
617 scope = PyLong_AS_LONG(v_scope);
618 flags |= (scope << SCOPE_OFFSET);
619 v_new = PyLong_FromLong(flags);
620 if (!v_new)
621 return 0;
622 if (PyDict_SetItem(symbols, name, v_new) < 0) {
623 Py_DECREF(v_new);
624 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 Py_DECREF(v_new);
627 }
628
629 /* Record not yet resolved free variables from children (if any) */
630 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
631 if (!v_free)
632 return 0;
633
634 itr = PyObject_GetIter(free);
635 if (!itr)
636 goto error;
637
638 while ((name = PyIter_Next(itr))) {
639 v = PyDict_GetItem(symbols, name);
640
641 /* Handle symbol that already exists in this scope */
642 if (v) {
643 /* Handle a free variable in a method of
644 the class that has the same name as a local
645 or global in the class scope.
646 */
647 if (classflag &&
648 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
649 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
650 v_new = PyLong_FromLong(flags);
651 if (!v_new) {
652 goto error;
653 }
654 if (PyDict_SetItem(symbols, name, v_new) < 0) {
655 Py_DECREF(v_new);
656 goto error;
657 }
658 Py_DECREF(v_new);
659 }
660 /* It's a cell, or already free in this scope */
661 Py_DECREF(name);
662 continue;
663 }
664 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200665 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 Py_DECREF(name);
667 continue; /* it's a global */
668 }
669 /* Propagate new free symbol up the lexical stack */
670 if (PyDict_SetItem(symbols, name, v_free) < 0) {
671 goto error;
672 }
673 Py_DECREF(name);
674 }
675 Py_DECREF(itr);
676 Py_DECREF(v_free);
677 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000678error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 Py_XDECREF(v_free);
680 Py_XDECREF(itr);
681 Py_XDECREF(name);
682 return 0;
683}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684
685/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000686
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 Arguments:
688 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000689 bound -- set of variables bound in enclosing scopes (input). bound
690 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691 free -- set of free variables in enclosed scopes (output)
692 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000693
694 The implementation uses two mutually recursive functions,
695 analyze_block() and analyze_child_block(). analyze_block() is
696 responsible for analyzing the individual names defined in a block.
697 analyze_child_block() prepares temporary namespace dictionaries
698 used to evaluated nested blocks.
699
700 The two functions exist because a child block should see the name
701 bindings of its enclosing blocks, but those bindings should not
702 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703*/
704
705static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
707 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000708
709static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
711 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
714 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
715 PyObject *temp;
716 int i, success = 0;
717 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 local = PySet_New(NULL); /* collect new names bound in block */
720 if (!local)
721 goto error;
722 scopes = PyDict_New(); /* collect scopes defined for each name */
723 if (!scopes)
724 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 /* Allocate new global and bound variable dictionaries. These
727 dictionaries hold the names visible in nested blocks. For
728 ClassBlocks, the bound and global names are initialized
729 before analyzing names, because class bindings aren't
730 visible in methods. For other blocks, they are initialized
731 after names are analyzed.
732 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 /* TODO(jhylton): Package these dicts in a struct so that we
735 can write reasonable helper functions?
736 */
737 newglobal = PySet_New(NULL);
738 if (!newglobal)
739 goto error;
740 newfree = PySet_New(NULL);
741 if (!newfree)
742 goto error;
743 newbound = PySet_New(NULL);
744 if (!newbound)
745 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 /* Class namespace has no effect on names visible in
748 nested functions, so populate the global and bound
749 sets to be passed to child blocks before analyzing
750 this one.
751 */
752 if (ste->ste_type == ClassBlock) {
753 /* Pass down known globals */
754 temp = PyNumber_InPlaceOr(newglobal, global);
755 if (!temp)
756 goto error;
757 Py_DECREF(temp);
758 /* Pass down previously bound symbols */
759 if (bound) {
760 temp = PyNumber_InPlaceOr(newbound, bound);
761 if (!temp)
762 goto error;
763 Py_DECREF(temp);
764 }
765 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
768 long flags = PyLong_AS_LONG(v);
769 if (!analyze_name(ste, scopes, name, flags,
770 bound, local, free, global))
771 goto error;
772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 /* Populate global and bound sets to be passed to children. */
775 if (ste->ste_type != ClassBlock) {
776 /* Add function locals to bound set */
777 if (ste->ste_type == FunctionBlock) {
778 temp = PyNumber_InPlaceOr(newbound, local);
779 if (!temp)
780 goto error;
781 Py_DECREF(temp);
782 }
783 /* Pass down previously bound symbols */
784 if (bound) {
785 temp = PyNumber_InPlaceOr(newbound, bound);
786 if (!temp)
787 goto error;
788 Py_DECREF(temp);
789 }
790 /* Pass down known globals */
791 temp = PyNumber_InPlaceOr(newglobal, global);
792 if (!temp)
793 goto error;
794 Py_DECREF(temp);
795 }
796 else {
797 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000798 if (!GET_IDENTIFIER(__class__))
799 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 if (PySet_Add(newbound, __class__) < 0)
801 goto error;
802 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300804 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 newbound, newglobal now contain the names visible in
807 nested blocks. The free variables in the children will
808 be collected in allfree.
809 */
810 allfree = PySet_New(NULL);
811 if (!allfree)
812 goto error;
813 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
814 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
815 PySTEntryObject* entry;
816 assert(c && PySTEntry_Check(c));
817 entry = (PySTEntryObject*)c;
818 if (!analyze_child_block(entry, newbound, newfree, newglobal,
819 allfree))
820 goto error;
821 /* Check if any children have free variables */
822 if (entry->ste_free || entry->ste_child_free)
823 ste->ste_child_free = 1;
824 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 temp = PyNumber_InPlaceOr(newfree, allfree);
827 if (!temp)
828 goto error;
829 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500832 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500834 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 goto error;
836 /* Records the results of the analysis in the symbol table entry */
837 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
838 ste->ste_type == ClassBlock))
839 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 temp = PyNumber_InPlaceOr(free, newfree);
842 if (!temp)
843 goto error;
844 Py_DECREF(temp);
845 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 Py_XDECREF(scopes);
848 Py_XDECREF(local);
849 Py_XDECREF(newbound);
850 Py_XDECREF(newglobal);
851 Py_XDECREF(newfree);
852 Py_XDECREF(allfree);
853 if (!success)
854 assert(PyErr_Occurred());
855 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856}
857
858static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
860 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
863 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000866
Martin Panter3ee62702016-06-04 04:57:19 +0000867 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 current block. The analyze_block() call modifies these
869 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 */
872 temp_bound = PySet_New(bound);
873 if (!temp_bound)
874 goto error;
875 temp_free = PySet_New(free);
876 if (!temp_free)
877 goto error;
878 temp_global = PySet_New(global);
879 if (!temp_global)
880 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
883 goto error;
884 temp = PyNumber_InPlaceOr(child_free, temp_free);
885 if (!temp)
886 goto error;
887 Py_DECREF(temp);
888 Py_DECREF(temp_bound);
889 Py_DECREF(temp_free);
890 Py_DECREF(temp_global);
891 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000892 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 Py_XDECREF(temp_bound);
894 Py_XDECREF(temp_free);
895 Py_XDECREF(temp_global);
896 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000897}
898
899static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900symtable_analyze(struct symtable *st)
901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 PyObject *free, *global;
903 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 free = PySet_New(NULL);
906 if (!free)
907 return 0;
908 global = PySet_New(NULL);
909 if (!global) {
910 Py_DECREF(free);
911 return 0;
912 }
913 r = analyze_block(st->st_top, NULL, free, global);
914 Py_DECREF(free);
915 Py_DECREF(global);
916 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917}
918
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000919/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920 This reference is released when the block is exited, via the DECREF
921 in symtable_exit_block().
922*/
923
924static int
925symtable_exit_block(struct symtable *st, void *ast)
926{
Benjamin Peterson609da582011-06-29 22:52:39 -0500927 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928
Benjamin Peterson609da582011-06-29 22:52:39 -0500929 st->st_cur = NULL;
930 size = PyList_GET_SIZE(st->st_stack);
931 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500932 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500934 if (--size)
935 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 }
937 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938}
939
940static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000942 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943{
Benjamin Peterson609da582011-06-29 22:52:39 -0500944 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945
Benjamin Peterson609da582011-06-29 22:52:39 -0500946 ste = ste_new(st, name, block, ast, lineno, col_offset);
947 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500949 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
950 Py_DECREF(ste);
951 return 0;
952 }
953 prev = st->st_cur;
954 /* The entry is owned by the stack. Borrow it for st_cur. */
955 Py_DECREF(ste);
956 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000957 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 st->st_global = st->st_cur->ste_symbols;
959 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500960 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 return 0;
962 }
963 }
964 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965}
966
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000967static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968symtable_lookup(struct symtable *st, PyObject *name)
969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 PyObject *o;
971 PyObject *mangled = _Py_Mangle(st->st_private, name);
972 if (!mangled)
973 return 0;
974 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
975 Py_DECREF(mangled);
976 if (!o)
977 return 0;
978 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979}
980
981static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 PyObject *o;
985 PyObject *dict;
986 long val;
987 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988
Jeremy Hylton81e95022007-02-27 06:50:52 +0000989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 if (!mangled)
991 return 0;
992 dict = st->st_cur->ste_symbols;
993 if ((o = PyDict_GetItem(dict, mangled))) {
994 val = PyLong_AS_LONG(o);
995 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
996 /* Is it better to use 'mangled' or 'name' here? */
997 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +0200998 PyErr_SyntaxLocationObject(st->st_filename,
999 st->st_cur->ste_lineno,
1000 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 goto error;
1002 }
1003 val |= flag;
1004 } else
1005 val = flag;
1006 o = PyLong_FromLong(val);
1007 if (o == NULL)
1008 goto error;
1009 if (PyDict_SetItem(dict, mangled, o) < 0) {
1010 Py_DECREF(o);
1011 goto error;
1012 }
1013 Py_DECREF(o);
1014
1015 if (flag & DEF_PARAM) {
1016 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1017 goto error;
1018 } else if (flag & DEF_GLOBAL) {
1019 /* XXX need to update DEF_GLOBAL for other flags too;
1020 perhaps only DEF_FREE_GLOBAL */
1021 val = flag;
1022 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1023 val |= PyLong_AS_LONG(o);
1024 }
1025 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 goto error;
1028 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1029 Py_DECREF(o);
1030 goto error;
1031 }
1032 Py_DECREF(o);
1033 }
1034 Py_DECREF(mangled);
1035 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001036
1037error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 Py_DECREF(mangled);
1039 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040}
1041
1042/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1043 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 function.
1045
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1047 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001048
1049 VISIT_QUIT macro returns the specified value exiting from the function but
1050 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051*/
1052
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001053#define VISIT_QUIT(ST, X) \
1054 return --(ST)->recursion_depth,(X)
1055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001058 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 int i; \
1062 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1063 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1064 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1065 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001066 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 int i; \
1072 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1073 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1074 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1075 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001076 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001079
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001080#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001082 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001084 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001086 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001087 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001089}
1090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001092symtable_new_tmpname(struct symtable *st)
1093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 char tmpname[256];
1095 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1098 ++st->st_cur->ste_tmpname);
1099 tmp = PyUnicode_InternFromString(tmpname);
1100 if (!tmp)
1101 return 0;
1102 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1103 return 0;
1104 Py_DECREF(tmp);
1105 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001106}
1107
Guido van Rossum4f72a782006-10-27 23:31:49 +00001108
Guido van Rossumc2e20742006-02-27 22:32:47 +00001109static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001110symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1111{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001112 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001113 int res;
1114 if (!st->st_cur->ste_directives) {
1115 st->st_cur->ste_directives = PyList_New(0);
1116 if (!st->st_cur->ste_directives)
1117 return 0;
1118 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001119 mangled = _Py_Mangle(st->st_private, name);
1120 if (!mangled)
1121 return 0;
1122 data = Py_BuildValue("(Nii)", mangled, s->lineno, s->col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001123 if (!data)
1124 return 0;
1125 res = PyList_Append(st->st_cur->ste_directives, data);
1126 Py_DECREF(data);
1127 return res == 0;
1128}
1129
1130
1131static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132symtable_visit_stmt(struct symtable *st, stmt_ty s)
1133{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001134 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001135 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001136 "maximum recursion depth exceeded during compilation");
1137 VISIT_QUIT(st, 0);
1138 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 switch (s->kind) {
1140 case FunctionDef_kind:
1141 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001142 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 if (s->v.FunctionDef.args->defaults)
1144 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1145 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001146 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001147 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1148 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001149 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 if (s->v.FunctionDef.decorator_list)
1151 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1152 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001153 FunctionBlock, (void *)s, s->lineno,
1154 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001155 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001156 VISIT(st, arguments, s->v.FunctionDef.args);
1157 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001159 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 break;
1161 case ClassDef_kind: {
1162 PyObject *tmp;
1163 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001164 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1166 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (s->v.ClassDef.decorator_list)
1168 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1169 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001170 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001171 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 tmp = st->st_private;
1173 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001174 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 st->st_private = tmp;
1176 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001177 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 break;
1179 }
1180 case Return_kind:
1181 if (s->v.Return.value) {
1182 VISIT(st, expr, s->v.Return.value);
1183 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 }
1185 break;
1186 case Delete_kind:
1187 VISIT_SEQ(st, expr, s->v.Delete.targets);
1188 break;
1189 case Assign_kind:
1190 VISIT_SEQ(st, expr, s->v.Assign.targets);
1191 VISIT(st, expr, s->v.Assign.value);
1192 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001193 case AnnAssign_kind:
1194 if (s->v.AnnAssign.target->kind == Name_kind) {
1195 expr_ty e_name = s->v.AnnAssign.target;
1196 long cur = symtable_lookup(st, e_name->v.Name.id);
1197 if (cur < 0) {
1198 VISIT_QUIT(st, 0);
1199 }
1200 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
1201 && s->v.AnnAssign.simple) {
1202 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001203 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1204 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001205 PyErr_SyntaxLocationObject(st->st_filename,
1206 s->lineno,
1207 s->col_offset);
1208 VISIT_QUIT(st, 0);
1209 }
1210 if (s->v.AnnAssign.simple &&
1211 !symtable_add_def(st, e_name->v.Name.id,
1212 DEF_ANNOT | DEF_LOCAL)) {
1213 VISIT_QUIT(st, 0);
1214 }
1215 else {
1216 if (s->v.AnnAssign.value
1217 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1218 VISIT_QUIT(st, 0);
1219 }
1220 }
1221 }
1222 else {
1223 VISIT(st, expr, s->v.AnnAssign.target);
1224 }
1225 VISIT(st, expr, s->v.AnnAssign.annotation);
1226 if (s->v.AnnAssign.value) {
1227 VISIT(st, expr, s->v.AnnAssign.value);
1228 }
1229 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 case AugAssign_kind:
1231 VISIT(st, expr, s->v.AugAssign.target);
1232 VISIT(st, expr, s->v.AugAssign.value);
1233 break;
1234 case For_kind:
1235 VISIT(st, expr, s->v.For.target);
1236 VISIT(st, expr, s->v.For.iter);
1237 VISIT_SEQ(st, stmt, s->v.For.body);
1238 if (s->v.For.orelse)
1239 VISIT_SEQ(st, stmt, s->v.For.orelse);
1240 break;
1241 case While_kind:
1242 VISIT(st, expr, s->v.While.test);
1243 VISIT_SEQ(st, stmt, s->v.While.body);
1244 if (s->v.While.orelse)
1245 VISIT_SEQ(st, stmt, s->v.While.orelse);
1246 break;
1247 case If_kind:
1248 /* XXX if 0: and lookup_yield() hacks */
1249 VISIT(st, expr, s->v.If.test);
1250 VISIT_SEQ(st, stmt, s->v.If.body);
1251 if (s->v.If.orelse)
1252 VISIT_SEQ(st, stmt, s->v.If.orelse);
1253 break;
1254 case Raise_kind:
1255 if (s->v.Raise.exc) {
1256 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001257 if (s->v.Raise.cause) {
1258 VISIT(st, expr, s->v.Raise.cause);
1259 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 }
1261 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001262 case Try_kind:
1263 VISIT_SEQ(st, stmt, s->v.Try.body);
1264 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1265 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1266 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 break;
1268 case Assert_kind:
1269 VISIT(st, expr, s->v.Assert.test);
1270 if (s->v.Assert.msg)
1271 VISIT(st, expr, s->v.Assert.msg);
1272 break;
1273 case Import_kind:
1274 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 break;
1276 case ImportFrom_kind:
1277 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 break;
1279 case Global_kind: {
1280 int i;
1281 asdl_seq *seq = s->v.Global.names;
1282 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1283 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 long cur = symtable_lookup(st, name);
1285 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001286 VISIT_QUIT(st, 0);
Guido van Rossum6cff8742016-09-09 09:36:26 -07001287 if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) {
1288 char* msg;
Christian Heimes517507c2016-09-23 20:26:30 +02001289 if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001290 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001291 } else if (cur & DEF_ANNOT) {
1292 msg = GLOBAL_ANNOT;
1293 } else { /* DEF_LOCAL */
1294 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001295 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001296 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001297 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001298 PyErr_SyntaxLocationObject(st->st_filename,
1299 s->lineno,
1300 s->col_offset);
1301 VISIT_QUIT(st, 0);
1302 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001304 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001305 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001306 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 }
1308 break;
1309 }
1310 case Nonlocal_kind: {
1311 int i;
1312 asdl_seq *seq = s->v.Nonlocal.names;
1313 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1314 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 long cur = symtable_lookup(st, name);
1316 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001317 VISIT_QUIT(st, 0);
Guido van Rossum6cff8742016-09-09 09:36:26 -07001318 if (cur & (DEF_LOCAL | USE | DEF_ANNOT)) {
1319 char* msg;
Christian Heimes517507c2016-09-23 20:26:30 +02001320 if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001321 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001322 } else if (cur & DEF_ANNOT) {
1323 msg = NONLOCAL_ANNOT;
1324 } else { /* DEF_LOCAL */
1325 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001326 }
1327 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001328 PyErr_SyntaxLocationObject(st->st_filename,
1329 s->lineno,
1330 s->col_offset);
1331 VISIT_QUIT(st, 0);
1332 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001334 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001335 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001336 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 }
1338 break;
1339 }
1340 case Expr_kind:
1341 VISIT(st, expr, s->v.Expr.value);
1342 break;
1343 case Pass_kind:
1344 case Break_kind:
1345 case Continue_kind:
1346 /* nothing to do here */
1347 break;
1348 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001349 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 VISIT_SEQ(st, stmt, s->v.With.body);
1351 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001352 case AsyncFunctionDef_kind:
1353 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1354 VISIT_QUIT(st, 0);
1355 if (s->v.AsyncFunctionDef.args->defaults)
1356 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1357 if (s->v.AsyncFunctionDef.args->kw_defaults)
1358 VISIT_SEQ_WITH_NULL(st, expr,
1359 s->v.AsyncFunctionDef.args->kw_defaults);
1360 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1361 s->v.AsyncFunctionDef.returns))
1362 VISIT_QUIT(st, 0);
1363 if (s->v.AsyncFunctionDef.decorator_list)
1364 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1365 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1366 FunctionBlock, (void *)s, s->lineno,
1367 s->col_offset))
1368 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001369 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001370 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1371 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1372 if (!symtable_exit_block(st, s))
1373 VISIT_QUIT(st, 0);
1374 break;
1375 case AsyncWith_kind:
1376 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1377 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1378 break;
1379 case AsyncFor_kind:
1380 VISIT(st, expr, s->v.AsyncFor.target);
1381 VISIT(st, expr, s->v.AsyncFor.iter);
1382 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1383 if (s->v.AsyncFor.orelse)
1384 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1385 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001387 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388}
1389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391symtable_visit_expr(struct symtable *st, expr_ty e)
1392{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001393 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001394 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001395 "maximum recursion depth exceeded during compilation");
1396 VISIT_QUIT(st, 0);
1397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 switch (e->kind) {
1399 case BoolOp_kind:
1400 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1401 break;
1402 case BinOp_kind:
1403 VISIT(st, expr, e->v.BinOp.left);
1404 VISIT(st, expr, e->v.BinOp.right);
1405 break;
1406 case UnaryOp_kind:
1407 VISIT(st, expr, e->v.UnaryOp.operand);
1408 break;
1409 case Lambda_kind: {
1410 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001411 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (e->v.Lambda.args->defaults)
1413 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001414 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001415 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001417 FunctionBlock, (void *)e, e->lineno,
1418 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001419 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001420 VISIT(st, arguments, e->v.Lambda.args);
1421 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001423 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 break;
1425 }
1426 case IfExp_kind:
1427 VISIT(st, expr, e->v.IfExp.test);
1428 VISIT(st, expr, e->v.IfExp.body);
1429 VISIT(st, expr, e->v.IfExp.orelse);
1430 break;
1431 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001432 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 VISIT_SEQ(st, expr, e->v.Dict.values);
1434 break;
1435 case Set_kind:
1436 VISIT_SEQ(st, expr, e->v.Set.elts);
1437 break;
1438 case GeneratorExp_kind:
1439 if (!symtable_visit_genexp(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 ListComp_kind:
1443 if (!symtable_visit_listcomp(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 SetComp_kind:
1447 if (!symtable_visit_setcomp(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 DictComp_kind:
1451 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001452 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 break;
1454 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001455 if (e->v.Yield.value)
1456 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001459 case YieldFrom_kind:
1460 VISIT(st, expr, e->v.YieldFrom.value);
1461 st->st_cur->ste_generator = 1;
1462 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001463 case Await_kind:
1464 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001465 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001466 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 case Compare_kind:
1468 VISIT(st, expr, e->v.Compare.left);
1469 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1470 break;
1471 case Call_kind:
1472 VISIT(st, expr, e->v.Call.func);
1473 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001474 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001476 case FormattedValue_kind:
1477 VISIT(st, expr, e->v.FormattedValue.value);
1478 if (e->v.FormattedValue.format_spec)
1479 VISIT(st, expr, e->v.FormattedValue.format_spec);
1480 break;
1481 case JoinedStr_kind:
1482 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1483 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001484 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 case Num_kind:
1486 case Str_kind:
1487 case Bytes_kind:
1488 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001489 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 /* Nothing to do here. */
1491 break;
1492 /* The following exprs can be assignment targets. */
1493 case Attribute_kind:
1494 VISIT(st, expr, e->v.Attribute.value);
1495 break;
1496 case Subscript_kind:
1497 VISIT(st, expr, e->v.Subscript.value);
1498 VISIT(st, slice, e->v.Subscript.slice);
1499 break;
1500 case Starred_kind:
1501 VISIT(st, expr, e->v.Starred.value);
1502 break;
1503 case Name_kind:
1504 if (!symtable_add_def(st, e->v.Name.id,
1505 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001506 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 /* Special-case super: it counts as a use of __class__ */
1508 if (e->v.Name.ctx == Load &&
1509 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001510 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001511 if (!GET_IDENTIFIER(__class__) ||
1512 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001513 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 }
1515 break;
1516 /* child nodes of List and Tuple will have expr_context set */
1517 case List_kind:
1518 VISIT_SEQ(st, expr, e->v.List.elts);
1519 break;
1520 case Tuple_kind:
1521 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1522 break;
1523 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001524 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
1526
1527static int
1528symtable_implicit_arg(struct symtable *st, int pos)
1529{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1531 if (id == NULL)
1532 return 0;
1533 if (!symtable_add_def(st, id, DEF_PARAM)) {
1534 Py_DECREF(id);
1535 return 0;
1536 }
1537 Py_DECREF(id);
1538 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539}
1540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001542symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 if (!args)
1547 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 for (i = 0; i < asdl_seq_LEN(args); i++) {
1550 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1551 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1552 return 0;
1553 }
1554
1555 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556}
1557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001559symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 if (!args)
1564 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 for (i = 0; i < asdl_seq_LEN(args); i++) {
1567 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1568 if (arg->annotation)
1569 VISIT(st, expr, arg->annotation);
1570 }
1571
1572 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001573}
1574
Neal Norwitzc1505362006-12-28 06:47:50 +00001575static int
Yury Selivanov75445082015-05-11 22:57:16 -04001576symtable_visit_annotations(struct symtable *st, stmt_ty s,
1577 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001578{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 if (a->args && !symtable_visit_argannotations(st, a->args))
1580 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001581 if (a->vararg && a->vararg->annotation)
1582 VISIT(st, expr, a->vararg->annotation);
1583 if (a->kwarg && a->kwarg->annotation)
1584 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1586 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001587 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001588 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590}
1591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593symtable_visit_arguments(struct symtable *st, arguments_ty a)
1594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 /* skip default arguments inside function block
1596 XXX should ast be different?
1597 */
1598 if (a->args && !symtable_visit_params(st, a->args))
1599 return 0;
1600 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1601 return 0;
1602 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001603 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 return 0;
1605 st->st_cur->ste_varargs = 1;
1606 }
1607 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001608 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 return 0;
1610 st->st_cur->ste_varkeywords = 1;
1611 }
1612 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613}
1614
1615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 if (eh->v.ExceptHandler.type)
1620 VISIT(st, expr, eh->v.ExceptHandler.type);
1621 if (eh->v.ExceptHandler.name)
1622 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1623 return 0;
1624 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1625 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626}
1627
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001628static int
1629symtable_visit_withitem(struct symtable *st, withitem_ty item)
1630{
1631 VISIT(st, expr, item->context_expr);
1632 if (item->optional_vars) {
1633 VISIT(st, expr, item->optional_vars);
1634 }
1635 return 1;
1636}
1637
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640symtable_visit_alias(struct symtable *st, alias_ty a)
1641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001643 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 dotted package name (e.g. spam.eggs)
1645 */
1646 PyObject *store_name;
1647 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001648 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1649 PyUnicode_GET_LENGTH(name), 1);
1650 if (dot != -1) {
1651 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 if (!store_name)
1653 return 0;
1654 }
1655 else {
1656 store_name = name;
1657 Py_INCREF(store_name);
1658 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001659 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1661 Py_DECREF(store_name);
1662 return r;
1663 }
1664 else {
1665 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001666 int lineno = st->st_cur->ste_lineno;
1667 int col_offset = st->st_cur->ste_col_offset;
1668 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001669 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001670 Py_DECREF(store_name);
1671 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 Py_DECREF(store_name);
1674 return 1;
1675 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676}
1677
1678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 VISIT(st, expr, lc->target);
1683 VISIT(st, expr, lc->iter);
1684 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001685 if (lc->is_async) {
1686 st->st_cur->ste_coroutine = 1;
1687 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689}
1690
1691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693symtable_visit_keyword(struct symtable *st, keyword_ty k)
1694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 VISIT(st, expr, k->value);
1696 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697}
1698
1699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701symtable_visit_slice(struct symtable *st, slice_ty s)
1702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 switch (s->kind) {
1704 case Slice_kind:
1705 if (s->v.Slice.lower)
1706 VISIT(st, expr, s->v.Slice.lower)
1707 if (s->v.Slice.upper)
1708 VISIT(st, expr, s->v.Slice.upper)
1709 if (s->v.Slice.step)
1710 VISIT(st, expr, s->v.Slice.step)
1711 break;
1712 case ExtSlice_kind:
1713 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1714 break;
1715 case Index_kind:
1716 VISIT(st, expr, s->v.Index.value)
1717 break;
1718 }
1719 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720}
1721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001723symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001724 identifier scope_name, asdl_seq *generators,
1725 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 int is_generator = (e->kind == GeneratorExp_kind);
1728 int needs_tmp = !is_generator;
1729 comprehension_ty outermost = ((comprehension_ty)
1730 asdl_seq_GET(generators, 0));
1731 /* Outermost iterator is evaluated in current scope */
1732 VISIT(st, expr, outermost->iter);
1733 /* Create comprehension scope for the rest */
1734 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001735 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1736 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 return 0;
1738 }
1739 st->st_cur->ste_generator = is_generator;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001740 if (outermost->is_async) {
1741 st->st_cur->ste_coroutine = 1;
1742 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 /* Outermost iter is received as an argument */
1744 if (!symtable_implicit_arg(st, 0)) {
1745 symtable_exit_block(st, (void *)e);
1746 return 0;
1747 }
1748 /* Allocate temporary name if needed */
1749 if (needs_tmp && !symtable_new_tmpname(st)) {
1750 symtable_exit_block(st, (void *)e);
1751 return 0;
1752 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001753 VISIT(st, expr, outermost->target);
1754 VISIT_SEQ(st, expr, outermost->ifs);
1755 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001757 VISIT(st, expr, value);
1758 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001763symtable_visit_genexp(struct symtable *st, expr_ty e)
1764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1766 e->v.GeneratorExp.generators,
1767 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001768}
1769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001771symtable_visit_listcomp(struct symtable *st, expr_ty e)
1772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1774 e->v.ListComp.generators,
1775 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001776}
1777
1778static int
1779symtable_visit_setcomp(struct symtable *st, expr_ty e)
1780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001781 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1782 e->v.SetComp.generators,
1783 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001784}
1785
1786static int
1787symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1790 e->v.DictComp.generators,
1791 e->v.DictComp.key,
1792 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001793}