blob: 55898f99bf14adadba3aa73ee78483ec2ca0f4bb [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 ste->ste_name = name;
41 Py_INCREF(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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 ste->ste_symbols = PyDict_New();
48 if (ste->ste_symbols == NULL)
49 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 ste->ste_varnames = PyList_New(0);
52 if (ste->ste_varnames == NULL)
53 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 ste->ste_children = PyList_New(0);
56 if (ste->ste_children == NULL)
57 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 ste->ste_type = block;
60 ste->ste_unoptimized = 0;
61 ste->ste_nested = 0;
62 ste->ste_free = 0;
63 ste->ste_varargs = 0;
64 ste->ste_varkeywords = 0;
65 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000066 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000067 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000069 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (st->st_cur != NULL &&
72 (st->st_cur->ste_nested ||
73 st->st_cur->ste_type == FunctionBlock))
74 ste->ste_nested = 1;
75 ste->ste_child_free = 0;
76 ste->ste_generator = 0;
77 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
80 goto fail;
81
82 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 Py_XDECREF(ste);
85 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000086}
87
88static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
92 ste->ste_name,
93 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000094}
95
96static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 ste->ste_table = NULL;
100 Py_XDECREF(ste->ste_id);
101 Py_XDECREF(ste->ste_name);
102 Py_XDECREF(ste->ste_symbols);
103 Py_XDECREF(ste->ste_varnames);
104 Py_XDECREF(ste->ste_children);
105 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,
191 __class__ = NULL, __locals__ = 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
345
346/* Analyze raw symbol information to determine scope of each name.
347
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000348 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354 explicit global is declared with the global statement. An implicit
355 global is a free variable for which the compiler has found no binding
356 in an enclosing function scope. The implicit global is either a global
357 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
358 to handle these names to implement slightly odd semantics. In such a
359 block, the name is treated as global until it is assigned to; then it
360 is treated as a local.
361
362 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000363 The first pass collects raw facts from the AST via the symtable_visit_*
364 functions: the name is a parameter here, the name is used but not defined
365 here, etc. The second pass analyzes these facts during a pass over the
366 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367
368 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000370 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000371 Names which are explicitly declared nonlocal must exist in this set of
372 visible names - if they do not, a syntax error is raised. After doing
373 the local analysis, it analyzes each of its child blocks using an
374 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375
Nick Coghlan650f0d02007-04-15 12:05:43 +0000376 The children update the free variable set. If a local variable is added to
377 the free variable set by the child, the variable is marked as a cell. The
378 function object being defined must provide runtime storage for the variable
379 that may outlive the function's frame. Cell variables are removed from the
380 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000381
Nick Coghlan650f0d02007-04-15 12:05:43 +0000382 During analysis, the names are:
383 symbols: dict mapping from symbol names to flag values (including offset scope values)
384 scopes: dict mapping from symbol names to scope values (no offset)
385 local: set of all symbol names local to the current scope
386 bound: set of all symbol names local to a containing function scope
387 free: set of all symbol names referenced but not bound in child scopes
388 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389*/
390
391#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 PyObject *o = PyLong_FromLong(I); \
393 if (!o) \
394 return 0; \
395 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
396 Py_DECREF(o); \
397 return 0; \
398 } \
399 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400}
401
402/* Decide on scope of name, given flags.
403
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000404 The namespace dictionaries may be modified to record information
405 about the new name. For example, a new global will add an entry to
406 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407*/
408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000410analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 PyObject *bound, PyObject *local, PyObject *free,
412 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (flags & DEF_GLOBAL) {
415 if (flags & DEF_PARAM) {
416 PyErr_Format(PyExc_SyntaxError,
417 "name '%U' is parameter and global",
418 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000419 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
420 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421
422 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000423 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 if (flags & DEF_NONLOCAL) {
425 PyErr_Format(PyExc_SyntaxError,
426 "name '%U' is nonlocal and global",
427 name);
428 return 0;
429 }
430 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
431 if (PySet_Add(global, name) < 0)
432 return 0;
433 if (bound && (PySet_Discard(bound, name) < 0))
434 return 0;
435 return 1;
436 }
437 if (flags & DEF_NONLOCAL) {
438 if (flags & DEF_PARAM) {
439 PyErr_Format(PyExc_SyntaxError,
440 "name '%U' is parameter and nonlocal",
441 name);
442 return 0;
443 }
444 if (!bound) {
445 PyErr_Format(PyExc_SyntaxError,
446 "nonlocal declaration not allowed at module level");
447 return 0;
448 }
449 if (!PySet_Contains(bound, name)) {
450 PyErr_Format(PyExc_SyntaxError,
451 "no binding for nonlocal '%U' found",
452 name);
453
454 return 0;
455 }
456 SET_SCOPE(scopes, name, FREE);
457 ste->ste_free = 1;
458 return PySet_Add(free, name) >= 0;
459 }
460 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000461 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 if (PySet_Add(local, name) < 0)
463 return 0;
464 if (PySet_Discard(global, name) < 0)
465 return 0;
466 return 1;
467 }
468 /* If an enclosing block has a binding for this name, it
469 is a free variable rather than a global variable.
470 Note that having a non-NULL bound implies that the block
471 is nested.
472 */
473 if (bound && PySet_Contains(bound, name)) {
474 SET_SCOPE(scopes, name, FREE);
475 ste->ste_free = 1;
476 return PySet_Add(free, name) >= 0;
477 }
478 /* If a parent has a global statement, then call it global
479 explicit? It could also be global implicit.
480 */
481 if (global && PySet_Contains(global, name)) {
482 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
483 return 1;
484 }
485 if (ste->ste_nested)
486 ste->ste_free = 1;
487 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
488 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489}
490
491#undef SET_SCOPE
492
493/* If a name is defined in free and also in locals, then this block
494 provides the binding for the free variable. The name should be
495 marked CELL in this block and removed from the free list.
496
497 Note that the current block's free variables are included in free.
498 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000499
Martin v. Löwis2673a572007-10-29 19:54:24 +0000500 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000501 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502*/
503
504static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000505analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 PyObject *name, *v, *v_cell;
508 int success = 0;
509 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 v_cell = PyLong_FromLong(CELL);
512 if (!v_cell)
513 return 0;
514 while (PyDict_Next(scopes, &pos, &name, &v)) {
515 long scope;
516 assert(PyLong_Check(v));
517 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000518 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 continue;
520 if (!PySet_Contains(free, name))
521 continue;
522 if (restricted != NULL &&
523 PyUnicode_CompareWithASCIIString(name, restricted))
524 continue;
525 /* Replace LOCAL with CELL for this name, and remove
526 from free. It is safe to replace the value of name
527 in the dict, because it will not cause a resize.
528 */
529 if (PyDict_SetItem(scopes, name, v_cell) < 0)
530 goto error;
531 if (PySet_Discard(free, name) < 0)
532 goto error;
533 }
534 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 Py_DECREF(v_cell);
537 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538}
539
540/* Check for illegal statements in unoptimized namespaces */
541static int
542check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
546 || !(ste->ste_free || ste->ste_child_free))
547 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 trailer = (ste->ste_child_free ?
550 "contains a nested function with free variables" :
551 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 switch (ste->ste_unoptimized) {
554 case OPT_TOPLEVEL: /* import * at top-level is fine */
555 return 1;
556 case OPT_IMPORT_STAR:
557 PyErr_Format(PyExc_SyntaxError,
558 "import * is not allowed in function '%U' because it %s",
559 ste->ste_name, trailer);
560 break;
561 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000563 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
564 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566}
567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568/* Enter the final scope information into the ste_symbols dict.
569 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 * All arguments are dicts. Modifies symbols, others are read-only.
571*/
572static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000574 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 PyObject *name = NULL, *itr = NULL;
577 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
578 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 /* Update scope information for all symbols in this scope */
581 while (PyDict_Next(symbols, &pos, &name, &v)) {
582 long scope, flags;
583 assert(PyLong_Check(v));
584 flags = PyLong_AS_LONG(v);
585 v_scope = PyDict_GetItem(scopes, name);
586 assert(v_scope && PyLong_Check(v_scope));
587 scope = PyLong_AS_LONG(v_scope);
588 flags |= (scope << SCOPE_OFFSET);
589 v_new = PyLong_FromLong(flags);
590 if (!v_new)
591 return 0;
592 if (PyDict_SetItem(symbols, name, v_new) < 0) {
593 Py_DECREF(v_new);
594 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 Py_DECREF(v_new);
597 }
598
599 /* Record not yet resolved free variables from children (if any) */
600 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
601 if (!v_free)
602 return 0;
603
604 itr = PyObject_GetIter(free);
605 if (!itr)
606 goto error;
607
608 while ((name = PyIter_Next(itr))) {
609 v = PyDict_GetItem(symbols, name);
610
611 /* Handle symbol that already exists in this scope */
612 if (v) {
613 /* Handle a free variable in a method of
614 the class that has the same name as a local
615 or global in the class scope.
616 */
617 if (classflag &&
618 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
619 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
620 v_new = PyLong_FromLong(flags);
621 if (!v_new) {
622 goto error;
623 }
624 if (PyDict_SetItem(symbols, name, v_new) < 0) {
625 Py_DECREF(v_new);
626 goto error;
627 }
628 Py_DECREF(v_new);
629 }
630 /* It's a cell, or already free in this scope */
631 Py_DECREF(name);
632 continue;
633 }
634 /* Handle global symbol */
635 if (!PySet_Contains(bound, name)) {
636 Py_DECREF(name);
637 continue; /* it's a global */
638 }
639 /* Propagate new free symbol up the lexical stack */
640 if (PyDict_SetItem(symbols, name, v_free) < 0) {
641 goto error;
642 }
643 Py_DECREF(name);
644 }
645 Py_DECREF(itr);
646 Py_DECREF(v_free);
647 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000648error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 Py_XDECREF(v_free);
650 Py_XDECREF(itr);
651 Py_XDECREF(name);
652 return 0;
653}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654
655/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000656
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657 Arguments:
658 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000659 bound -- set of variables bound in enclosing scopes (input). bound
660 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661 free -- set of free variables in enclosed scopes (output)
662 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000663
664 The implementation uses two mutually recursive functions,
665 analyze_block() and analyze_child_block(). analyze_block() is
666 responsible for analyzing the individual names defined in a block.
667 analyze_child_block() prepares temporary namespace dictionaries
668 used to evaluated nested blocks.
669
670 The two functions exist because a child block should see the name
671 bindings of its enclosing blocks, but those bindings should not
672 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673*/
674
675static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
677 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000678
679static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
681 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
684 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
685 PyObject *temp;
686 int i, success = 0;
687 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 local = PySet_New(NULL); /* collect new names bound in block */
690 if (!local)
691 goto error;
692 scopes = PyDict_New(); /* collect scopes defined for each name */
693 if (!scopes)
694 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 /* Allocate new global and bound variable dictionaries. These
697 dictionaries hold the names visible in nested blocks. For
698 ClassBlocks, the bound and global names are initialized
699 before analyzing names, because class bindings aren't
700 visible in methods. For other blocks, they are initialized
701 after names are analyzed.
702 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 /* TODO(jhylton): Package these dicts in a struct so that we
705 can write reasonable helper functions?
706 */
707 newglobal = PySet_New(NULL);
708 if (!newglobal)
709 goto error;
710 newfree = PySet_New(NULL);
711 if (!newfree)
712 goto error;
713 newbound = PySet_New(NULL);
714 if (!newbound)
715 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 /* Class namespace has no effect on names visible in
718 nested functions, so populate the global and bound
719 sets to be passed to child blocks before analyzing
720 this one.
721 */
722 if (ste->ste_type == ClassBlock) {
723 /* Pass down known globals */
724 temp = PyNumber_InPlaceOr(newglobal, global);
725 if (!temp)
726 goto error;
727 Py_DECREF(temp);
728 /* Pass down previously bound symbols */
729 if (bound) {
730 temp = PyNumber_InPlaceOr(newbound, bound);
731 if (!temp)
732 goto error;
733 Py_DECREF(temp);
734 }
735 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
738 long flags = PyLong_AS_LONG(v);
739 if (!analyze_name(ste, scopes, name, flags,
740 bound, local, free, global))
741 goto error;
742 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 /* Populate global and bound sets to be passed to children. */
745 if (ste->ste_type != ClassBlock) {
746 /* Add function locals to bound set */
747 if (ste->ste_type == FunctionBlock) {
748 temp = PyNumber_InPlaceOr(newbound, local);
749 if (!temp)
750 goto error;
751 Py_DECREF(temp);
752 }
753 /* Pass down previously bound symbols */
754 if (bound) {
755 temp = PyNumber_InPlaceOr(newbound, bound);
756 if (!temp)
757 goto error;
758 Py_DECREF(temp);
759 }
760 /* Pass down known globals */
761 temp = PyNumber_InPlaceOr(newglobal, global);
762 if (!temp)
763 goto error;
764 Py_DECREF(temp);
765 }
766 else {
767 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000768 if (!GET_IDENTIFIER(__class__))
769 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 assert(PySet_Contains(local, __class__) == 1);
771 if (PySet_Add(newbound, __class__) < 0)
772 goto error;
773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300775 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 newbound, newglobal now contain the names visible in
778 nested blocks. The free variables in the children will
779 be collected in allfree.
780 */
781 allfree = PySet_New(NULL);
782 if (!allfree)
783 goto error;
784 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
785 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
786 PySTEntryObject* entry;
787 assert(c && PySTEntry_Check(c));
788 entry = (PySTEntryObject*)c;
789 if (!analyze_child_block(entry, newbound, newfree, newglobal,
790 allfree))
791 goto error;
792 /* Check if any children have free variables */
793 if (entry->ste_free || entry->ste_child_free)
794 ste->ste_child_free = 1;
795 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 temp = PyNumber_InPlaceOr(newfree, allfree);
798 if (!temp)
799 goto error;
800 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 /* Check if any local variables must be converted to cell variables */
803 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
804 NULL))
805 goto error;
806 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000807 "__class__"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 goto error;
809 /* Records the results of the analysis in the symbol table entry */
810 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
811 ste->ste_type == ClassBlock))
812 goto error;
813 if (!check_unoptimized(ste))
814 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 temp = PyNumber_InPlaceOr(free, newfree);
817 if (!temp)
818 goto error;
819 Py_DECREF(temp);
820 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 Py_XDECREF(scopes);
823 Py_XDECREF(local);
824 Py_XDECREF(newbound);
825 Py_XDECREF(newglobal);
826 Py_XDECREF(newfree);
827 Py_XDECREF(allfree);
828 if (!success)
829 assert(PyErr_Occurred());
830 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831}
832
833static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
835 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
838 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 These dictionary are used by all blocks enclosed by the
843 current block. The analyze_block() call modifies these
844 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 */
847 temp_bound = PySet_New(bound);
848 if (!temp_bound)
849 goto error;
850 temp_free = PySet_New(free);
851 if (!temp_free)
852 goto error;
853 temp_global = PySet_New(global);
854 if (!temp_global)
855 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
858 goto error;
859 temp = PyNumber_InPlaceOr(child_free, temp_free);
860 if (!temp)
861 goto error;
862 Py_DECREF(temp);
863 Py_DECREF(temp_bound);
864 Py_DECREF(temp_free);
865 Py_DECREF(temp_global);
866 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000867 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 Py_XDECREF(temp_bound);
869 Py_XDECREF(temp_free);
870 Py_XDECREF(temp_global);
871 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000872}
873
874static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875symtable_analyze(struct symtable *st)
876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 PyObject *free, *global;
878 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 free = PySet_New(NULL);
881 if (!free)
882 return 0;
883 global = PySet_New(NULL);
884 if (!global) {
885 Py_DECREF(free);
886 return 0;
887 }
888 r = analyze_block(st->st_top, NULL, free, global);
889 Py_DECREF(free);
890 Py_DECREF(global);
891 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892}
893
894
895static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000896symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
899 lineno, NULL, NULL) < 0) {
900 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
901 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000902 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
903 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 }
905 return 0;
906 }
907 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908}
909
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000910/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911 This reference is released when the block is exited, via the DECREF
912 in symtable_exit_block().
913*/
914
915static int
916symtable_exit_block(struct symtable *st, void *ast)
917{
Benjamin Peterson609da582011-06-29 22:52:39 -0500918 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919
Benjamin Peterson609da582011-06-29 22:52:39 -0500920 st->st_cur = NULL;
921 size = PyList_GET_SIZE(st->st_stack);
922 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500923 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500925 if (--size)
926 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 }
928 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929}
930
931static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000933 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934{
Benjamin Peterson609da582011-06-29 22:52:39 -0500935 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936
Benjamin Peterson609da582011-06-29 22:52:39 -0500937 ste = ste_new(st, name, block, ast, lineno, col_offset);
938 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500940 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
941 Py_DECREF(ste);
942 return 0;
943 }
944 prev = st->st_cur;
945 /* The entry is owned by the stack. Borrow it for st_cur. */
946 Py_DECREF(ste);
947 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000948 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 st->st_global = st->st_cur->ste_symbols;
950 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500951 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 return 0;
953 }
954 }
955 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956}
957
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000958static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959symtable_lookup(struct symtable *st, PyObject *name)
960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 PyObject *o;
962 PyObject *mangled = _Py_Mangle(st->st_private, name);
963 if (!mangled)
964 return 0;
965 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
966 Py_DECREF(mangled);
967 if (!o)
968 return 0;
969 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970}
971
972static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 PyObject *o;
976 PyObject *dict;
977 long val;
978 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979
Jeremy Hylton81e95022007-02-27 06:50:52 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 if (!mangled)
982 return 0;
983 dict = st->st_cur->ste_symbols;
984 if ((o = PyDict_GetItem(dict, mangled))) {
985 val = PyLong_AS_LONG(o);
986 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
987 /* Is it better to use 'mangled' or 'name' here? */
988 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000989 PyErr_SyntaxLocationEx(st->st_filename,
990 st->st_cur->ste_lineno,
991 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 goto error;
993 }
994 val |= flag;
995 } else
996 val = flag;
997 o = PyLong_FromLong(val);
998 if (o == NULL)
999 goto error;
1000 if (PyDict_SetItem(dict, mangled, o) < 0) {
1001 Py_DECREF(o);
1002 goto error;
1003 }
1004 Py_DECREF(o);
1005
1006 if (flag & DEF_PARAM) {
1007 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1008 goto error;
1009 } else if (flag & DEF_GLOBAL) {
1010 /* XXX need to update DEF_GLOBAL for other flags too;
1011 perhaps only DEF_FREE_GLOBAL */
1012 val = flag;
1013 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1014 val |= PyLong_AS_LONG(o);
1015 }
1016 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 goto error;
1019 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1020 Py_DECREF(o);
1021 goto error;
1022 }
1023 Py_DECREF(o);
1024 }
1025 Py_DECREF(mangled);
1026 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001027
1028error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 Py_DECREF(mangled);
1030 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031}
1032
1033/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1034 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 function.
1036
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1038 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001039
1040 VISIT_QUIT macro returns the specified value exiting from the function but
1041 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042*/
1043
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001044#define VISIT_QUIT(ST, X) \
1045 return --(ST)->recursion_depth,(X)
1046
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001049 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001050
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 int i; \
1053 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1054 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1055 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1056 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001057 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001060
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 int i; \
1063 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1064 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1065 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1066 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001067 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001070
Guido van Rossum4f72a782006-10-27 23:31:49 +00001071#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 int i = 0; \
1073 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1074 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1075 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1076 if (!elt) continue; /* can be NULL */ \
1077 if (!symtable_visit_expr((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001078 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001080}
1081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001083symtable_new_tmpname(struct symtable *st)
1084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 char tmpname[256];
1086 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1089 ++st->st_cur->ste_tmpname);
1090 tmp = PyUnicode_InternFromString(tmpname);
1091 if (!tmp)
1092 return 0;
1093 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1094 return 0;
1095 Py_DECREF(tmp);
1096 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001097}
1098
Guido van Rossum4f72a782006-10-27 23:31:49 +00001099
Guido van Rossumc2e20742006-02-27 22:32:47 +00001100static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101symtable_visit_stmt(struct symtable *st, stmt_ty s)
1102{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001103 if (++st->recursion_depth > st->recursion_limit) {
1104 PyErr_SetString(PyExc_RuntimeError,
1105 "maximum recursion depth exceeded during compilation");
1106 VISIT_QUIT(st, 0);
1107 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 switch (s->kind) {
1109 case FunctionDef_kind:
1110 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001111 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 if (s->v.FunctionDef.args->defaults)
1113 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1114 if (s->v.FunctionDef.args->kw_defaults)
1115 VISIT_KWONLYDEFAULTS(st,
1116 s->v.FunctionDef.args->kw_defaults);
1117 if (!symtable_visit_annotations(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001118 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 if (s->v.FunctionDef.decorator_list)
1120 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1121 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001122 FunctionBlock, (void *)s, s->lineno,
1123 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001124 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001125 VISIT(st, arguments, s->v.FunctionDef.args);
1126 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001128 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 break;
1130 case ClassDef_kind: {
1131 PyObject *tmp;
1132 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001133 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1135 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1136 if (s->v.ClassDef.starargs)
1137 VISIT(st, expr, s->v.ClassDef.starargs);
1138 if (s->v.ClassDef.kwargs)
1139 VISIT(st, expr, s->v.ClassDef.kwargs);
1140 if (s->v.ClassDef.decorator_list)
1141 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1142 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001143 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001144 VISIT_QUIT(st, 0);
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001145 if (!GET_IDENTIFIER(__class__) ||
1146 !symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 !GET_IDENTIFIER(__locals__) ||
1148 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1149 symtable_exit_block(st, s);
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001150 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 }
1152 tmp = st->st_private;
1153 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001154 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 st->st_private = tmp;
1156 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001157 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 break;
1159 }
1160 case Return_kind:
1161 if (s->v.Return.value) {
1162 VISIT(st, expr, s->v.Return.value);
1163 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 }
1165 break;
1166 case Delete_kind:
1167 VISIT_SEQ(st, expr, s->v.Delete.targets);
1168 break;
1169 case Assign_kind:
1170 VISIT_SEQ(st, expr, s->v.Assign.targets);
1171 VISIT(st, expr, s->v.Assign.value);
1172 break;
1173 case AugAssign_kind:
1174 VISIT(st, expr, s->v.AugAssign.target);
1175 VISIT(st, expr, s->v.AugAssign.value);
1176 break;
1177 case For_kind:
1178 VISIT(st, expr, s->v.For.target);
1179 VISIT(st, expr, s->v.For.iter);
1180 VISIT_SEQ(st, stmt, s->v.For.body);
1181 if (s->v.For.orelse)
1182 VISIT_SEQ(st, stmt, s->v.For.orelse);
1183 break;
1184 case While_kind:
1185 VISIT(st, expr, s->v.While.test);
1186 VISIT_SEQ(st, stmt, s->v.While.body);
1187 if (s->v.While.orelse)
1188 VISIT_SEQ(st, stmt, s->v.While.orelse);
1189 break;
1190 case If_kind:
1191 /* XXX if 0: and lookup_yield() hacks */
1192 VISIT(st, expr, s->v.If.test);
1193 VISIT_SEQ(st, stmt, s->v.If.body);
1194 if (s->v.If.orelse)
1195 VISIT_SEQ(st, stmt, s->v.If.orelse);
1196 break;
1197 case Raise_kind:
1198 if (s->v.Raise.exc) {
1199 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001200 if (s->v.Raise.cause) {
1201 VISIT(st, expr, s->v.Raise.cause);
1202 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 }
1204 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001205 case Try_kind:
1206 VISIT_SEQ(st, stmt, s->v.Try.body);
1207 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1208 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1209 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 break;
1211 case Assert_kind:
1212 VISIT(st, expr, s->v.Assert.test);
1213 if (s->v.Assert.msg)
1214 VISIT(st, expr, s->v.Assert.msg);
1215 break;
1216 case Import_kind:
1217 VISIT_SEQ(st, alias, s->v.Import.names);
1218 /* XXX Don't have the lineno available inside
1219 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001220 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001222 st->st_cur->ste_opt_col_offset = s->col_offset;
1223 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 break;
1225 case ImportFrom_kind:
1226 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1227 /* XXX Don't have the lineno available inside
1228 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001229 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001231 st->st_cur->ste_opt_col_offset = s->col_offset;
1232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 break;
1234 case Global_kind: {
1235 int i;
1236 asdl_seq *seq = s->v.Global.names;
1237 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1238 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 long cur = symtable_lookup(st, name);
1240 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001241 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 if (cur & (DEF_LOCAL | USE)) {
1243 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001244 char *c_name = _PyUnicode_AsString(name);
1245 if (!c_name)
1246 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (cur & DEF_LOCAL)
1248 PyOS_snprintf(buf, sizeof(buf),
1249 GLOBAL_AFTER_ASSIGN,
1250 c_name);
1251 else
1252 PyOS_snprintf(buf, sizeof(buf),
1253 GLOBAL_AFTER_USE,
1254 c_name);
1255 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001256 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 }
1258 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001259 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 }
1261 break;
1262 }
1263 case Nonlocal_kind: {
1264 int i;
1265 asdl_seq *seq = s->v.Nonlocal.names;
1266 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1267 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 long cur = symtable_lookup(st, name);
1269 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001270 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 if (cur & (DEF_LOCAL | USE)) {
1272 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001273 char *c_name = _PyUnicode_AsString(name);
1274 if (!c_name)
1275 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 if (cur & DEF_LOCAL)
1277 PyOS_snprintf(buf, sizeof(buf),
1278 NONLOCAL_AFTER_ASSIGN,
1279 c_name);
1280 else
1281 PyOS_snprintf(buf, sizeof(buf),
1282 NONLOCAL_AFTER_USE,
1283 c_name);
1284 if (!symtable_warn(st, buf, s->lineno))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001285 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 }
1287 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001288 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 }
1290 break;
1291 }
1292 case Expr_kind:
1293 VISIT(st, expr, s->v.Expr.value);
1294 break;
1295 case Pass_kind:
1296 case Break_kind:
1297 case Continue_kind:
1298 /* nothing to do here */
1299 break;
1300 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001301 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 VISIT_SEQ(st, stmt, s->v.With.body);
1303 break;
1304 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001305 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306}
1307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309symtable_visit_expr(struct symtable *st, expr_ty e)
1310{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001311 if (++st->recursion_depth > st->recursion_limit) {
1312 PyErr_SetString(PyExc_RuntimeError,
1313 "maximum recursion depth exceeded during compilation");
1314 VISIT_QUIT(st, 0);
1315 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 switch (e->kind) {
1317 case BoolOp_kind:
1318 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1319 break;
1320 case BinOp_kind:
1321 VISIT(st, expr, e->v.BinOp.left);
1322 VISIT(st, expr, e->v.BinOp.right);
1323 break;
1324 case UnaryOp_kind:
1325 VISIT(st, expr, e->v.UnaryOp.operand);
1326 break;
1327 case Lambda_kind: {
1328 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001329 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 if (e->v.Lambda.args->defaults)
1331 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001332 if (e->v.Lambda.args->kw_defaults)
1333 VISIT_KWONLYDEFAULTS(st,
1334 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001336 FunctionBlock, (void *)e, e->lineno,
1337 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001338 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001339 VISIT(st, arguments, e->v.Lambda.args);
1340 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001342 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 break;
1344 }
1345 case IfExp_kind:
1346 VISIT(st, expr, e->v.IfExp.test);
1347 VISIT(st, expr, e->v.IfExp.body);
1348 VISIT(st, expr, e->v.IfExp.orelse);
1349 break;
1350 case Dict_kind:
1351 VISIT_SEQ(st, expr, e->v.Dict.keys);
1352 VISIT_SEQ(st, expr, e->v.Dict.values);
1353 break;
1354 case Set_kind:
1355 VISIT_SEQ(st, expr, e->v.Set.elts);
1356 break;
1357 case GeneratorExp_kind:
1358 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001359 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 break;
1361 case ListComp_kind:
1362 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001363 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 break;
1365 case SetComp_kind:
1366 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001367 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 break;
1369 case DictComp_kind:
1370 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001371 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 break;
1373 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001374 if (e->v.Yield.value)
1375 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001378 case YieldFrom_kind:
1379 VISIT(st, expr, e->v.YieldFrom.value);
1380 st->st_cur->ste_generator = 1;
1381 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 case Compare_kind:
1383 VISIT(st, expr, e->v.Compare.left);
1384 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1385 break;
1386 case Call_kind:
1387 VISIT(st, expr, e->v.Call.func);
1388 VISIT_SEQ(st, expr, e->v.Call.args);
1389 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1390 if (e->v.Call.starargs)
1391 VISIT(st, expr, e->v.Call.starargs);
1392 if (e->v.Call.kwargs)
1393 VISIT(st, expr, e->v.Call.kwargs);
1394 break;
1395 case Num_kind:
1396 case Str_kind:
1397 case Bytes_kind:
1398 case Ellipsis_kind:
1399 /* Nothing to do here. */
1400 break;
1401 /* The following exprs can be assignment targets. */
1402 case Attribute_kind:
1403 VISIT(st, expr, e->v.Attribute.value);
1404 break;
1405 case Subscript_kind:
1406 VISIT(st, expr, e->v.Subscript.value);
1407 VISIT(st, slice, e->v.Subscript.slice);
1408 break;
1409 case Starred_kind:
1410 VISIT(st, expr, e->v.Starred.value);
1411 break;
1412 case Name_kind:
1413 if (!symtable_add_def(st, e->v.Name.id,
1414 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001415 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 /* Special-case super: it counts as a use of __class__ */
1417 if (e->v.Name.ctx == Load &&
1418 st->st_cur->ste_type == FunctionBlock &&
1419 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001420 if (!GET_IDENTIFIER(__class__) ||
1421 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001422 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 }
1424 break;
1425 /* child nodes of List and Tuple will have expr_context set */
1426 case List_kind:
1427 VISIT_SEQ(st, expr, e->v.List.elts);
1428 break;
1429 case Tuple_kind:
1430 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1431 break;
1432 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001433 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434}
1435
1436static int
1437symtable_implicit_arg(struct symtable *st, int pos)
1438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1440 if (id == NULL)
1441 return 0;
1442 if (!symtable_add_def(st, id, DEF_PARAM)) {
1443 Py_DECREF(id);
1444 return 0;
1445 }
1446 Py_DECREF(id);
1447 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448}
1449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001451symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 if (!args)
1456 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 for (i = 0; i < asdl_seq_LEN(args); i++) {
1459 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1460 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1461 return 0;
1462 }
1463
1464 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465}
1466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001468symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 if (!args)
1473 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 for (i = 0; i < asdl_seq_LEN(args); i++) {
1476 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1477 if (arg->annotation)
1478 VISIT(st, expr, arg->annotation);
1479 }
1480
1481 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001482}
1483
Neal Norwitzc1505362006-12-28 06:47:50 +00001484static int
1485symtable_visit_annotations(struct symtable *st, stmt_ty s)
1486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 arguments_ty a = s->v.FunctionDef.args;
1488
1489 if (a->args && !symtable_visit_argannotations(st, a->args))
1490 return 0;
1491 if (a->varargannotation)
1492 VISIT(st, expr, a->varargannotation);
1493 if (a->kwargannotation)
1494 VISIT(st, expr, a->kwargannotation);
1495 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1496 return 0;
1497 if (s->v.FunctionDef.returns)
1498 VISIT(st, expr, s->v.FunctionDef.returns);
1499 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500}
1501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503symtable_visit_arguments(struct symtable *st, arguments_ty a)
1504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 /* skip default arguments inside function block
1506 XXX should ast be different?
1507 */
1508 if (a->args && !symtable_visit_params(st, a->args))
1509 return 0;
1510 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1511 return 0;
1512 if (a->vararg) {
1513 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1514 return 0;
1515 st->st_cur->ste_varargs = 1;
1516 }
1517 if (a->kwarg) {
1518 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1519 return 0;
1520 st->st_cur->ste_varkeywords = 1;
1521 }
1522 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523}
1524
1525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 if (eh->v.ExceptHandler.type)
1530 VISIT(st, expr, eh->v.ExceptHandler.type);
1531 if (eh->v.ExceptHandler.name)
1532 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1533 return 0;
1534 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1535 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536}
1537
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001538static int
1539symtable_visit_withitem(struct symtable *st, withitem_ty item)
1540{
1541 VISIT(st, expr, item->context_expr);
1542 if (item->optional_vars) {
1543 VISIT(st, expr, item->optional_vars);
1544 }
1545 return 1;
1546}
1547
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550symtable_visit_alias(struct symtable *st, alias_ty a)
1551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001553 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 dotted package name (e.g. spam.eggs)
1555 */
1556 PyObject *store_name;
1557 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001558 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1559 PyUnicode_GET_LENGTH(name), 1);
1560 if (dot != -1) {
1561 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (!store_name)
1563 return 0;
1564 }
1565 else {
1566 store_name = name;
1567 Py_INCREF(store_name);
1568 }
1569 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1570 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1571 Py_DECREF(store_name);
1572 return r;
1573 }
1574 else {
1575 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001576 int lineno = st->st_cur->ste_lineno;
1577 int col_offset = st->st_cur->ste_col_offset;
1578 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1579 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1580 Py_DECREF(store_name);
1581 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 }
1583 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1584 Py_DECREF(store_name);
1585 return 1;
1586 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587}
1588
1589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 VISIT(st, expr, lc->target);
1594 VISIT(st, expr, lc->iter);
1595 VISIT_SEQ(st, expr, lc->ifs);
1596 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597}
1598
1599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601symtable_visit_keyword(struct symtable *st, keyword_ty k)
1602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 VISIT(st, expr, k->value);
1604 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605}
1606
1607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609symtable_visit_slice(struct symtable *st, slice_ty s)
1610{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 switch (s->kind) {
1612 case Slice_kind:
1613 if (s->v.Slice.lower)
1614 VISIT(st, expr, s->v.Slice.lower)
1615 if (s->v.Slice.upper)
1616 VISIT(st, expr, s->v.Slice.upper)
1617 if (s->v.Slice.step)
1618 VISIT(st, expr, s->v.Slice.step)
1619 break;
1620 case ExtSlice_kind:
1621 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1622 break;
1623 case Index_kind:
1624 VISIT(st, expr, s->v.Index.value)
1625 break;
1626 }
1627 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628}
1629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001631symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001632 identifier scope_name, asdl_seq *generators,
1633 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 int is_generator = (e->kind == GeneratorExp_kind);
1636 int needs_tmp = !is_generator;
1637 comprehension_ty outermost = ((comprehension_ty)
1638 asdl_seq_GET(generators, 0));
1639 /* Outermost iterator is evaluated in current scope */
1640 VISIT(st, expr, outermost->iter);
1641 /* Create comprehension scope for the rest */
1642 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001643 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1644 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 return 0;
1646 }
1647 st->st_cur->ste_generator = is_generator;
1648 /* Outermost iter is received as an argument */
1649 if (!symtable_implicit_arg(st, 0)) {
1650 symtable_exit_block(st, (void *)e);
1651 return 0;
1652 }
1653 /* Allocate temporary name if needed */
1654 if (needs_tmp && !symtable_new_tmpname(st)) {
1655 symtable_exit_block(st, (void *)e);
1656 return 0;
1657 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001658 VISIT(st, expr, outermost->target);
1659 VISIT_SEQ(st, expr, outermost->ifs);
1660 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001662 VISIT(st, expr, value);
1663 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001668symtable_visit_genexp(struct symtable *st, expr_ty e)
1669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1671 e->v.GeneratorExp.generators,
1672 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001673}
1674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001676symtable_visit_listcomp(struct symtable *st, expr_ty e)
1677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1679 e->v.ListComp.generators,
1680 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001681}
1682
1683static int
1684symtable_visit_setcomp(struct symtable *st, expr_ty e)
1685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1687 e->v.SetComp.generators,
1688 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001689}
1690
1691static int
1692symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1695 e->v.DictComp.generators,
1696 e->v.DictComp.key,
1697 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001698}