blob: 35fc6e195431c6207a0ad30610bb438d43a13238 [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
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000022#define RETURN_VAL_IN_GENERATOR \
23 "'return' with argument inside generator"
Neal Norwitz5d0ad502005-12-19 04:27:42 +000024
Benjamin Peterson55e00f22008-08-17 18:02:44 +000025
Neal Norwitz090b3dd2006-02-28 22:36:46 +000026static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000027ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000028 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020031 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 k = PyLong_FromVoidPtr(key);
34 if (k == NULL)
35 goto fail;
36 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020037 if (ste == NULL) {
38 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020040 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020042 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 ste->ste_name = name;
45 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 ste->ste_symbols = NULL;
48 ste->ste_varnames = NULL;
49 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 ste->ste_symbols = PyDict_New();
52 if (ste->ste_symbols == NULL)
53 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 ste->ste_varnames = PyList_New(0);
56 if (ste->ste_varnames == NULL)
57 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 ste->ste_children = PyList_New(0);
60 if (ste->ste_children == NULL)
61 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 ste->ste_type = block;
64 ste->ste_unoptimized = 0;
65 ste->ste_nested = 0;
66 ste->ste_free = 0;
67 ste->ste_varargs = 0;
68 ste->ste_varkeywords = 0;
69 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000070 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000071 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000073 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 if (st->st_cur != NULL &&
76 (st->st_cur->ste_nested ||
77 st->st_cur->ste_type == FunctionBlock))
78 ste->ste_nested = 1;
79 ste->ste_child_free = 0;
80 ste->ste_generator = 0;
81 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
84 goto fail;
85
86 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000087 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 Py_XDECREF(ste);
89 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000090}
91
92static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000093ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
96 ste->ste_name,
97 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000098}
99
100static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000101ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 ste->ste_table = NULL;
104 Py_XDECREF(ste->ste_id);
105 Py_XDECREF(ste->ste_name);
106 Py_XDECREF(ste->ste_symbols);
107 Py_XDECREF(ste->ste_varnames);
108 Py_XDECREF(ste->ste_children);
109 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000110}
111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000113
Guido van Rossum6f799372001-09-20 20:46:19 +0000114static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 {"id", T_OBJECT, OFF(ste_id), READONLY},
116 {"name", T_OBJECT, OFF(ste_name), READONLY},
117 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
118 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
119 {"children", T_OBJECT, OFF(ste_children), READONLY},
120 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
121 {"nested", T_INT, OFF(ste_nested), READONLY},
122 {"type", T_INT, OFF(ste_type), READONLY},
123 {"lineno", T_INT, OFF(ste_lineno), READONLY},
124 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000125};
126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 PyVarObject_HEAD_INIT(&PyType_Type, 0)
129 "symtable entry",
130 sizeof(PySTEntryObject),
131 0,
132 (destructor)ste_dealloc, /* tp_dealloc */
133 0, /* tp_print */
134 0, /* tp_getattr */
135 0, /* tp_setattr */
136 0, /* tp_reserved */
137 (reprfunc)ste_repr, /* tp_repr */
138 0, /* tp_as_number */
139 0, /* tp_as_sequence */
140 0, /* tp_as_mapping */
141 0, /* tp_hash */
142 0, /* tp_call */
143 0, /* tp_str */
144 PyObject_GenericGetAttr, /* tp_getattro */
145 0, /* tp_setattro */
146 0, /* tp_as_buffer */
147 Py_TPFLAGS_DEFAULT, /* tp_flags */
148 0, /* tp_doc */
149 0, /* tp_traverse */
150 0, /* tp_clear */
151 0, /* tp_richcompare */
152 0, /* tp_weaklistoffset */
153 0, /* tp_iter */
154 0, /* tp_iternext */
155 0, /* tp_methods */
156 ste_memberlist, /* tp_members */
157 0, /* tp_getset */
158 0, /* tp_base */
159 0, /* tp_dict */
160 0, /* tp_descr_get */
161 0, /* tp_descr_set */
162 0, /* tp_dictoffset */
163 0, /* tp_init */
164 0, /* tp_alloc */
165 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000166};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167
168static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000169static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000171 _Py_block_ty block, void *ast, int lineno,
172 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173static int symtable_exit_block(struct symtable *st, void *ast);
174static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
175static int symtable_visit_expr(struct symtable *st, expr_ty s);
176static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000177static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
178static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000179static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180static int symtable_visit_arguments(struct symtable *st, arguments_ty);
181static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
182static int symtable_visit_alias(struct symtable *st, alias_ty);
183static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
184static int symtable_visit_keyword(struct symtable *st, keyword_ty);
185static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000186static int symtable_visit_params(struct symtable *st, asdl_seq *args);
187static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000189static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191
Nick Coghlan650f0d02007-04-15 12:05:43 +0000192static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
194 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
199#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000200"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
202static struct symtable *
203symtable_new(void)
204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
208 if (st == NULL)
209 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 st->st_filename = NULL;
212 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if ((st->st_stack = PyList_New(0)) == NULL)
215 goto fail;
216 if ((st->st_blocks = PyDict_New()) == NULL)
217 goto fail;
218 st->st_cur = NULL;
219 st->st_private = NULL;
220 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 PySymtable_Free(st);
223 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224}
225
226struct symtable *
227PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 struct symtable *st = symtable_new();
230 asdl_seq *seq;
231 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (st == NULL)
234 return st;
235 st->st_filename = filename;
236 st->st_future = future;
237 /* Make the initial symbol information gathering pass */
238 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000239 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 PySymtable_Free(st);
241 return NULL;
242 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 st->st_top = st->st_cur;
245 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
246 switch (mod->kind) {
247 case Module_kind:
248 seq = mod->v.Module.body;
249 for (i = 0; i < asdl_seq_LEN(seq); i++)
250 if (!symtable_visit_stmt(st,
251 (stmt_ty)asdl_seq_GET(seq, i)))
252 goto error;
253 break;
254 case Expression_kind:
255 if (!symtable_visit_expr(st, mod->v.Expression.body))
256 goto error;
257 break;
258 case Interactive_kind:
259 seq = mod->v.Interactive.body;
260 for (i = 0; i < asdl_seq_LEN(seq); i++)
261 if (!symtable_visit_stmt(st,
262 (stmt_ty)asdl_seq_GET(seq, i)))
263 goto error;
264 break;
265 case Suite_kind:
266 PyErr_SetString(PyExc_RuntimeError,
267 "this compiler does not handle Suites");
268 goto error;
269 }
270 if (!symtable_exit_block(st, (void *)mod)) {
271 PySymtable_Free(st);
272 return NULL;
273 }
274 /* Make the second symbol analysis pass */
275 if (symtable_analyze(st))
276 return st;
277 PySymtable_Free(st);
278 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 (void) symtable_exit_block(st, (void *)mod);
281 PySymtable_Free(st);
282 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283}
284
285void
286PySymtable_Free(struct symtable *st)
287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_XDECREF(st->st_blocks);
289 Py_XDECREF(st->st_stack);
290 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291}
292
293PySTEntryObject *
294PySymtable_Lookup(struct symtable *st, void *key)
295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 k = PyLong_FromVoidPtr(key);
299 if (k == NULL)
300 return NULL;
301 v = PyDict_GetItem(st->st_blocks, k);
302 if (v) {
303 assert(PySTEntry_Check(v));
304 Py_INCREF(v);
305 }
306 else {
307 PyErr_SetString(PyExc_KeyError,
308 "unknown symbol table entry");
309 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_DECREF(k);
312 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313}
314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000316PyST_GetScope(PySTEntryObject *ste, PyObject *name)
317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
319 if (!v)
320 return 0;
321 assert(PyLong_Check(v));
322 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323}
324
325
326/* Analyze raw symbol information to determine scope of each name.
327
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000328 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334 explicit global is declared with the global statement. An implicit
335 global is a free variable for which the compiler has found no binding
336 in an enclosing function scope. The implicit global is either a global
337 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
338 to handle these names to implement slightly odd semantics. In such a
339 block, the name is treated as global until it is assigned to; then it
340 is treated as a local.
341
342 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000343 The first pass collects raw facts from the AST via the symtable_visit_*
344 functions: the name is a parameter here, the name is used but not defined
345 here, etc. The second pass analyzes these facts during a pass over the
346 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347
348 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000350 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000351 Names which are explicitly declared nonlocal must exist in this set of
352 visible names - if they do not, a syntax error is raised. After doing
353 the local analysis, it analyzes each of its child blocks using an
354 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355
Nick Coghlan650f0d02007-04-15 12:05:43 +0000356 The children update the free variable set. If a local variable is added to
357 the free variable set by the child, the variable is marked as a cell. The
358 function object being defined must provide runtime storage for the variable
359 that may outlive the function's frame. Cell variables are removed from the
360 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000361
Nick Coghlan650f0d02007-04-15 12:05:43 +0000362 During analysis, the names are:
363 symbols: dict mapping from symbol names to flag values (including offset scope values)
364 scopes: dict mapping from symbol names to scope values (no offset)
365 local: set of all symbol names local to the current scope
366 bound: set of all symbol names local to a containing function scope
367 free: set of all symbol names referenced but not bound in child scopes
368 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369*/
370
371#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 PyObject *o = PyLong_FromLong(I); \
373 if (!o) \
374 return 0; \
375 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
376 Py_DECREF(o); \
377 return 0; \
378 } \
379 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380}
381
382/* Decide on scope of name, given flags.
383
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000384 The namespace dictionaries may be modified to record information
385 about the new name. For example, a new global will add an entry to
386 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387*/
388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000390analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 PyObject *bound, PyObject *local, PyObject *free,
392 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (flags & DEF_GLOBAL) {
395 if (flags & DEF_PARAM) {
396 PyErr_Format(PyExc_SyntaxError,
397 "name '%U' is parameter and global",
398 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000399 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
400 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401
402 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000403 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 if (flags & DEF_NONLOCAL) {
405 PyErr_Format(PyExc_SyntaxError,
406 "name '%U' is nonlocal and global",
407 name);
408 return 0;
409 }
410 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
411 if (PySet_Add(global, name) < 0)
412 return 0;
413 if (bound && (PySet_Discard(bound, name) < 0))
414 return 0;
415 return 1;
416 }
417 if (flags & DEF_NONLOCAL) {
418 if (flags & DEF_PARAM) {
419 PyErr_Format(PyExc_SyntaxError,
420 "name '%U' is parameter and nonlocal",
421 name);
422 return 0;
423 }
424 if (!bound) {
425 PyErr_Format(PyExc_SyntaxError,
426 "nonlocal declaration not allowed at module level");
427 return 0;
428 }
429 if (!PySet_Contains(bound, name)) {
430 PyErr_Format(PyExc_SyntaxError,
431 "no binding for nonlocal '%U' found",
432 name);
433
434 return 0;
435 }
436 SET_SCOPE(scopes, name, FREE);
437 ste->ste_free = 1;
438 return PySet_Add(free, name) >= 0;
439 }
440 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000441 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 if (PySet_Add(local, name) < 0)
443 return 0;
444 if (PySet_Discard(global, name) < 0)
445 return 0;
446 return 1;
447 }
448 /* If an enclosing block has a binding for this name, it
449 is a free variable rather than a global variable.
450 Note that having a non-NULL bound implies that the block
451 is nested.
452 */
453 if (bound && PySet_Contains(bound, name)) {
454 SET_SCOPE(scopes, name, FREE);
455 ste->ste_free = 1;
456 return PySet_Add(free, name) >= 0;
457 }
458 /* If a parent has a global statement, then call it global
459 explicit? It could also be global implicit.
460 */
461 if (global && PySet_Contains(global, name)) {
462 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
463 return 1;
464 }
465 if (ste->ste_nested)
466 ste->ste_free = 1;
467 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
468 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469}
470
471#undef SET_SCOPE
472
473/* If a name is defined in free and also in locals, then this block
474 provides the binding for the free variable. The name should be
475 marked CELL in this block and removed from the free list.
476
477 Note that the current block's free variables are included in free.
478 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000479
Martin v. Löwis2673a572007-10-29 19:54:24 +0000480 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000481 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482*/
483
484static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000485analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 PyObject *name, *v, *v_cell;
488 int success = 0;
489 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 v_cell = PyLong_FromLong(CELL);
492 if (!v_cell)
493 return 0;
494 while (PyDict_Next(scopes, &pos, &name, &v)) {
495 long scope;
496 assert(PyLong_Check(v));
497 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000498 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 continue;
500 if (!PySet_Contains(free, name))
501 continue;
502 if (restricted != NULL &&
503 PyUnicode_CompareWithASCIIString(name, restricted))
504 continue;
505 /* Replace LOCAL with CELL for this name, and remove
506 from free. It is safe to replace the value of name
507 in the dict, because it will not cause a resize.
508 */
509 if (PyDict_SetItem(scopes, name, v_cell) < 0)
510 goto error;
511 if (PySet_Discard(free, name) < 0)
512 goto error;
513 }
514 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 Py_DECREF(v_cell);
517 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518}
519
520/* Check for illegal statements in unoptimized namespaces */
521static int
522check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
526 || !(ste->ste_free || ste->ste_child_free))
527 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 trailer = (ste->ste_child_free ?
530 "contains a nested function with free variables" :
531 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 switch (ste->ste_unoptimized) {
534 case OPT_TOPLEVEL: /* import * at top-level is fine */
535 return 1;
536 case OPT_IMPORT_STAR:
537 PyErr_Format(PyExc_SyntaxError,
538 "import * is not allowed in function '%U' because it %s",
539 ste->ste_name, trailer);
540 break;
541 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000543 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
544 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546}
547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548/* Enter the final scope information into the ste_symbols dict.
549 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550 * All arguments are dicts. Modifies symbols, others are read-only.
551*/
552static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 PyObject *name = NULL, *itr = NULL;
557 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
558 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* Update scope information for all symbols in this scope */
561 while (PyDict_Next(symbols, &pos, &name, &v)) {
562 long scope, flags;
563 assert(PyLong_Check(v));
564 flags = PyLong_AS_LONG(v);
565 v_scope = PyDict_GetItem(scopes, name);
566 assert(v_scope && PyLong_Check(v_scope));
567 scope = PyLong_AS_LONG(v_scope);
568 flags |= (scope << SCOPE_OFFSET);
569 v_new = PyLong_FromLong(flags);
570 if (!v_new)
571 return 0;
572 if (PyDict_SetItem(symbols, name, v_new) < 0) {
573 Py_DECREF(v_new);
574 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 Py_DECREF(v_new);
577 }
578
579 /* Record not yet resolved free variables from children (if any) */
580 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
581 if (!v_free)
582 return 0;
583
584 itr = PyObject_GetIter(free);
585 if (!itr)
586 goto error;
587
588 while ((name = PyIter_Next(itr))) {
589 v = PyDict_GetItem(symbols, name);
590
591 /* Handle symbol that already exists in this scope */
592 if (v) {
593 /* Handle a free variable in a method of
594 the class that has the same name as a local
595 or global in the class scope.
596 */
597 if (classflag &&
598 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
599 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
600 v_new = PyLong_FromLong(flags);
601 if (!v_new) {
602 goto error;
603 }
604 if (PyDict_SetItem(symbols, name, v_new) < 0) {
605 Py_DECREF(v_new);
606 goto error;
607 }
608 Py_DECREF(v_new);
609 }
610 /* It's a cell, or already free in this scope */
611 Py_DECREF(name);
612 continue;
613 }
614 /* Handle global symbol */
615 if (!PySet_Contains(bound, name)) {
616 Py_DECREF(name);
617 continue; /* it's a global */
618 }
619 /* Propagate new free symbol up the lexical stack */
620 if (PyDict_SetItem(symbols, name, v_free) < 0) {
621 goto error;
622 }
623 Py_DECREF(name);
624 }
625 Py_DECREF(itr);
626 Py_DECREF(v_free);
627 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000628error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 Py_XDECREF(v_free);
630 Py_XDECREF(itr);
631 Py_XDECREF(name);
632 return 0;
633}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634
635/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000636
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 Arguments:
638 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000639 bound -- set of variables bound in enclosing scopes (input). bound
640 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641 free -- set of free variables in enclosed scopes (output)
642 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000643
644 The implementation uses two mutually recursive functions,
645 analyze_block() and analyze_child_block(). analyze_block() is
646 responsible for analyzing the individual names defined in a block.
647 analyze_child_block() prepares temporary namespace dictionaries
648 used to evaluated nested blocks.
649
650 The two functions exist because a child block should see the name
651 bindings of its enclosing blocks, but those bindings should not
652 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653*/
654
655static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
657 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000658
659static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
661 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
664 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
665 PyObject *temp;
666 int i, success = 0;
667 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 local = PySet_New(NULL); /* collect new names bound in block */
670 if (!local)
671 goto error;
672 scopes = PyDict_New(); /* collect scopes defined for each name */
673 if (!scopes)
674 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 /* Allocate new global and bound variable dictionaries. These
677 dictionaries hold the names visible in nested blocks. For
678 ClassBlocks, the bound and global names are initialized
679 before analyzing names, because class bindings aren't
680 visible in methods. For other blocks, they are initialized
681 after names are analyzed.
682 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 /* TODO(jhylton): Package these dicts in a struct so that we
685 can write reasonable helper functions?
686 */
687 newglobal = PySet_New(NULL);
688 if (!newglobal)
689 goto error;
690 newfree = PySet_New(NULL);
691 if (!newfree)
692 goto error;
693 newbound = PySet_New(NULL);
694 if (!newbound)
695 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 /* Class namespace has no effect on names visible in
698 nested functions, so populate the global and bound
699 sets to be passed to child blocks before analyzing
700 this one.
701 */
702 if (ste->ste_type == ClassBlock) {
703 /* Pass down known globals */
704 temp = PyNumber_InPlaceOr(newglobal, global);
705 if (!temp)
706 goto error;
707 Py_DECREF(temp);
708 /* Pass down previously bound symbols */
709 if (bound) {
710 temp = PyNumber_InPlaceOr(newbound, bound);
711 if (!temp)
712 goto error;
713 Py_DECREF(temp);
714 }
715 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
718 long flags = PyLong_AS_LONG(v);
719 if (!analyze_name(ste, scopes, name, flags,
720 bound, local, free, global))
721 goto error;
722 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 /* Populate global and bound sets to be passed to children. */
725 if (ste->ste_type != ClassBlock) {
726 /* Add function locals to bound set */
727 if (ste->ste_type == FunctionBlock) {
728 temp = PyNumber_InPlaceOr(newbound, local);
729 if (!temp)
730 goto error;
731 Py_DECREF(temp);
732 }
733 /* Pass down previously bound symbols */
734 if (bound) {
735 temp = PyNumber_InPlaceOr(newbound, bound);
736 if (!temp)
737 goto error;
738 Py_DECREF(temp);
739 }
740 /* Pass down known globals */
741 temp = PyNumber_InPlaceOr(newglobal, global);
742 if (!temp)
743 goto error;
744 Py_DECREF(temp);
745 }
746 else {
747 /* Special-case __class__ */
748 if (!GET_IDENTIFIER(__class__))
749 goto error;
750 assert(PySet_Contains(local, __class__) == 1);
751 if (PySet_Add(newbound, __class__) < 0)
752 goto error;
753 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 /* Recursively call analyze_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 newbound, newglobal now contain the names visible in
758 nested blocks. The free variables in the children will
759 be collected in allfree.
760 */
761 allfree = PySet_New(NULL);
762 if (!allfree)
763 goto error;
764 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
765 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
766 PySTEntryObject* entry;
767 assert(c && PySTEntry_Check(c));
768 entry = (PySTEntryObject*)c;
769 if (!analyze_child_block(entry, newbound, newfree, newglobal,
770 allfree))
771 goto error;
772 /* Check if any children have free variables */
773 if (entry->ste_free || entry->ste_child_free)
774 ste->ste_child_free = 1;
775 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 temp = PyNumber_InPlaceOr(newfree, allfree);
778 if (!temp)
779 goto error;
780 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 /* Check if any local variables must be converted to cell variables */
783 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
784 NULL))
785 goto error;
786 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
787 "__class__"))
788 goto error;
789 /* Records the results of the analysis in the symbol table entry */
790 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
791 ste->ste_type == ClassBlock))
792 goto error;
793 if (!check_unoptimized(ste))
794 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 temp = PyNumber_InPlaceOr(free, newfree);
797 if (!temp)
798 goto error;
799 Py_DECREF(temp);
800 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 Py_XDECREF(scopes);
803 Py_XDECREF(local);
804 Py_XDECREF(newbound);
805 Py_XDECREF(newglobal);
806 Py_XDECREF(newfree);
807 Py_XDECREF(allfree);
808 if (!success)
809 assert(PyErr_Occurred());
810 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811}
812
813static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
815 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
818 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 These dictionary are used by all blocks enclosed by the
823 current block. The analyze_block() call modifies these
824 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 */
827 temp_bound = PySet_New(bound);
828 if (!temp_bound)
829 goto error;
830 temp_free = PySet_New(free);
831 if (!temp_free)
832 goto error;
833 temp_global = PySet_New(global);
834 if (!temp_global)
835 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
838 goto error;
839 temp = PyNumber_InPlaceOr(child_free, temp_free);
840 if (!temp)
841 goto error;
842 Py_DECREF(temp);
843 Py_DECREF(temp_bound);
844 Py_DECREF(temp_free);
845 Py_DECREF(temp_global);
846 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000847 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 Py_XDECREF(temp_bound);
849 Py_XDECREF(temp_free);
850 Py_XDECREF(temp_global);
851 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000852}
853
854static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855symtable_analyze(struct symtable *st)
856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyObject *free, *global;
858 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 free = PySet_New(NULL);
861 if (!free)
862 return 0;
863 global = PySet_New(NULL);
864 if (!global) {
865 Py_DECREF(free);
866 return 0;
867 }
868 r = analyze_block(st->st_top, NULL, free, global);
869 Py_DECREF(free);
870 Py_DECREF(global);
871 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872}
873
874
875static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000876symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
879 lineno, NULL, NULL) < 0) {
880 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
881 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000882 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
883 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 }
885 return 0;
886 }
887 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888}
889
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000890/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891 This reference is released when the block is exited, via the DECREF
892 in symtable_exit_block().
893*/
894
895static int
896symtable_exit_block(struct symtable *st, void *ast)
897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 Py_CLEAR(st->st_cur);
901 end = PyList_GET_SIZE(st->st_stack) - 1;
902 if (end >= 0) {
903 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
904 end);
905 if (st->st_cur == NULL)
906 return 0;
907 Py_INCREF(st->st_cur);
908 if (PySequence_DelItem(st->st_stack, end) < 0)
909 return 0;
910 }
911 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912}
913
914static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000916 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 if (st->st_cur) {
921 prev = st->st_cur;
922 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
923 return 0;
924 }
925 Py_DECREF(st->st_cur);
926 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000927 st->st_cur = ste_new(st, name, block, ast, lineno, col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 if (st->st_cur == NULL)
929 return 0;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000930 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 st->st_global = st->st_cur->ste_symbols;
932 if (prev) {
933 if (PyList_Append(prev->ste_children,
934 (PyObject *)st->st_cur) < 0) {
935 return 0;
936 }
937 }
938 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939}
940
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000941static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942symtable_lookup(struct symtable *st, PyObject *name)
943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 PyObject *o;
945 PyObject *mangled = _Py_Mangle(st->st_private, name);
946 if (!mangled)
947 return 0;
948 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
949 Py_DECREF(mangled);
950 if (!o)
951 return 0;
952 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953}
954
955static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 PyObject *o;
959 PyObject *dict;
960 long val;
961 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962
Jeremy Hylton81e95022007-02-27 06:50:52 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 if (!mangled)
965 return 0;
966 dict = st->st_cur->ste_symbols;
967 if ((o = PyDict_GetItem(dict, mangled))) {
968 val = PyLong_AS_LONG(o);
969 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
970 /* Is it better to use 'mangled' or 'name' here? */
971 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000972 PyErr_SyntaxLocationEx(st->st_filename,
973 st->st_cur->ste_lineno,
974 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 goto error;
976 }
977 val |= flag;
978 } else
979 val = flag;
980 o = PyLong_FromLong(val);
981 if (o == NULL)
982 goto error;
983 if (PyDict_SetItem(dict, mangled, o) < 0) {
984 Py_DECREF(o);
985 goto error;
986 }
987 Py_DECREF(o);
988
989 if (flag & DEF_PARAM) {
990 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
991 goto error;
992 } else if (flag & DEF_GLOBAL) {
993 /* XXX need to update DEF_GLOBAL for other flags too;
994 perhaps only DEF_FREE_GLOBAL */
995 val = flag;
996 if ((o = PyDict_GetItem(st->st_global, mangled))) {
997 val |= PyLong_AS_LONG(o);
998 }
999 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 goto error;
1002 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1003 Py_DECREF(o);
1004 goto error;
1005 }
1006 Py_DECREF(o);
1007 }
1008 Py_DECREF(mangled);
1009 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001010
1011error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 Py_DECREF(mangled);
1013 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014}
1015
1016/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1017 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 function.
1019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1021 useful if the first node in the sequence requires special treatment.
1022*/
1023
1024#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 if (!symtable_visit_ ## TYPE((ST), (V))) \
1026 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001027
1028#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1030 symtable_exit_block((ST), (S)); \
1031 return 0; \
1032 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001033
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 int i; \
1036 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1037 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1038 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1039 if (!symtable_visit_ ## TYPE((ST), elt)) \
1040 return 0; \
1041 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001043
1044#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 int i; \
1046 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1047 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1048 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1049 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1050 symtable_exit_block((ST), (S)); \
1051 return 0; \
1052 } \
1053 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001054}
1055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 int i; \
1058 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1059 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1060 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1061 if (!symtable_visit_ ## TYPE((ST), elt)) \
1062 return 0; \
1063 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001065
1066#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 int i; \
1068 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1069 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1070 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1071 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1072 symtable_exit_block((ST), (S)); \
1073 return 0; \
1074 } \
1075 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001076}
1077
Guido van Rossum4f72a782006-10-27 23:31:49 +00001078#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 int i = 0; \
1080 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1081 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1082 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1083 if (!elt) continue; /* can be NULL */ \
1084 if (!symtable_visit_expr((ST), elt)) \
1085 return 0; \
1086 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001087}
1088
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001090symtable_new_tmpname(struct symtable *st)
1091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 char tmpname[256];
1093 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1096 ++st->st_cur->ste_tmpname);
1097 tmp = PyUnicode_InternFromString(tmpname);
1098 if (!tmp)
1099 return 0;
1100 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1101 return 0;
1102 Py_DECREF(tmp);
1103 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001104}
1105
Guido van Rossum4f72a782006-10-27 23:31:49 +00001106
Guido van Rossumc2e20742006-02-27 22:32:47 +00001107static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108symtable_visit_stmt(struct symtable *st, stmt_ty s)
1109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 switch (s->kind) {
1111 case FunctionDef_kind:
1112 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1113 return 0;
1114 if (s->v.FunctionDef.args->defaults)
1115 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1116 if (s->v.FunctionDef.args->kw_defaults)
1117 VISIT_KWONLYDEFAULTS(st,
1118 s->v.FunctionDef.args->kw_defaults);
1119 if (!symtable_visit_annotations(st, s))
1120 return 0;
1121 if (s->v.FunctionDef.decorator_list)
1122 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1123 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001124 FunctionBlock, (void *)s, s->lineno,
1125 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 return 0;
1127 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1128 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1129 if (!symtable_exit_block(st, s))
1130 return 0;
1131 break;
1132 case ClassDef_kind: {
1133 PyObject *tmp;
1134 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1135 return 0;
1136 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1137 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1138 if (s->v.ClassDef.starargs)
1139 VISIT(st, expr, s->v.ClassDef.starargs);
1140 if (s->v.ClassDef.kwargs)
1141 VISIT(st, expr, s->v.ClassDef.kwargs);
1142 if (s->v.ClassDef.decorator_list)
1143 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1144 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001145 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 return 0;
1147 if (!GET_IDENTIFIER(__class__) ||
1148 !symtable_add_def(st, __class__, DEF_LOCAL) ||
1149 !GET_IDENTIFIER(__locals__) ||
1150 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1151 symtable_exit_block(st, s);
1152 return 0;
1153 }
1154 tmp = st->st_private;
1155 st->st_private = s->v.ClassDef.name;
1156 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1157 st->st_private = tmp;
1158 if (!symtable_exit_block(st, s))
1159 return 0;
1160 break;
1161 }
1162 case Return_kind:
1163 if (s->v.Return.value) {
1164 VISIT(st, expr, s->v.Return.value);
1165 st->st_cur->ste_returns_value = 1;
1166 if (st->st_cur->ste_generator) {
1167 PyErr_SetString(PyExc_SyntaxError,
1168 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001169 PyErr_SyntaxLocationEx(st->st_filename,
1170 s->lineno,
1171 s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00001173 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 }
1175 break;
1176 case Delete_kind:
1177 VISIT_SEQ(st, expr, s->v.Delete.targets);
1178 break;
1179 case Assign_kind:
1180 VISIT_SEQ(st, expr, s->v.Assign.targets);
1181 VISIT(st, expr, s->v.Assign.value);
1182 break;
1183 case AugAssign_kind:
1184 VISIT(st, expr, s->v.AugAssign.target);
1185 VISIT(st, expr, s->v.AugAssign.value);
1186 break;
1187 case For_kind:
1188 VISIT(st, expr, s->v.For.target);
1189 VISIT(st, expr, s->v.For.iter);
1190 VISIT_SEQ(st, stmt, s->v.For.body);
1191 if (s->v.For.orelse)
1192 VISIT_SEQ(st, stmt, s->v.For.orelse);
1193 break;
1194 case While_kind:
1195 VISIT(st, expr, s->v.While.test);
1196 VISIT_SEQ(st, stmt, s->v.While.body);
1197 if (s->v.While.orelse)
1198 VISIT_SEQ(st, stmt, s->v.While.orelse);
1199 break;
1200 case If_kind:
1201 /* XXX if 0: and lookup_yield() hacks */
1202 VISIT(st, expr, s->v.If.test);
1203 VISIT_SEQ(st, stmt, s->v.If.body);
1204 if (s->v.If.orelse)
1205 VISIT_SEQ(st, stmt, s->v.If.orelse);
1206 break;
1207 case Raise_kind:
1208 if (s->v.Raise.exc) {
1209 VISIT(st, expr, s->v.Raise.exc);
1210 if (s->v.Raise.cause) {
1211 VISIT(st, expr, s->v.Raise.cause);
1212 }
1213 }
1214 break;
1215 case TryExcept_kind:
1216 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1217 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1218 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1219 break;
1220 case TryFinally_kind:
1221 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1222 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1223 break;
1224 case Assert_kind:
1225 VISIT(st, expr, s->v.Assert.test);
1226 if (s->v.Assert.msg)
1227 VISIT(st, expr, s->v.Assert.msg);
1228 break;
1229 case Import_kind:
1230 VISIT_SEQ(st, alias, s->v.Import.names);
1231 /* XXX Don't have the lineno available inside
1232 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001233 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001235 st->st_cur->ste_opt_col_offset = s->col_offset;
1236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 break;
1238 case ImportFrom_kind:
1239 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1240 /* XXX Don't have the lineno available inside
1241 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001242 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001244 st->st_cur->ste_opt_col_offset = s->col_offset;
1245 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 break;
1247 case Global_kind: {
1248 int i;
1249 asdl_seq *seq = s->v.Global.names;
1250 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1251 identifier name = (identifier)asdl_seq_GET(seq, i);
1252 char *c_name = _PyUnicode_AsString(name);
1253 long cur = symtable_lookup(st, name);
1254 if (cur < 0)
1255 return 0;
1256 if (cur & (DEF_LOCAL | USE)) {
1257 char buf[256];
1258 if (cur & DEF_LOCAL)
1259 PyOS_snprintf(buf, sizeof(buf),
1260 GLOBAL_AFTER_ASSIGN,
1261 c_name);
1262 else
1263 PyOS_snprintf(buf, sizeof(buf),
1264 GLOBAL_AFTER_USE,
1265 c_name);
1266 if (!symtable_warn(st, buf, s->lineno))
1267 return 0;
1268 }
1269 if (!symtable_add_def(st, name, DEF_GLOBAL))
1270 return 0;
1271 }
1272 break;
1273 }
1274 case Nonlocal_kind: {
1275 int i;
1276 asdl_seq *seq = s->v.Nonlocal.names;
1277 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1278 identifier name = (identifier)asdl_seq_GET(seq, i);
1279 char *c_name = _PyUnicode_AsString(name);
1280 long cur = symtable_lookup(st, name);
1281 if (cur < 0)
1282 return 0;
1283 if (cur & (DEF_LOCAL | USE)) {
1284 char buf[256];
1285 if (cur & DEF_LOCAL)
1286 PyOS_snprintf(buf, sizeof(buf),
1287 NONLOCAL_AFTER_ASSIGN,
1288 c_name);
1289 else
1290 PyOS_snprintf(buf, sizeof(buf),
1291 NONLOCAL_AFTER_USE,
1292 c_name);
1293 if (!symtable_warn(st, buf, s->lineno))
1294 return 0;
1295 }
1296 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1297 return 0;
1298 }
1299 break;
1300 }
1301 case Expr_kind:
1302 VISIT(st, expr, s->v.Expr.value);
1303 break;
1304 case Pass_kind:
1305 case Break_kind:
1306 case Continue_kind:
1307 /* nothing to do here */
1308 break;
1309 case With_kind:
1310 VISIT(st, expr, s->v.With.context_expr);
1311 if (s->v.With.optional_vars) {
1312 VISIT(st, expr, s->v.With.optional_vars);
1313 }
1314 VISIT_SEQ(st, stmt, s->v.With.body);
1315 break;
1316 }
1317 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318}
1319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321symtable_visit_expr(struct symtable *st, expr_ty e)
1322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 switch (e->kind) {
1324 case BoolOp_kind:
1325 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1326 break;
1327 case BinOp_kind:
1328 VISIT(st, expr, e->v.BinOp.left);
1329 VISIT(st, expr, e->v.BinOp.right);
1330 break;
1331 case UnaryOp_kind:
1332 VISIT(st, expr, e->v.UnaryOp.operand);
1333 break;
1334 case Lambda_kind: {
1335 if (!GET_IDENTIFIER(lambda))
1336 return 0;
1337 if (e->v.Lambda.args->defaults)
1338 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001339 if (e->v.Lambda.args->kw_defaults)
1340 VISIT_KWONLYDEFAULTS(st,
1341 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001343 FunctionBlock, (void *)e, e->lineno,
1344 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 return 0;
1346 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1347 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1348 if (!symtable_exit_block(st, (void *)e))
1349 return 0;
1350 break;
1351 }
1352 case IfExp_kind:
1353 VISIT(st, expr, e->v.IfExp.test);
1354 VISIT(st, expr, e->v.IfExp.body);
1355 VISIT(st, expr, e->v.IfExp.orelse);
1356 break;
1357 case Dict_kind:
1358 VISIT_SEQ(st, expr, e->v.Dict.keys);
1359 VISIT_SEQ(st, expr, e->v.Dict.values);
1360 break;
1361 case Set_kind:
1362 VISIT_SEQ(st, expr, e->v.Set.elts);
1363 break;
1364 case GeneratorExp_kind:
1365 if (!symtable_visit_genexp(st, e))
1366 return 0;
1367 break;
1368 case ListComp_kind:
1369 if (!symtable_visit_listcomp(st, e))
1370 return 0;
1371 break;
1372 case SetComp_kind:
1373 if (!symtable_visit_setcomp(st, e))
1374 return 0;
1375 break;
1376 case DictComp_kind:
1377 if (!symtable_visit_dictcomp(st, e))
1378 return 0;
1379 break;
1380 case Yield_kind:
1381 if (e->v.Yield.value)
1382 VISIT(st, expr, e->v.Yield.value);
1383 st->st_cur->ste_generator = 1;
1384 if (st->st_cur->ste_returns_value) {
1385 PyErr_SetString(PyExc_SyntaxError,
1386 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001387 PyErr_SyntaxLocationEx(st->st_filename,
1388 e->lineno, e->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 return 0;
1390 }
1391 break;
1392 case Compare_kind:
1393 VISIT(st, expr, e->v.Compare.left);
1394 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1395 break;
1396 case Call_kind:
1397 VISIT(st, expr, e->v.Call.func);
1398 VISIT_SEQ(st, expr, e->v.Call.args);
1399 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1400 if (e->v.Call.starargs)
1401 VISIT(st, expr, e->v.Call.starargs);
1402 if (e->v.Call.kwargs)
1403 VISIT(st, expr, e->v.Call.kwargs);
1404 break;
1405 case Num_kind:
1406 case Str_kind:
1407 case Bytes_kind:
1408 case Ellipsis_kind:
1409 /* Nothing to do here. */
1410 break;
1411 /* The following exprs can be assignment targets. */
1412 case Attribute_kind:
1413 VISIT(st, expr, e->v.Attribute.value);
1414 break;
1415 case Subscript_kind:
1416 VISIT(st, expr, e->v.Subscript.value);
1417 VISIT(st, slice, e->v.Subscript.slice);
1418 break;
1419 case Starred_kind:
1420 VISIT(st, expr, e->v.Starred.value);
1421 break;
1422 case Name_kind:
1423 if (!symtable_add_def(st, e->v.Name.id,
1424 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1425 return 0;
1426 /* Special-case super: it counts as a use of __class__ */
1427 if (e->v.Name.ctx == Load &&
1428 st->st_cur->ste_type == FunctionBlock &&
1429 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1430 if (!GET_IDENTIFIER(__class__) ||
1431 !symtable_add_def(st, __class__, USE))
1432 return 0;
1433 }
1434 break;
1435 /* child nodes of List and Tuple will have expr_context set */
1436 case List_kind:
1437 VISIT_SEQ(st, expr, e->v.List.elts);
1438 break;
1439 case Tuple_kind:
1440 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1441 break;
1442 }
1443 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444}
1445
1446static int
1447symtable_implicit_arg(struct symtable *st, int pos)
1448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1450 if (id == NULL)
1451 return 0;
1452 if (!symtable_add_def(st, id, DEF_PARAM)) {
1453 Py_DECREF(id);
1454 return 0;
1455 }
1456 Py_DECREF(id);
1457 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458}
1459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001461symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 if (!args)
1466 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 for (i = 0; i < asdl_seq_LEN(args); i++) {
1469 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1470 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1471 return 0;
1472 }
1473
1474 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475}
1476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001478symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 if (!args)
1483 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 for (i = 0; i < asdl_seq_LEN(args); i++) {
1486 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1487 if (arg->annotation)
1488 VISIT(st, expr, arg->annotation);
1489 }
1490
1491 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001492}
1493
Neal Norwitzc1505362006-12-28 06:47:50 +00001494static int
1495symtable_visit_annotations(struct symtable *st, stmt_ty s)
1496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 arguments_ty a = s->v.FunctionDef.args;
1498
1499 if (a->args && !symtable_visit_argannotations(st, a->args))
1500 return 0;
1501 if (a->varargannotation)
1502 VISIT(st, expr, a->varargannotation);
1503 if (a->kwargannotation)
1504 VISIT(st, expr, a->kwargannotation);
1505 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1506 return 0;
1507 if (s->v.FunctionDef.returns)
1508 VISIT(st, expr, s->v.FunctionDef.returns);
1509 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510}
1511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513symtable_visit_arguments(struct symtable *st, arguments_ty a)
1514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 /* skip default arguments inside function block
1516 XXX should ast be different?
1517 */
1518 if (a->args && !symtable_visit_params(st, a->args))
1519 return 0;
1520 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1521 return 0;
1522 if (a->vararg) {
1523 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1524 return 0;
1525 st->st_cur->ste_varargs = 1;
1526 }
1527 if (a->kwarg) {
1528 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1529 return 0;
1530 st->st_cur->ste_varkeywords = 1;
1531 }
1532 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533}
1534
1535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1538{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 if (eh->v.ExceptHandler.type)
1540 VISIT(st, expr, eh->v.ExceptHandler.type);
1541 if (eh->v.ExceptHandler.name)
1542 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1543 return 0;
1544 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1545 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546}
1547
1548
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;
1558 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1559 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
1560 if (dot) {
1561 store_name = PyUnicode_FromUnicode(base, dot - base);
1562 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 }
1658 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1659 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1660 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1661 generators, 1, (void*)e);
1662 if (value)
1663 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1664 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1665 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001669symtable_visit_genexp(struct symtable *st, expr_ty e)
1670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1672 e->v.GeneratorExp.generators,
1673 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001674}
1675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001677symtable_visit_listcomp(struct symtable *st, expr_ty e)
1678{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1680 e->v.ListComp.generators,
1681 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001682}
1683
1684static int
1685symtable_visit_setcomp(struct symtable *st, expr_ty e)
1686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1688 e->v.SetComp.generators,
1689 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001690}
1691
1692static int
1693symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1696 e->v.DictComp.generators,
1697 e->v.DictComp.key,
1698 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001699}