blob: 183bf691335f1d0671a6adf32b5e6faf2d5f5939 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
3#include "code.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005#include "structmember.h"
6
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#define GLOBAL_AFTER_ASSIGN \
9"name '%.400s' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000010
Jeremy Hylton81e95022007-02-27 06:50:52 +000011#define NONLOCAL_AFTER_ASSIGN \
12"name '%.400s' is assigned to before nonlocal declaration"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_USE \
15"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_USE \
18"name '%.400s' is used prior to nonlocal declaration"
19
Neal Norwitz5d0ad502005-12-19 04:27:42 +000020#define IMPORT_STAR_WARNING "import * only allowed at module level"
21
Neal Norwitz090b3dd2006-02-28 22:36:46 +000022static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000023ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000024 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020027 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 k = PyLong_FromVoidPtr(key);
30 if (k == NULL)
31 goto fail;
32 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020033 if (ste == NULL) {
34 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020036 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020038 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020041 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 ste->ste_symbols = NULL;
44 ste->ste_varnames = NULL;
45 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000046
Benjamin Petersond9c87022012-10-31 20:26:20 -040047 ste->ste_directives = NULL;
48
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 ste->ste_type = block;
50 ste->ste_unoptimized = 0;
51 ste->ste_nested = 0;
52 ste->ste_free = 0;
53 ste->ste_varargs = 0;
54 ste->ste_varkeywords = 0;
55 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000056 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000057 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000059 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 if (st->st_cur != NULL &&
62 (st->st_cur->ste_nested ||
63 st->st_cur->ste_type == FunctionBlock))
64 ste->ste_nested = 1;
65 ste->ste_child_free = 0;
66 ste->ste_generator = 0;
67 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050068 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069
Victor Stinner9a4fb662013-07-11 22:49:00 +020070 ste->ste_symbols = PyDict_New();
71 ste->ste_varnames = PyList_New(0);
72 ste->ste_children = PyList_New(0);
73 if (ste->ste_symbols == NULL
74 || ste->ste_varnames == NULL
75 || ste->ste_children == NULL)
76 goto fail;
77
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
79 goto fail;
80
81 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 Py_XDECREF(ste);
84 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085}
86
87static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
91 ste->ste_name,
92 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093}
94
95static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 ste->ste_table = NULL;
99 Py_XDECREF(ste->ste_id);
100 Py_XDECREF(ste->ste_name);
101 Py_XDECREF(ste->ste_symbols);
102 Py_XDECREF(ste->ste_varnames);
103 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400104 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000106}
107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109
Guido van Rossum6f799372001-09-20 20:46:19 +0000110static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 {"id", T_OBJECT, OFF(ste_id), READONLY},
112 {"name", T_OBJECT, OFF(ste_name), READONLY},
113 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
114 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
115 {"children", T_OBJECT, OFF(ste_children), READONLY},
116 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
117 {"nested", T_INT, OFF(ste_nested), READONLY},
118 {"type", T_INT, OFF(ste_type), READONLY},
119 {"lineno", T_INT, OFF(ste_lineno), READONLY},
120 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121};
122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 PyVarObject_HEAD_INIT(&PyType_Type, 0)
125 "symtable entry",
126 sizeof(PySTEntryObject),
127 0,
128 (destructor)ste_dealloc, /* tp_dealloc */
129 0, /* tp_print */
130 0, /* tp_getattr */
131 0, /* tp_setattr */
132 0, /* tp_reserved */
133 (reprfunc)ste_repr, /* tp_repr */
134 0, /* tp_as_number */
135 0, /* tp_as_sequence */
136 0, /* tp_as_mapping */
137 0, /* tp_hash */
138 0, /* tp_call */
139 0, /* tp_str */
140 PyObject_GenericGetAttr, /* tp_getattro */
141 0, /* tp_setattro */
142 0, /* tp_as_buffer */
143 Py_TPFLAGS_DEFAULT, /* tp_flags */
144 0, /* tp_doc */
145 0, /* tp_traverse */
146 0, /* tp_clear */
147 0, /* tp_richcompare */
148 0, /* tp_weaklistoffset */
149 0, /* tp_iter */
150 0, /* tp_iternext */
151 0, /* tp_methods */
152 ste_memberlist, /* tp_members */
153 0, /* tp_getset */
154 0, /* tp_base */
155 0, /* tp_dict */
156 0, /* tp_descr_get */
157 0, /* tp_descr_set */
158 0, /* tp_dictoffset */
159 0, /* tp_init */
160 0, /* tp_alloc */
161 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000162};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163
164static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000165static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000167 _Py_block_ty block, void *ast, int lineno,
168 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169static int symtable_exit_block(struct symtable *st, void *ast);
170static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
171static int symtable_visit_expr(struct symtable *st, expr_ty s);
172static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000173static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
174static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000175static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000176static int symtable_visit_arguments(struct symtable *st, arguments_ty);
177static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
178static int symtable_visit_alias(struct symtable *st, alias_ty);
179static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
180static int symtable_visit_keyword(struct symtable *st, keyword_ty);
181static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000182static int symtable_visit_params(struct symtable *st, asdl_seq *args);
183static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000185static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500186static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
188
Nick Coghlan650f0d02007-04-15 12:05:43 +0000189static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500191 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
193#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000197"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
199static struct symtable *
200symtable_new(void)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
205 if (st == NULL)
206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 st->st_filename = NULL;
209 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if ((st->st_stack = PyList_New(0)) == NULL)
212 goto fail;
213 if ((st->st_blocks = PyDict_New()) == NULL)
214 goto fail;
215 st->st_cur = NULL;
216 st->st_private = NULL;
217 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 PySymtable_Free(st);
220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221}
222
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000223/* When compiling the use of C stack is probably going to be a lot
224 lighter than when executing Python code but still can overflow
225 and causing a Python crash if not checked (e.g. eval("()"*300000)).
226 Using the current recursion limit for the compiler seems too
227 restrictive (it caused at least one test to fail) so a factor is
228 used to allow deeper recursion when compiling an expression.
229
230 Using a scaling factor means this should automatically adjust when
231 the recursion limit is adjusted for small or large C stack allocations.
232*/
233#define COMPILER_STACK_FRAME_SCALE 3
234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235struct symtable *
236PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
237{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000238 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 asdl_seq *seq;
240 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000241 PyThreadState *tstate;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 if (st == NULL)
244 return st;
245 st->st_filename = filename;
246 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000247
248 /* Setup recursion depth check counters */
249 tstate = PyThreadState_GET();
250 if (!tstate) {
251 PySymtable_Free(st);
252 return NULL;
253 }
254 st->recursion_depth = tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE;
255 st->recursion_limit = Py_GetRecursionLimit() * COMPILER_STACK_FRAME_SCALE;
256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 /* Make the initial symbol information gathering pass */
258 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000259 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 PySymtable_Free(st);
261 return NULL;
262 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 st->st_top = st->st_cur;
265 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
266 switch (mod->kind) {
267 case Module_kind:
268 seq = mod->v.Module.body;
269 for (i = 0; i < asdl_seq_LEN(seq); i++)
270 if (!symtable_visit_stmt(st,
271 (stmt_ty)asdl_seq_GET(seq, i)))
272 goto error;
273 break;
274 case Expression_kind:
275 if (!symtable_visit_expr(st, mod->v.Expression.body))
276 goto error;
277 break;
278 case Interactive_kind:
279 seq = mod->v.Interactive.body;
280 for (i = 0; i < asdl_seq_LEN(seq); i++)
281 if (!symtable_visit_stmt(st,
282 (stmt_ty)asdl_seq_GET(seq, i)))
283 goto error;
284 break;
285 case Suite_kind:
286 PyErr_SetString(PyExc_RuntimeError,
287 "this compiler does not handle Suites");
288 goto error;
289 }
290 if (!symtable_exit_block(st, (void *)mod)) {
291 PySymtable_Free(st);
292 return NULL;
293 }
294 /* Make the second symbol analysis pass */
295 if (symtable_analyze(st))
296 return st;
297 PySymtable_Free(st);
298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 (void) symtable_exit_block(st, (void *)mod);
301 PySymtable_Free(st);
302 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303}
304
305void
306PySymtable_Free(struct symtable *st)
307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 Py_XDECREF(st->st_blocks);
309 Py_XDECREF(st->st_stack);
310 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311}
312
313PySTEntryObject *
314PySymtable_Lookup(struct symtable *st, void *key)
315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 k = PyLong_FromVoidPtr(key);
319 if (k == NULL)
320 return NULL;
321 v = PyDict_GetItem(st->st_blocks, k);
322 if (v) {
323 assert(PySTEntry_Check(v));
324 Py_INCREF(v);
325 }
326 else {
327 PyErr_SetString(PyExc_KeyError,
328 "unknown symbol table entry");
329 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 Py_DECREF(k);
332 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000333}
334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336PyST_GetScope(PySTEntryObject *ste, PyObject *name)
337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
339 if (!v)
340 return 0;
341 assert(PyLong_Check(v));
342 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343}
344
Benjamin Petersond9c87022012-10-31 20:26:20 -0400345static int
346error_at_directive(PySTEntryObject *ste, PyObject *name)
347{
348 Py_ssize_t i;
349 PyObject *data;
350 assert(ste->ste_directives);
351 for (i = 0; ; i++) {
352 data = PyList_GET_ITEM(ste->ste_directives, i);
353 assert(PyTuple_CheckExact(data));
354 if (PyTuple_GET_ITEM(data, 0) == name)
355 break;
356 }
357 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
358 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
359 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
360 return 0;
361}
362
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363
364/* Analyze raw symbol information to determine scope of each name.
365
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000366 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372 explicit global is declared with the global statement. An implicit
373 global is a free variable for which the compiler has found no binding
374 in an enclosing function scope. The implicit global is either a global
375 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
376 to handle these names to implement slightly odd semantics. In such a
377 block, the name is treated as global until it is assigned to; then it
378 is treated as a local.
379
380 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000381 The first pass collects raw facts from the AST via the symtable_visit_*
382 functions: the name is a parameter here, the name is used but not defined
383 here, etc. The second pass analyzes these facts during a pass over the
384 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385
386 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000388 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000389 Names which are explicitly declared nonlocal must exist in this set of
390 visible names - if they do not, a syntax error is raised. After doing
391 the local analysis, it analyzes each of its child blocks using an
392 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393
Nick Coghlan650f0d02007-04-15 12:05:43 +0000394 The children update the free variable set. If a local variable is added to
395 the free variable set by the child, the variable is marked as a cell. The
396 function object being defined must provide runtime storage for the variable
397 that may outlive the function's frame. Cell variables are removed from the
398 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000399
Nick Coghlan650f0d02007-04-15 12:05:43 +0000400 During analysis, the names are:
401 symbols: dict mapping from symbol names to flag values (including offset scope values)
402 scopes: dict mapping from symbol names to scope values (no offset)
403 local: set of all symbol names local to the current scope
404 bound: set of all symbol names local to a containing function scope
405 free: set of all symbol names referenced but not bound in child scopes
406 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407*/
408
409#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 PyObject *o = PyLong_FromLong(I); \
411 if (!o) \
412 return 0; \
413 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
414 Py_DECREF(o); \
415 return 0; \
416 } \
417 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418}
419
420/* Decide on scope of name, given flags.
421
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000422 The namespace dictionaries may be modified to record information
423 about the new name. For example, a new global will add an entry to
424 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425*/
426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000428analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 PyObject *bound, PyObject *local, PyObject *free,
430 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (flags & DEF_GLOBAL) {
433 if (flags & DEF_PARAM) {
434 PyErr_Format(PyExc_SyntaxError,
435 "name '%U' is parameter and global",
436 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400437 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000438 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (flags & DEF_NONLOCAL) {
440 PyErr_Format(PyExc_SyntaxError,
441 "name '%U' is nonlocal and global",
442 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400443 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 }
445 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
446 if (PySet_Add(global, name) < 0)
447 return 0;
448 if (bound && (PySet_Discard(bound, name) < 0))
449 return 0;
450 return 1;
451 }
452 if (flags & DEF_NONLOCAL) {
453 if (flags & DEF_PARAM) {
454 PyErr_Format(PyExc_SyntaxError,
455 "name '%U' is parameter and nonlocal",
456 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400457 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 }
459 if (!bound) {
460 PyErr_Format(PyExc_SyntaxError,
461 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400462 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 }
464 if (!PySet_Contains(bound, name)) {
465 PyErr_Format(PyExc_SyntaxError,
466 "no binding for nonlocal '%U' found",
467 name);
468
Benjamin Petersond9c87022012-10-31 20:26:20 -0400469 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 }
471 SET_SCOPE(scopes, name, FREE);
472 ste->ste_free = 1;
473 return PySet_Add(free, name) >= 0;
474 }
475 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000476 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 if (PySet_Add(local, name) < 0)
478 return 0;
479 if (PySet_Discard(global, name) < 0)
480 return 0;
481 return 1;
482 }
483 /* If an enclosing block has a binding for this name, it
484 is a free variable rather than a global variable.
485 Note that having a non-NULL bound implies that the block
486 is nested.
487 */
488 if (bound && PySet_Contains(bound, name)) {
489 SET_SCOPE(scopes, name, FREE);
490 ste->ste_free = 1;
491 return PySet_Add(free, name) >= 0;
492 }
493 /* If a parent has a global statement, then call it global
494 explicit? It could also be global implicit.
495 */
496 if (global && PySet_Contains(global, name)) {
497 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
498 return 1;
499 }
500 if (ste->ste_nested)
501 ste->ste_free = 1;
502 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
503 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504}
505
506#undef SET_SCOPE
507
508/* If a name is defined in free and also in locals, then this block
509 provides the binding for the free variable. The name should be
510 marked CELL in this block and removed from the free list.
511
512 Note that the current block's free variables are included in free.
513 That's safe because no name can be free and local in the same scope.
514*/
515
516static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500517analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 PyObject *name, *v, *v_cell;
520 int success = 0;
521 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 v_cell = PyLong_FromLong(CELL);
524 if (!v_cell)
525 return 0;
526 while (PyDict_Next(scopes, &pos, &name, &v)) {
527 long scope;
528 assert(PyLong_Check(v));
529 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000530 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 continue;
532 if (!PySet_Contains(free, name))
533 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 /* Replace LOCAL with CELL for this name, and remove
535 from free. It is safe to replace the value of name
536 in the dict, because it will not cause a resize.
537 */
538 if (PyDict_SetItem(scopes, name, v_cell) < 0)
539 goto error;
540 if (PySet_Discard(free, name) < 0)
541 goto error;
542 }
543 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 Py_DECREF(v_cell);
546 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547}
548
Benjamin Peterson312595c2013-05-15 15:26:42 -0500549static int
550drop_class_free(PySTEntryObject *ste, PyObject *free)
551{
552 int res;
553 if (!GET_IDENTIFIER(__class__))
554 return 0;
555 res = PySet_Discard(free, __class__);
556 if (res < 0)
557 return 0;
558 if (res)
559 ste->ste_needs_class_closure = 1;
560 return 1;
561}
562
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563/* Check for illegal statements in unoptimized namespaces */
564static int
565check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
569 || !(ste->ste_free || ste->ste_child_free))
570 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 trailer = (ste->ste_child_free ?
573 "contains a nested function with free variables" :
574 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 switch (ste->ste_unoptimized) {
577 case OPT_TOPLEVEL: /* import * at top-level is fine */
578 return 1;
579 case OPT_IMPORT_STAR:
580 PyErr_Format(PyExc_SyntaxError,
581 "import * is not allowed in function '%U' because it %s",
582 ste->ste_name, trailer);
583 break;
584 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000585
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000586 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
587 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589}
590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591/* Enter the final scope information into the ste_symbols dict.
592 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 * All arguments are dicts. Modifies symbols, others are read-only.
594*/
595static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 PyObject *name = NULL, *itr = NULL;
600 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
601 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 /* Update scope information for all symbols in this scope */
604 while (PyDict_Next(symbols, &pos, &name, &v)) {
605 long scope, flags;
606 assert(PyLong_Check(v));
607 flags = PyLong_AS_LONG(v);
608 v_scope = PyDict_GetItem(scopes, name);
609 assert(v_scope && PyLong_Check(v_scope));
610 scope = PyLong_AS_LONG(v_scope);
611 flags |= (scope << SCOPE_OFFSET);
612 v_new = PyLong_FromLong(flags);
613 if (!v_new)
614 return 0;
615 if (PyDict_SetItem(symbols, name, v_new) < 0) {
616 Py_DECREF(v_new);
617 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 Py_DECREF(v_new);
620 }
621
622 /* Record not yet resolved free variables from children (if any) */
623 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
624 if (!v_free)
625 return 0;
626
627 itr = PyObject_GetIter(free);
628 if (!itr)
629 goto error;
630
631 while ((name = PyIter_Next(itr))) {
632 v = PyDict_GetItem(symbols, name);
633
634 /* Handle symbol that already exists in this scope */
635 if (v) {
636 /* Handle a free variable in a method of
637 the class that has the same name as a local
638 or global in the class scope.
639 */
640 if (classflag &&
641 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
642 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
643 v_new = PyLong_FromLong(flags);
644 if (!v_new) {
645 goto error;
646 }
647 if (PyDict_SetItem(symbols, name, v_new) < 0) {
648 Py_DECREF(v_new);
649 goto error;
650 }
651 Py_DECREF(v_new);
652 }
653 /* It's a cell, or already free in this scope */
654 Py_DECREF(name);
655 continue;
656 }
657 /* Handle global symbol */
658 if (!PySet_Contains(bound, name)) {
659 Py_DECREF(name);
660 continue; /* it's a global */
661 }
662 /* Propagate new free symbol up the lexical stack */
663 if (PyDict_SetItem(symbols, name, v_free) < 0) {
664 goto error;
665 }
666 Py_DECREF(name);
667 }
668 Py_DECREF(itr);
669 Py_DECREF(v_free);
670 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000671error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 Py_XDECREF(v_free);
673 Py_XDECREF(itr);
674 Py_XDECREF(name);
675 return 0;
676}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677
678/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000679
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 Arguments:
681 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000682 bound -- set of variables bound in enclosing scopes (input). bound
683 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684 free -- set of free variables in enclosed scopes (output)
685 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000686
687 The implementation uses two mutually recursive functions,
688 analyze_block() and analyze_child_block(). analyze_block() is
689 responsible for analyzing the individual names defined in a block.
690 analyze_child_block() prepares temporary namespace dictionaries
691 used to evaluated nested blocks.
692
693 The two functions exist because a child block should see the name
694 bindings of its enclosing blocks, but those bindings should not
695 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696*/
697
698static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
700 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000701
702static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
704 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
707 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
708 PyObject *temp;
709 int i, success = 0;
710 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 local = PySet_New(NULL); /* collect new names bound in block */
713 if (!local)
714 goto error;
715 scopes = PyDict_New(); /* collect scopes defined for each name */
716 if (!scopes)
717 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 /* Allocate new global and bound variable dictionaries. These
720 dictionaries hold the names visible in nested blocks. For
721 ClassBlocks, the bound and global names are initialized
722 before analyzing names, because class bindings aren't
723 visible in methods. For other blocks, they are initialized
724 after names are analyzed.
725 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 /* TODO(jhylton): Package these dicts in a struct so that we
728 can write reasonable helper functions?
729 */
730 newglobal = PySet_New(NULL);
731 if (!newglobal)
732 goto error;
733 newfree = PySet_New(NULL);
734 if (!newfree)
735 goto error;
736 newbound = PySet_New(NULL);
737 if (!newbound)
738 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 /* Class namespace has no effect on names visible in
741 nested functions, so populate the global and bound
742 sets to be passed to child blocks before analyzing
743 this one.
744 */
745 if (ste->ste_type == ClassBlock) {
746 /* Pass down known globals */
747 temp = PyNumber_InPlaceOr(newglobal, global);
748 if (!temp)
749 goto error;
750 Py_DECREF(temp);
751 /* Pass down previously bound symbols */
752 if (bound) {
753 temp = PyNumber_InPlaceOr(newbound, bound);
754 if (!temp)
755 goto error;
756 Py_DECREF(temp);
757 }
758 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
761 long flags = PyLong_AS_LONG(v);
762 if (!analyze_name(ste, scopes, name, flags,
763 bound, local, free, global))
764 goto error;
765 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 /* Populate global and bound sets to be passed to children. */
768 if (ste->ste_type != ClassBlock) {
769 /* Add function locals to bound set */
770 if (ste->ste_type == FunctionBlock) {
771 temp = PyNumber_InPlaceOr(newbound, local);
772 if (!temp)
773 goto error;
774 Py_DECREF(temp);
775 }
776 /* Pass down previously bound symbols */
777 if (bound) {
778 temp = PyNumber_InPlaceOr(newbound, bound);
779 if (!temp)
780 goto error;
781 Py_DECREF(temp);
782 }
783 /* Pass down known globals */
784 temp = PyNumber_InPlaceOr(newglobal, global);
785 if (!temp)
786 goto error;
787 Py_DECREF(temp);
788 }
789 else {
790 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000791 if (!GET_IDENTIFIER(__class__))
792 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 if (PySet_Add(newbound, __class__) < 0)
794 goto error;
795 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300797 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 newbound, newglobal now contain the names visible in
800 nested blocks. The free variables in the children will
801 be collected in allfree.
802 */
803 allfree = PySet_New(NULL);
804 if (!allfree)
805 goto error;
806 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
807 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
808 PySTEntryObject* entry;
809 assert(c && PySTEntry_Check(c));
810 entry = (PySTEntryObject*)c;
811 if (!analyze_child_block(entry, newbound, newfree, newglobal,
812 allfree))
813 goto error;
814 /* Check if any children have free variables */
815 if (entry->ste_free || entry->ste_child_free)
816 ste->ste_child_free = 1;
817 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 temp = PyNumber_InPlaceOr(newfree, allfree);
820 if (!temp)
821 goto error;
822 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500825 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500827 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 goto error;
829 /* Records the results of the analysis in the symbol table entry */
830 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
831 ste->ste_type == ClassBlock))
832 goto error;
833 if (!check_unoptimized(ste))
834 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 temp = PyNumber_InPlaceOr(free, newfree);
837 if (!temp)
838 goto error;
839 Py_DECREF(temp);
840 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 Py_XDECREF(scopes);
843 Py_XDECREF(local);
844 Py_XDECREF(newbound);
845 Py_XDECREF(newglobal);
846 Py_XDECREF(newfree);
847 Py_XDECREF(allfree);
848 if (!success)
849 assert(PyErr_Occurred());
850 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851}
852
853static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
855 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
858 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 These dictionary are used by all blocks enclosed by the
863 current block. The analyze_block() call modifies these
864 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 */
867 temp_bound = PySet_New(bound);
868 if (!temp_bound)
869 goto error;
870 temp_free = PySet_New(free);
871 if (!temp_free)
872 goto error;
873 temp_global = PySet_New(global);
874 if (!temp_global)
875 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
878 goto error;
879 temp = PyNumber_InPlaceOr(child_free, temp_free);
880 if (!temp)
881 goto error;
882 Py_DECREF(temp);
883 Py_DECREF(temp_bound);
884 Py_DECREF(temp_free);
885 Py_DECREF(temp_global);
886 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000887 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 Py_XDECREF(temp_bound);
889 Py_XDECREF(temp_free);
890 Py_XDECREF(temp_global);
891 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000892}
893
894static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895symtable_analyze(struct symtable *st)
896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 PyObject *free, *global;
898 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 free = PySet_New(NULL);
901 if (!free)
902 return 0;
903 global = PySet_New(NULL);
904 if (!global) {
905 Py_DECREF(free);
906 return 0;
907 }
908 r = analyze_block(st->st_top, NULL, free, global);
909 Py_DECREF(free);
910 Py_DECREF(global);
911 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912}
913
914
915static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000916symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
919 lineno, NULL, NULL) < 0) {
920 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
921 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000922 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
923 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 }
925 return 0;
926 }
927 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928}
929
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000930/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931 This reference is released when the block is exited, via the DECREF
932 in symtable_exit_block().
933*/
934
935static int
936symtable_exit_block(struct symtable *st, void *ast)
937{
Benjamin Peterson609da582011-06-29 22:52:39 -0500938 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939
Benjamin Peterson609da582011-06-29 22:52:39 -0500940 st->st_cur = NULL;
941 size = PyList_GET_SIZE(st->st_stack);
942 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500943 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500945 if (--size)
946 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 }
948 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949}
950
951static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000953 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954{
Benjamin Peterson609da582011-06-29 22:52:39 -0500955 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956
Benjamin Peterson609da582011-06-29 22:52:39 -0500957 ste = ste_new(st, name, block, ast, lineno, col_offset);
958 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500960 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
961 Py_DECREF(ste);
962 return 0;
963 }
964 prev = st->st_cur;
965 /* The entry is owned by the stack. Borrow it for st_cur. */
966 Py_DECREF(ste);
967 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000968 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 st->st_global = st->st_cur->ste_symbols;
970 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500971 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 return 0;
973 }
974 }
975 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976}
977
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000978static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979symtable_lookup(struct symtable *st, PyObject *name)
980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 PyObject *o;
982 PyObject *mangled = _Py_Mangle(st->st_private, name);
983 if (!mangled)
984 return 0;
985 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
986 Py_DECREF(mangled);
987 if (!o)
988 return 0;
989 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990}
991
992static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 PyObject *o;
996 PyObject *dict;
997 long val;
998 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999
Jeremy Hylton81e95022007-02-27 06:50:52 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 if (!mangled)
1002 return 0;
1003 dict = st->st_cur->ste_symbols;
1004 if ((o = PyDict_GetItem(dict, mangled))) {
1005 val = PyLong_AS_LONG(o);
1006 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1007 /* Is it better to use 'mangled' or 'name' here? */
1008 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001009 PyErr_SyntaxLocationEx(st->st_filename,
1010 st->st_cur->ste_lineno,
1011 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 goto error;
1013 }
1014 val |= flag;
1015 } else
1016 val = flag;
1017 o = PyLong_FromLong(val);
1018 if (o == NULL)
1019 goto error;
1020 if (PyDict_SetItem(dict, mangled, o) < 0) {
1021 Py_DECREF(o);
1022 goto error;
1023 }
1024 Py_DECREF(o);
1025
1026 if (flag & DEF_PARAM) {
1027 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1028 goto error;
1029 } else if (flag & DEF_GLOBAL) {
1030 /* XXX need to update DEF_GLOBAL for other flags too;
1031 perhaps only DEF_FREE_GLOBAL */
1032 val = flag;
1033 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1034 val |= PyLong_AS_LONG(o);
1035 }
1036 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 goto error;
1039 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1040 Py_DECREF(o);
1041 goto error;
1042 }
1043 Py_DECREF(o);
1044 }
1045 Py_DECREF(mangled);
1046 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001047
1048error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 Py_DECREF(mangled);
1050 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051}
1052
1053/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1054 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 function.
1056
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1058 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001059
1060 VISIT_QUIT macro returns the specified value exiting from the function but
1061 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062*/
1063
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001064#define VISIT_QUIT(ST, X) \
1065 return --(ST)->recursion_depth,(X)
1066
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001069 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 int i; \
1073 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1074 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1075 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1076 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001077 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 int i; \
1083 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1084 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1085 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1086 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001087 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001090
Guido van Rossum4f72a782006-10-27 23:31:49 +00001091#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 int i = 0; \
1093 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1094 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1095 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1096 if (!elt) continue; /* can be NULL */ \
1097 if (!symtable_visit_expr((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001098 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001100}
1101
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001103symtable_new_tmpname(struct symtable *st)
1104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 char tmpname[256];
1106 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1109 ++st->st_cur->ste_tmpname);
1110 tmp = PyUnicode_InternFromString(tmpname);
1111 if (!tmp)
1112 return 0;
1113 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1114 return 0;
1115 Py_DECREF(tmp);
1116 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001117}
1118
Guido van Rossum4f72a782006-10-27 23:31:49 +00001119
Guido van Rossumc2e20742006-02-27 22:32:47 +00001120static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001121symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1122{
1123 PyObject *data;
1124 int res;
1125 if (!st->st_cur->ste_directives) {
1126 st->st_cur->ste_directives = PyList_New(0);
1127 if (!st->st_cur->ste_directives)
1128 return 0;
1129 }
1130 data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset);
1131 if (!data)
1132 return 0;
1133 res = PyList_Append(st->st_cur->ste_directives, data);
1134 Py_DECREF(data);
1135 return res == 0;
1136}
1137
1138
1139static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140symtable_visit_stmt(struct symtable *st, stmt_ty s)
1141{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001142 if (++st->recursion_depth > st->recursion_limit) {
1143 PyErr_SetString(PyExc_RuntimeError,
1144 "maximum recursion depth exceeded during compilation");
1145 VISIT_QUIT(st, 0);
1146 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 switch (s->kind) {
1148 case FunctionDef_kind:
1149 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001150 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 if (s->v.FunctionDef.args->defaults)
1152 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1153 if (s->v.FunctionDef.args->kw_defaults)
1154 VISIT_KWONLYDEFAULTS(st,
1155 s->v.FunctionDef.args->kw_defaults);
1156 if (!symtable_visit_annotations(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001157 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (s->v.FunctionDef.decorator_list)
1159 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1160 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001161 FunctionBlock, (void *)s, s->lineno,
1162 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001163 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001164 VISIT(st, arguments, s->v.FunctionDef.args);
1165 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001167 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 break;
1169 case ClassDef_kind: {
1170 PyObject *tmp;
1171 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001172 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1174 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1175 if (s->v.ClassDef.starargs)
1176 VISIT(st, expr, s->v.ClassDef.starargs);
1177 if (s->v.ClassDef.kwargs)
1178 VISIT(st, expr, s->v.ClassDef.kwargs);
1179 if (s->v.ClassDef.decorator_list)
1180 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1181 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001182 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001183 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 tmp = st->st_private;
1185 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001186 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 st->st_private = tmp;
1188 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001189 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 break;
1191 }
1192 case Return_kind:
1193 if (s->v.Return.value) {
1194 VISIT(st, expr, s->v.Return.value);
1195 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 }
1197 break;
1198 case Delete_kind:
1199 VISIT_SEQ(st, expr, s->v.Delete.targets);
1200 break;
1201 case Assign_kind:
1202 VISIT_SEQ(st, expr, s->v.Assign.targets);
1203 VISIT(st, expr, s->v.Assign.value);
1204 break;
1205 case AugAssign_kind:
1206 VISIT(st, expr, s->v.AugAssign.target);
1207 VISIT(st, expr, s->v.AugAssign.value);
1208 break;
1209 case For_kind:
1210 VISIT(st, expr, s->v.For.target);
1211 VISIT(st, expr, s->v.For.iter);
1212 VISIT_SEQ(st, stmt, s->v.For.body);
1213 if (s->v.For.orelse)
1214 VISIT_SEQ(st, stmt, s->v.For.orelse);
1215 break;
1216 case While_kind:
1217 VISIT(st, expr, s->v.While.test);
1218 VISIT_SEQ(st, stmt, s->v.While.body);
1219 if (s->v.While.orelse)
1220 VISIT_SEQ(st, stmt, s->v.While.orelse);
1221 break;
1222 case If_kind:
1223 /* XXX if 0: and lookup_yield() hacks */
1224 VISIT(st, expr, s->v.If.test);
1225 VISIT_SEQ(st, stmt, s->v.If.body);
1226 if (s->v.If.orelse)
1227 VISIT_SEQ(st, stmt, s->v.If.orelse);
1228 break;
1229 case Raise_kind:
1230 if (s->v.Raise.exc) {
1231 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001232 if (s->v.Raise.cause) {
1233 VISIT(st, expr, s->v.Raise.cause);
1234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
1236 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001237 case Try_kind:
1238 VISIT_SEQ(st, stmt, s->v.Try.body);
1239 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1240 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1241 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 break;
1243 case Assert_kind:
1244 VISIT(st, expr, s->v.Assert.test);
1245 if (s->v.Assert.msg)
1246 VISIT(st, expr, s->v.Assert.msg);
1247 break;
1248 case Import_kind:
1249 VISIT_SEQ(st, alias, s->v.Import.names);
1250 /* XXX Don't have the lineno available inside
1251 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001252 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001254 st->st_cur->ste_opt_col_offset = s->col_offset;
1255 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 break;
1257 case ImportFrom_kind:
1258 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1259 /* XXX Don't have the lineno available inside
1260 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001261 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001263 st->st_cur->ste_opt_col_offset = s->col_offset;
1264 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 break;
1266 case Global_kind: {
1267 int i;
1268 asdl_seq *seq = s->v.Global.names;
1269 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1270 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 long cur = symtable_lookup(st, name);
1272 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001273 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (cur & (DEF_LOCAL | USE)) {
1275 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001276 char *c_name = _PyUnicode_AsString(name);
1277 if (!c_name)
1278 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (cur & DEF_LOCAL)
1280 PyOS_snprintf(buf, sizeof(buf),
1281 GLOBAL_AFTER_ASSIGN,
1282 c_name);
1283 else
1284 PyOS_snprintf(buf, sizeof(buf),
1285 GLOBAL_AFTER_USE,
1286 c_name);
1287 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001288 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 }
1290 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001291 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001292 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001293 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 }
1295 break;
1296 }
1297 case Nonlocal_kind: {
1298 int i;
1299 asdl_seq *seq = s->v.Nonlocal.names;
1300 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1301 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 long cur = symtable_lookup(st, name);
1303 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001304 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 if (cur & (DEF_LOCAL | USE)) {
1306 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001307 char *c_name = _PyUnicode_AsString(name);
1308 if (!c_name)
1309 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (cur & DEF_LOCAL)
1311 PyOS_snprintf(buf, sizeof(buf),
1312 NONLOCAL_AFTER_ASSIGN,
1313 c_name);
1314 else
1315 PyOS_snprintf(buf, sizeof(buf),
1316 NONLOCAL_AFTER_USE,
1317 c_name);
1318 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001319 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 }
1321 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001322 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001323 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001324 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 }
1326 break;
1327 }
1328 case Expr_kind:
1329 VISIT(st, expr, s->v.Expr.value);
1330 break;
1331 case Pass_kind:
1332 case Break_kind:
1333 case Continue_kind:
1334 /* nothing to do here */
1335 break;
1336 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001337 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 VISIT_SEQ(st, stmt, s->v.With.body);
1339 break;
1340 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001341 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342}
1343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345symtable_visit_expr(struct symtable *st, expr_ty e)
1346{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001347 if (++st->recursion_depth > st->recursion_limit) {
1348 PyErr_SetString(PyExc_RuntimeError,
1349 "maximum recursion depth exceeded during compilation");
1350 VISIT_QUIT(st, 0);
1351 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 switch (e->kind) {
1353 case BoolOp_kind:
1354 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1355 break;
1356 case BinOp_kind:
1357 VISIT(st, expr, e->v.BinOp.left);
1358 VISIT(st, expr, e->v.BinOp.right);
1359 break;
1360 case UnaryOp_kind:
1361 VISIT(st, expr, e->v.UnaryOp.operand);
1362 break;
1363 case Lambda_kind: {
1364 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001365 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 if (e->v.Lambda.args->defaults)
1367 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001368 if (e->v.Lambda.args->kw_defaults)
1369 VISIT_KWONLYDEFAULTS(st,
1370 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001372 FunctionBlock, (void *)e, e->lineno,
1373 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001374 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001375 VISIT(st, arguments, e->v.Lambda.args);
1376 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001378 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 break;
1380 }
1381 case IfExp_kind:
1382 VISIT(st, expr, e->v.IfExp.test);
1383 VISIT(st, expr, e->v.IfExp.body);
1384 VISIT(st, expr, e->v.IfExp.orelse);
1385 break;
1386 case Dict_kind:
1387 VISIT_SEQ(st, expr, e->v.Dict.keys);
1388 VISIT_SEQ(st, expr, e->v.Dict.values);
1389 break;
1390 case Set_kind:
1391 VISIT_SEQ(st, expr, e->v.Set.elts);
1392 break;
1393 case GeneratorExp_kind:
1394 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001395 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 break;
1397 case ListComp_kind:
1398 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001399 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 break;
1401 case SetComp_kind:
1402 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001403 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 break;
1405 case DictComp_kind:
1406 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001407 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 break;
1409 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001410 if (e->v.Yield.value)
1411 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001414 case YieldFrom_kind:
1415 VISIT(st, expr, e->v.YieldFrom.value);
1416 st->st_cur->ste_generator = 1;
1417 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 case Compare_kind:
1419 VISIT(st, expr, e->v.Compare.left);
1420 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1421 break;
1422 case Call_kind:
1423 VISIT(st, expr, e->v.Call.func);
1424 VISIT_SEQ(st, expr, e->v.Call.args);
1425 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1426 if (e->v.Call.starargs)
1427 VISIT(st, expr, e->v.Call.starargs);
1428 if (e->v.Call.kwargs)
1429 VISIT(st, expr, e->v.Call.kwargs);
1430 break;
1431 case Num_kind:
1432 case Str_kind:
1433 case Bytes_kind:
1434 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001435 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 /* Nothing to do here. */
1437 break;
1438 /* The following exprs can be assignment targets. */
1439 case Attribute_kind:
1440 VISIT(st, expr, e->v.Attribute.value);
1441 break;
1442 case Subscript_kind:
1443 VISIT(st, expr, e->v.Subscript.value);
1444 VISIT(st, slice, e->v.Subscript.slice);
1445 break;
1446 case Starred_kind:
1447 VISIT(st, expr, e->v.Starred.value);
1448 break;
1449 case Name_kind:
1450 if (!symtable_add_def(st, e->v.Name.id,
1451 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001452 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 /* Special-case super: it counts as a use of __class__ */
1454 if (e->v.Name.ctx == Load &&
1455 st->st_cur->ste_type == FunctionBlock &&
1456 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001457 if (!GET_IDENTIFIER(__class__) ||
1458 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001459 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 }
1461 break;
1462 /* child nodes of List and Tuple will have expr_context set */
1463 case List_kind:
1464 VISIT_SEQ(st, expr, e->v.List.elts);
1465 break;
1466 case Tuple_kind:
1467 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1468 break;
1469 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001470 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471}
1472
1473static int
1474symtable_implicit_arg(struct symtable *st, int pos)
1475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1477 if (id == NULL)
1478 return 0;
1479 if (!symtable_add_def(st, id, DEF_PARAM)) {
1480 Py_DECREF(id);
1481 return 0;
1482 }
1483 Py_DECREF(id);
1484 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485}
1486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001488symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 if (!args)
1493 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 for (i = 0; i < asdl_seq_LEN(args); i++) {
1496 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1497 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1498 return 0;
1499 }
1500
1501 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502}
1503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001505symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 if (!args)
1510 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 for (i = 0; i < asdl_seq_LEN(args); i++) {
1513 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1514 if (arg->annotation)
1515 VISIT(st, expr, arg->annotation);
1516 }
1517
1518 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001519}
1520
Neal Norwitzc1505362006-12-28 06:47:50 +00001521static int
1522symtable_visit_annotations(struct symtable *st, stmt_ty s)
1523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 arguments_ty a = s->v.FunctionDef.args;
1525
1526 if (a->args && !symtable_visit_argannotations(st, a->args))
1527 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001528 if (a->vararg && a->vararg->annotation)
1529 VISIT(st, expr, a->vararg->annotation);
1530 if (a->kwarg && a->kwarg->annotation)
1531 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1533 return 0;
1534 if (s->v.FunctionDef.returns)
1535 VISIT(st, expr, s->v.FunctionDef.returns);
1536 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537}
1538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540symtable_visit_arguments(struct symtable *st, arguments_ty a)
1541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 /* skip default arguments inside function block
1543 XXX should ast be different?
1544 */
1545 if (a->args && !symtable_visit_params(st, a->args))
1546 return 0;
1547 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1548 return 0;
1549 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001550 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 return 0;
1552 st->st_cur->ste_varargs = 1;
1553 }
1554 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001555 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 return 0;
1557 st->st_cur->ste_varkeywords = 1;
1558 }
1559 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560}
1561
1562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 if (eh->v.ExceptHandler.type)
1567 VISIT(st, expr, eh->v.ExceptHandler.type);
1568 if (eh->v.ExceptHandler.name)
1569 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1570 return 0;
1571 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1572 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573}
1574
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001575static int
1576symtable_visit_withitem(struct symtable *st, withitem_ty item)
1577{
1578 VISIT(st, expr, item->context_expr);
1579 if (item->optional_vars) {
1580 VISIT(st, expr, item->optional_vars);
1581 }
1582 return 1;
1583}
1584
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587symtable_visit_alias(struct symtable *st, alias_ty a)
1588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001590 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 dotted package name (e.g. spam.eggs)
1592 */
1593 PyObject *store_name;
1594 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001595 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1596 PyUnicode_GET_LENGTH(name), 1);
1597 if (dot != -1) {
1598 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 if (!store_name)
1600 return 0;
1601 }
1602 else {
1603 store_name = name;
1604 Py_INCREF(store_name);
1605 }
1606 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1607 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1608 Py_DECREF(store_name);
1609 return r;
1610 }
1611 else {
1612 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001613 int lineno = st->st_cur->ste_lineno;
1614 int col_offset = st->st_cur->ste_col_offset;
1615 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1616 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1617 Py_DECREF(store_name);
1618 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 }
1620 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1621 Py_DECREF(store_name);
1622 return 1;
1623 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624}
1625
1626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 VISIT(st, expr, lc->target);
1631 VISIT(st, expr, lc->iter);
1632 VISIT_SEQ(st, expr, lc->ifs);
1633 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634}
1635
1636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638symtable_visit_keyword(struct symtable *st, keyword_ty k)
1639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 VISIT(st, expr, k->value);
1641 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642}
1643
1644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646symtable_visit_slice(struct symtable *st, slice_ty s)
1647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 switch (s->kind) {
1649 case Slice_kind:
1650 if (s->v.Slice.lower)
1651 VISIT(st, expr, s->v.Slice.lower)
1652 if (s->v.Slice.upper)
1653 VISIT(st, expr, s->v.Slice.upper)
1654 if (s->v.Slice.step)
1655 VISIT(st, expr, s->v.Slice.step)
1656 break;
1657 case ExtSlice_kind:
1658 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1659 break;
1660 case Index_kind:
1661 VISIT(st, expr, s->v.Index.value)
1662 break;
1663 }
1664 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665}
1666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001668symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001669 identifier scope_name, asdl_seq *generators,
1670 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 int is_generator = (e->kind == GeneratorExp_kind);
1673 int needs_tmp = !is_generator;
1674 comprehension_ty outermost = ((comprehension_ty)
1675 asdl_seq_GET(generators, 0));
1676 /* Outermost iterator is evaluated in current scope */
1677 VISIT(st, expr, outermost->iter);
1678 /* Create comprehension scope for the rest */
1679 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001680 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1681 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 return 0;
1683 }
1684 st->st_cur->ste_generator = is_generator;
1685 /* Outermost iter is received as an argument */
1686 if (!symtable_implicit_arg(st, 0)) {
1687 symtable_exit_block(st, (void *)e);
1688 return 0;
1689 }
1690 /* Allocate temporary name if needed */
1691 if (needs_tmp && !symtable_new_tmpname(st)) {
1692 symtable_exit_block(st, (void *)e);
1693 return 0;
1694 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001695 VISIT(st, expr, outermost->target);
1696 VISIT_SEQ(st, expr, outermost->ifs);
1697 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001699 VISIT(st, expr, value);
1700 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001705symtable_visit_genexp(struct symtable *st, expr_ty e)
1706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1708 e->v.GeneratorExp.generators,
1709 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001710}
1711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001713symtable_visit_listcomp(struct symtable *st, expr_ty e)
1714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1716 e->v.ListComp.generators,
1717 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001718}
1719
1720static int
1721symtable_visit_setcomp(struct symtable *st, expr_ty e)
1722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1724 e->v.SetComp.generators,
1725 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001726}
1727
1728static int
1729symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1732 e->v.DictComp.generators,
1733 e->v.DictComp.key,
1734 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001735}