blob: 00b342761f0c302f4d966a42b03a1645ec3d860b [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);
37 if (ste == NULL)
38 goto fail;
39 ste->ste_table = st;
40 ste->ste_id = k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 ste->ste_name = name;
43 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 ste->ste_symbols = NULL;
46 ste->ste_varnames = NULL;
47 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 ste->ste_symbols = PyDict_New();
50 if (ste->ste_symbols == NULL)
51 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 ste->ste_varnames = PyList_New(0);
54 if (ste->ste_varnames == NULL)
55 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 ste->ste_children = PyList_New(0);
58 if (ste->ste_children == NULL)
59 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 ste->ste_type = block;
62 ste->ste_unoptimized = 0;
63 ste->ste_nested = 0;
64 ste->ste_free = 0;
65 ste->ste_varargs = 0;
66 ste->ste_varkeywords = 0;
67 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000068 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000069 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000071 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (st->st_cur != NULL &&
74 (st->st_cur->ste_nested ||
75 st->st_cur->ste_type == FunctionBlock))
76 ste->ste_nested = 1;
77 ste->ste_child_free = 0;
78 ste->ste_generator = 0;
79 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
82 goto fail;
83
84 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085 fail:
Christian Heimes837e53a2012-09-10 03:08:46 +020086 Py_XDECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 Py_XDECREF(ste);
88 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000089}
90
91static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
95 ste->ste_name,
96 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097}
98
99static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 ste->ste_table = NULL;
103 Py_XDECREF(ste->ste_id);
104 Py_XDECREF(ste->ste_name);
105 Py_XDECREF(ste->ste_symbols);
106 Py_XDECREF(ste->ste_varnames);
107 Py_XDECREF(ste->ste_children);
108 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109}
110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000112
Guido van Rossum6f799372001-09-20 20:46:19 +0000113static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 {"id", T_OBJECT, OFF(ste_id), READONLY},
115 {"name", T_OBJECT, OFF(ste_name), READONLY},
116 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
117 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
118 {"children", T_OBJECT, OFF(ste_children), READONLY},
119 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
120 {"nested", T_INT, OFF(ste_nested), READONLY},
121 {"type", T_INT, OFF(ste_type), READONLY},
122 {"lineno", T_INT, OFF(ste_lineno), READONLY},
123 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124};
125
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000126PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 PyVarObject_HEAD_INIT(&PyType_Type, 0)
128 "symtable entry",
129 sizeof(PySTEntryObject),
130 0,
131 (destructor)ste_dealloc, /* tp_dealloc */
132 0, /* tp_print */
133 0, /* tp_getattr */
134 0, /* tp_setattr */
135 0, /* tp_reserved */
136 (reprfunc)ste_repr, /* tp_repr */
137 0, /* tp_as_number */
138 0, /* tp_as_sequence */
139 0, /* tp_as_mapping */
140 0, /* tp_hash */
141 0, /* tp_call */
142 0, /* tp_str */
143 PyObject_GenericGetAttr, /* tp_getattro */
144 0, /* tp_setattro */
145 0, /* tp_as_buffer */
146 Py_TPFLAGS_DEFAULT, /* tp_flags */
147 0, /* tp_doc */
148 0, /* tp_traverse */
149 0, /* tp_clear */
150 0, /* tp_richcompare */
151 0, /* tp_weaklistoffset */
152 0, /* tp_iter */
153 0, /* tp_iternext */
154 0, /* tp_methods */
155 ste_memberlist, /* tp_members */
156 0, /* tp_getset */
157 0, /* tp_base */
158 0, /* tp_dict */
159 0, /* tp_descr_get */
160 0, /* tp_descr_set */
161 0, /* tp_dictoffset */
162 0, /* tp_init */
163 0, /* tp_alloc */
164 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000165};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166
167static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000168static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000170 _Py_block_ty block, void *ast, int lineno,
171 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000172static int symtable_exit_block(struct symtable *st, void *ast);
173static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
174static int symtable_visit_expr(struct symtable *st, expr_ty s);
175static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000176static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
177static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000178static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179static int symtable_visit_arguments(struct symtable *st, arguments_ty);
180static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
181static int symtable_visit_alias(struct symtable *st, alias_ty);
182static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
183static int symtable_visit_keyword(struct symtable *st, keyword_ty);
184static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000185static int symtable_visit_params(struct symtable *st, asdl_seq *args);
186static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000188static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189
190
Nick Coghlan650f0d02007-04-15 12:05:43 +0000191static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
193 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194
195#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
198#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000199"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
201static struct symtable *
202symtable_new(void)
203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
207 if (st == NULL)
208 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 st->st_filename = NULL;
211 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if ((st->st_stack = PyList_New(0)) == NULL)
214 goto fail;
215 if ((st->st_blocks = PyDict_New()) == NULL)
216 goto fail;
217 st->st_cur = NULL;
218 st->st_private = NULL;
219 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 PySymtable_Free(st);
222 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223}
224
225struct symtable *
226PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 struct symtable *st = symtable_new();
229 asdl_seq *seq;
230 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (st == NULL)
233 return st;
234 st->st_filename = filename;
235 st->st_future = future;
236 /* Make the initial symbol information gathering pass */
237 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000238 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 PySymtable_Free(st);
240 return NULL;
241 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 st->st_top = st->st_cur;
244 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
245 switch (mod->kind) {
246 case Module_kind:
247 seq = mod->v.Module.body;
248 for (i = 0; i < asdl_seq_LEN(seq); i++)
249 if (!symtable_visit_stmt(st,
250 (stmt_ty)asdl_seq_GET(seq, i)))
251 goto error;
252 break;
253 case Expression_kind:
254 if (!symtable_visit_expr(st, mod->v.Expression.body))
255 goto error;
256 break;
257 case Interactive_kind:
258 seq = mod->v.Interactive.body;
259 for (i = 0; i < asdl_seq_LEN(seq); i++)
260 if (!symtable_visit_stmt(st,
261 (stmt_ty)asdl_seq_GET(seq, i)))
262 goto error;
263 break;
264 case Suite_kind:
265 PyErr_SetString(PyExc_RuntimeError,
266 "this compiler does not handle Suites");
267 goto error;
268 }
269 if (!symtable_exit_block(st, (void *)mod)) {
270 PySymtable_Free(st);
271 return NULL;
272 }
273 /* Make the second symbol analysis pass */
274 if (symtable_analyze(st))
275 return st;
276 PySymtable_Free(st);
277 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000278 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 (void) symtable_exit_block(st, (void *)mod);
280 PySymtable_Free(st);
281 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000282}
283
284void
285PySymtable_Free(struct symtable *st)
286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 Py_XDECREF(st->st_blocks);
288 Py_XDECREF(st->st_stack);
289 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290}
291
292PySTEntryObject *
293PySymtable_Lookup(struct symtable *st, void *key)
294{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 k = PyLong_FromVoidPtr(key);
298 if (k == NULL)
299 return NULL;
300 v = PyDict_GetItem(st->st_blocks, k);
301 if (v) {
302 assert(PySTEntry_Check(v));
303 Py_INCREF(v);
304 }
305 else {
306 PyErr_SetString(PyExc_KeyError,
307 "unknown symbol table entry");
308 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 Py_DECREF(k);
311 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312}
313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315PyST_GetScope(PySTEntryObject *ste, PyObject *name)
316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
318 if (!v)
319 return 0;
320 assert(PyLong_Check(v));
321 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322}
323
324
325/* Analyze raw symbol information to determine scope of each name.
326
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000327 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000333 explicit global is declared with the global statement. An implicit
334 global is a free variable for which the compiler has found no binding
335 in an enclosing function scope. The implicit global is either a global
336 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
337 to handle these names to implement slightly odd semantics. In such a
338 block, the name is treated as global until it is assigned to; then it
339 is treated as a local.
340
341 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000342 The first pass collects raw facts from the AST via the symtable_visit_*
343 functions: the name is a parameter here, the name is used but not defined
344 here, etc. The second pass analyzes these facts during a pass over the
345 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346
347 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000349 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000350 Names which are explicitly declared nonlocal must exist in this set of
351 visible names - if they do not, a syntax error is raised. After doing
352 the local analysis, it analyzes each of its child blocks using an
353 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354
Nick Coghlan650f0d02007-04-15 12:05:43 +0000355 The children update the free variable set. If a local variable is added to
356 the free variable set by the child, the variable is marked as a cell. The
357 function object being defined must provide runtime storage for the variable
358 that may outlive the function's frame. Cell variables are removed from the
359 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000360
Nick Coghlan650f0d02007-04-15 12:05:43 +0000361 During analysis, the names are:
362 symbols: dict mapping from symbol names to flag values (including offset scope values)
363 scopes: dict mapping from symbol names to scope values (no offset)
364 local: set of all symbol names local to the current scope
365 bound: set of all symbol names local to a containing function scope
366 free: set of all symbol names referenced but not bound in child scopes
367 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368*/
369
370#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 PyObject *o = PyLong_FromLong(I); \
372 if (!o) \
373 return 0; \
374 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
375 Py_DECREF(o); \
376 return 0; \
377 } \
378 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379}
380
381/* Decide on scope of name, given flags.
382
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000383 The namespace dictionaries may be modified to record information
384 about the new name. For example, a new global will add an entry to
385 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386*/
387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000389analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 PyObject *bound, PyObject *local, PyObject *free,
391 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 if (flags & DEF_GLOBAL) {
394 if (flags & DEF_PARAM) {
395 PyErr_Format(PyExc_SyntaxError,
396 "name '%U' is parameter and global",
397 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000398 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
399 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400
401 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000402 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (flags & DEF_NONLOCAL) {
404 PyErr_Format(PyExc_SyntaxError,
405 "name '%U' is nonlocal and global",
406 name);
407 return 0;
408 }
409 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
410 if (PySet_Add(global, name) < 0)
411 return 0;
412 if (bound && (PySet_Discard(bound, name) < 0))
413 return 0;
414 return 1;
415 }
416 if (flags & DEF_NONLOCAL) {
417 if (flags & DEF_PARAM) {
418 PyErr_Format(PyExc_SyntaxError,
419 "name '%U' is parameter and nonlocal",
420 name);
421 return 0;
422 }
423 if (!bound) {
424 PyErr_Format(PyExc_SyntaxError,
425 "nonlocal declaration not allowed at module level");
426 return 0;
427 }
428 if (!PySet_Contains(bound, name)) {
429 PyErr_Format(PyExc_SyntaxError,
430 "no binding for nonlocal '%U' found",
431 name);
432
433 return 0;
434 }
435 SET_SCOPE(scopes, name, FREE);
436 ste->ste_free = 1;
437 return PySet_Add(free, name) >= 0;
438 }
439 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000440 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (PySet_Add(local, name) < 0)
442 return 0;
443 if (PySet_Discard(global, name) < 0)
444 return 0;
445 return 1;
446 }
447 /* If an enclosing block has a binding for this name, it
448 is a free variable rather than a global variable.
449 Note that having a non-NULL bound implies that the block
450 is nested.
451 */
452 if (bound && PySet_Contains(bound, name)) {
453 SET_SCOPE(scopes, name, FREE);
454 ste->ste_free = 1;
455 return PySet_Add(free, name) >= 0;
456 }
457 /* If a parent has a global statement, then call it global
458 explicit? It could also be global implicit.
459 */
460 if (global && PySet_Contains(global, name)) {
461 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
462 return 1;
463 }
464 if (ste->ste_nested)
465 ste->ste_free = 1;
466 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
467 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468}
469
470#undef SET_SCOPE
471
472/* If a name is defined in free and also in locals, then this block
473 provides the binding for the free variable. The name should be
474 marked CELL in this block and removed from the free list.
475
476 Note that the current block's free variables are included in free.
477 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000478
Martin v. Löwis2673a572007-10-29 19:54:24 +0000479 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000480 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481*/
482
483static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000484analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 PyObject *name, *v, *v_cell;
487 int success = 0;
488 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 v_cell = PyLong_FromLong(CELL);
491 if (!v_cell)
492 return 0;
493 while (PyDict_Next(scopes, &pos, &name, &v)) {
494 long scope;
495 assert(PyLong_Check(v));
496 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000497 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 continue;
499 if (!PySet_Contains(free, name))
500 continue;
501 if (restricted != NULL &&
502 PyUnicode_CompareWithASCIIString(name, restricted))
503 continue;
504 /* Replace LOCAL with CELL for this name, and remove
505 from free. It is safe to replace the value of name
506 in the dict, because it will not cause a resize.
507 */
508 if (PyDict_SetItem(scopes, name, v_cell) < 0)
509 goto error;
510 if (PySet_Discard(free, name) < 0)
511 goto error;
512 }
513 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 Py_DECREF(v_cell);
516 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517}
518
519/* Check for illegal statements in unoptimized namespaces */
520static int
521check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
525 || !(ste->ste_free || ste->ste_child_free))
526 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 trailer = (ste->ste_child_free ?
529 "contains a nested function with free variables" :
530 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 switch (ste->ste_unoptimized) {
533 case OPT_TOPLEVEL: /* import * at top-level is fine */
534 return 1;
535 case OPT_IMPORT_STAR:
536 PyErr_Format(PyExc_SyntaxError,
537 "import * is not allowed in function '%U' because it %s",
538 ste->ste_name, trailer);
539 break;
540 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000542 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
543 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545}
546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547/* Enter the final scope information into the ste_symbols dict.
548 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000549 * All arguments are dicts. Modifies symbols, others are read-only.
550*/
551static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000553 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 PyObject *name = NULL, *itr = NULL;
556 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
557 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* Update scope information for all symbols in this scope */
560 while (PyDict_Next(symbols, &pos, &name, &v)) {
561 long scope, flags;
562 assert(PyLong_Check(v));
563 flags = PyLong_AS_LONG(v);
564 v_scope = PyDict_GetItem(scopes, name);
565 assert(v_scope && PyLong_Check(v_scope));
566 scope = PyLong_AS_LONG(v_scope);
567 flags |= (scope << SCOPE_OFFSET);
568 v_new = PyLong_FromLong(flags);
569 if (!v_new)
570 return 0;
571 if (PyDict_SetItem(symbols, name, v_new) < 0) {
572 Py_DECREF(v_new);
573 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 Py_DECREF(v_new);
576 }
577
578 /* Record not yet resolved free variables from children (if any) */
579 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
580 if (!v_free)
581 return 0;
582
583 itr = PyObject_GetIter(free);
584 if (!itr)
585 goto error;
586
587 while ((name = PyIter_Next(itr))) {
588 v = PyDict_GetItem(symbols, name);
589
590 /* Handle symbol that already exists in this scope */
591 if (v) {
592 /* Handle a free variable in a method of
593 the class that has the same name as a local
594 or global in the class scope.
595 */
596 if (classflag &&
597 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
598 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
599 v_new = PyLong_FromLong(flags);
600 if (!v_new) {
601 goto error;
602 }
603 if (PyDict_SetItem(symbols, name, v_new) < 0) {
604 Py_DECREF(v_new);
605 goto error;
606 }
607 Py_DECREF(v_new);
608 }
609 /* It's a cell, or already free in this scope */
610 Py_DECREF(name);
611 continue;
612 }
613 /* Handle global symbol */
614 if (!PySet_Contains(bound, name)) {
615 Py_DECREF(name);
616 continue; /* it's a global */
617 }
618 /* Propagate new free symbol up the lexical stack */
619 if (PyDict_SetItem(symbols, name, v_free) < 0) {
620 goto error;
621 }
622 Py_DECREF(name);
623 }
624 Py_DECREF(itr);
625 Py_DECREF(v_free);
626 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000627error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 Py_XDECREF(v_free);
629 Py_XDECREF(itr);
630 Py_XDECREF(name);
631 return 0;
632}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633
634/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000635
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 Arguments:
637 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000638 bound -- set of variables bound in enclosing scopes (input). bound
639 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640 free -- set of free variables in enclosed scopes (output)
641 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000642
643 The implementation uses two mutually recursive functions,
644 analyze_block() and analyze_child_block(). analyze_block() is
645 responsible for analyzing the individual names defined in a block.
646 analyze_child_block() prepares temporary namespace dictionaries
647 used to evaluated nested blocks.
648
649 The two functions exist because a child block should see the name
650 bindings of its enclosing blocks, but those bindings should not
651 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652*/
653
654static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
656 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000657
658static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
660 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
663 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
664 PyObject *temp;
665 int i, success = 0;
666 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 local = PySet_New(NULL); /* collect new names bound in block */
669 if (!local)
670 goto error;
671 scopes = PyDict_New(); /* collect scopes defined for each name */
672 if (!scopes)
673 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 /* Allocate new global and bound variable dictionaries. These
676 dictionaries hold the names visible in nested blocks. For
677 ClassBlocks, the bound and global names are initialized
678 before analyzing names, because class bindings aren't
679 visible in methods. For other blocks, they are initialized
680 after names are analyzed.
681 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 /* TODO(jhylton): Package these dicts in a struct so that we
684 can write reasonable helper functions?
685 */
686 newglobal = PySet_New(NULL);
687 if (!newglobal)
688 goto error;
689 newfree = PySet_New(NULL);
690 if (!newfree)
691 goto error;
692 newbound = PySet_New(NULL);
693 if (!newbound)
694 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 /* Class namespace has no effect on names visible in
697 nested functions, so populate the global and bound
698 sets to be passed to child blocks before analyzing
699 this one.
700 */
701 if (ste->ste_type == ClassBlock) {
702 /* Pass down known globals */
703 temp = PyNumber_InPlaceOr(newglobal, global);
704 if (!temp)
705 goto error;
706 Py_DECREF(temp);
707 /* Pass down previously bound symbols */
708 if (bound) {
709 temp = PyNumber_InPlaceOr(newbound, bound);
710 if (!temp)
711 goto error;
712 Py_DECREF(temp);
713 }
714 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
717 long flags = PyLong_AS_LONG(v);
718 if (!analyze_name(ste, scopes, name, flags,
719 bound, local, free, global))
720 goto error;
721 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 /* Populate global and bound sets to be passed to children. */
724 if (ste->ste_type != ClassBlock) {
725 /* Add function locals to bound set */
726 if (ste->ste_type == FunctionBlock) {
727 temp = PyNumber_InPlaceOr(newbound, local);
728 if (!temp)
729 goto error;
730 Py_DECREF(temp);
731 }
732 /* Pass down previously bound symbols */
733 if (bound) {
734 temp = PyNumber_InPlaceOr(newbound, bound);
735 if (!temp)
736 goto error;
737 Py_DECREF(temp);
738 }
739 /* Pass down known globals */
740 temp = PyNumber_InPlaceOr(newglobal, global);
741 if (!temp)
742 goto error;
743 Py_DECREF(temp);
744 }
745 else {
746 /* Special-case __class__ */
747 if (!GET_IDENTIFIER(__class__))
748 goto error;
749 assert(PySet_Contains(local, __class__) == 1);
750 if (PySet_Add(newbound, __class__) < 0)
751 goto error;
752 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 /* Recursively call analyze_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 newbound, newglobal now contain the names visible in
757 nested blocks. The free variables in the children will
758 be collected in allfree.
759 */
760 allfree = PySet_New(NULL);
761 if (!allfree)
762 goto error;
763 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
764 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
765 PySTEntryObject* entry;
766 assert(c && PySTEntry_Check(c));
767 entry = (PySTEntryObject*)c;
768 if (!analyze_child_block(entry, newbound, newfree, newglobal,
769 allfree))
770 goto error;
771 /* Check if any children have free variables */
772 if (entry->ste_free || entry->ste_child_free)
773 ste->ste_child_free = 1;
774 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 temp = PyNumber_InPlaceOr(newfree, allfree);
777 if (!temp)
778 goto error;
779 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 /* Check if any local variables must be converted to cell variables */
782 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
783 NULL))
784 goto error;
785 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
786 "__class__"))
787 goto error;
788 /* Records the results of the analysis in the symbol table entry */
789 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
790 ste->ste_type == ClassBlock))
791 goto error;
792 if (!check_unoptimized(ste))
793 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 temp = PyNumber_InPlaceOr(free, newfree);
796 if (!temp)
797 goto error;
798 Py_DECREF(temp);
799 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 Py_XDECREF(scopes);
802 Py_XDECREF(local);
803 Py_XDECREF(newbound);
804 Py_XDECREF(newglobal);
805 Py_XDECREF(newfree);
806 Py_XDECREF(allfree);
807 if (!success)
808 assert(PyErr_Occurred());
809 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810}
811
812static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
814 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
817 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 These dictionary are used by all blocks enclosed by the
822 current block. The analyze_block() call modifies these
823 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 */
826 temp_bound = PySet_New(bound);
827 if (!temp_bound)
828 goto error;
829 temp_free = PySet_New(free);
830 if (!temp_free)
831 goto error;
832 temp_global = PySet_New(global);
833 if (!temp_global)
834 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
837 goto error;
838 temp = PyNumber_InPlaceOr(child_free, temp_free);
839 if (!temp)
840 goto error;
841 Py_DECREF(temp);
842 Py_DECREF(temp_bound);
843 Py_DECREF(temp_free);
844 Py_DECREF(temp_global);
845 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000846 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 Py_XDECREF(temp_bound);
848 Py_XDECREF(temp_free);
849 Py_XDECREF(temp_global);
850 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000851}
852
853static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854symtable_analyze(struct symtable *st)
855{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 PyObject *free, *global;
857 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 free = PySet_New(NULL);
860 if (!free)
861 return 0;
862 global = PySet_New(NULL);
863 if (!global) {
864 Py_DECREF(free);
865 return 0;
866 }
867 r = analyze_block(st->st_top, NULL, free, global);
868 Py_DECREF(free);
869 Py_DECREF(global);
870 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871}
872
873
874static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000875symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
878 lineno, NULL, NULL) < 0) {
879 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
880 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000881 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
882 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 }
884 return 0;
885 }
886 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887}
888
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000889/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890 This reference is released when the block is exited, via the DECREF
891 in symtable_exit_block().
892*/
893
894static int
895symtable_exit_block(struct symtable *st, void *ast)
896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 Py_CLEAR(st->st_cur);
900 end = PyList_GET_SIZE(st->st_stack) - 1;
901 if (end >= 0) {
902 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
903 end);
904 if (st->st_cur == NULL)
905 return 0;
906 Py_INCREF(st->st_cur);
907 if (PySequence_DelItem(st->st_stack, end) < 0)
908 return 0;
909 }
910 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911}
912
913static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000915 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 if (st->st_cur) {
920 prev = st->st_cur;
921 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
922 return 0;
923 }
924 Py_DECREF(st->st_cur);
925 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000926 st->st_cur = ste_new(st, name, block, ast, lineno, col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 if (st->st_cur == NULL)
928 return 0;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000929 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 st->st_global = st->st_cur->ste_symbols;
931 if (prev) {
932 if (PyList_Append(prev->ste_children,
933 (PyObject *)st->st_cur) < 0) {
934 return 0;
935 }
936 }
937 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938}
939
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000940static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941symtable_lookup(struct symtable *st, PyObject *name)
942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 PyObject *o;
944 PyObject *mangled = _Py_Mangle(st->st_private, name);
945 if (!mangled)
946 return 0;
947 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
948 Py_DECREF(mangled);
949 if (!o)
950 return 0;
951 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952}
953
954static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 PyObject *o;
958 PyObject *dict;
959 long val;
960 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961
Jeremy Hylton81e95022007-02-27 06:50:52 +0000962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 if (!mangled)
964 return 0;
965 dict = st->st_cur->ste_symbols;
966 if ((o = PyDict_GetItem(dict, mangled))) {
967 val = PyLong_AS_LONG(o);
968 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
969 /* Is it better to use 'mangled' or 'name' here? */
970 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000971 PyErr_SyntaxLocationEx(st->st_filename,
972 st->st_cur->ste_lineno,
973 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 goto error;
975 }
976 val |= flag;
977 } else
978 val = flag;
979 o = PyLong_FromLong(val);
980 if (o == NULL)
981 goto error;
982 if (PyDict_SetItem(dict, mangled, o) < 0) {
983 Py_DECREF(o);
984 goto error;
985 }
986 Py_DECREF(o);
987
988 if (flag & DEF_PARAM) {
989 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
990 goto error;
991 } else if (flag & DEF_GLOBAL) {
992 /* XXX need to update DEF_GLOBAL for other flags too;
993 perhaps only DEF_FREE_GLOBAL */
994 val = flag;
995 if ((o = PyDict_GetItem(st->st_global, mangled))) {
996 val |= PyLong_AS_LONG(o);
997 }
998 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 goto error;
1001 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1002 Py_DECREF(o);
1003 goto error;
1004 }
1005 Py_DECREF(o);
1006 }
1007 Py_DECREF(mangled);
1008 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001009
1010error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 Py_DECREF(mangled);
1012 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013}
1014
1015/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1016 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 function.
1018
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1020 useful if the first node in the sequence requires special treatment.
1021*/
1022
1023#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 if (!symtable_visit_ ## TYPE((ST), (V))) \
1025 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001026
1027#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1029 symtable_exit_block((ST), (S)); \
1030 return 0; \
1031 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 int i; \
1035 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1036 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1037 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1038 if (!symtable_visit_ ## TYPE((ST), elt)) \
1039 return 0; \
1040 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001042
1043#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 int i; \
1045 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1046 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1047 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1048 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1049 symtable_exit_block((ST), (S)); \
1050 return 0; \
1051 } \
1052 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001053}
1054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 int i; \
1057 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1058 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1059 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1060 if (!symtable_visit_ ## TYPE((ST), elt)) \
1061 return 0; \
1062 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001064
1065#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 int i; \
1067 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1068 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1069 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1070 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1071 symtable_exit_block((ST), (S)); \
1072 return 0; \
1073 } \
1074 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001075}
1076
Guido van Rossum4f72a782006-10-27 23:31:49 +00001077#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 int i = 0; \
1079 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1080 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1081 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1082 if (!elt) continue; /* can be NULL */ \
1083 if (!symtable_visit_expr((ST), elt)) \
1084 return 0; \
1085 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001086}
1087
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001089symtable_new_tmpname(struct symtable *st)
1090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 char tmpname[256];
1092 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1095 ++st->st_cur->ste_tmpname);
1096 tmp = PyUnicode_InternFromString(tmpname);
1097 if (!tmp)
1098 return 0;
1099 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1100 return 0;
1101 Py_DECREF(tmp);
1102 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001103}
1104
Guido van Rossum4f72a782006-10-27 23:31:49 +00001105
Guido van Rossumc2e20742006-02-27 22:32:47 +00001106static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107symtable_visit_stmt(struct symtable *st, stmt_ty s)
1108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 switch (s->kind) {
1110 case FunctionDef_kind:
1111 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1112 return 0;
1113 if (s->v.FunctionDef.args->defaults)
1114 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1115 if (s->v.FunctionDef.args->kw_defaults)
1116 VISIT_KWONLYDEFAULTS(st,
1117 s->v.FunctionDef.args->kw_defaults);
1118 if (!symtable_visit_annotations(st, s))
1119 return 0;
1120 if (s->v.FunctionDef.decorator_list)
1121 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1122 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001123 FunctionBlock, (void *)s, s->lineno,
1124 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 return 0;
1126 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1127 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1128 if (!symtable_exit_block(st, s))
1129 return 0;
1130 break;
1131 case ClassDef_kind: {
1132 PyObject *tmp;
1133 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1134 return 0;
1135 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1136 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1137 if (s->v.ClassDef.starargs)
1138 VISIT(st, expr, s->v.ClassDef.starargs);
1139 if (s->v.ClassDef.kwargs)
1140 VISIT(st, expr, s->v.ClassDef.kwargs);
1141 if (s->v.ClassDef.decorator_list)
1142 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1143 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001144 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 return 0;
1146 if (!GET_IDENTIFIER(__class__) ||
1147 !symtable_add_def(st, __class__, DEF_LOCAL) ||
1148 !GET_IDENTIFIER(__locals__) ||
1149 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1150 symtable_exit_block(st, s);
1151 return 0;
1152 }
1153 tmp = st->st_private;
1154 st->st_private = s->v.ClassDef.name;
1155 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1156 st->st_private = tmp;
1157 if (!symtable_exit_block(st, s))
1158 return 0;
1159 break;
1160 }
1161 case Return_kind:
1162 if (s->v.Return.value) {
1163 VISIT(st, expr, s->v.Return.value);
1164 st->st_cur->ste_returns_value = 1;
1165 if (st->st_cur->ste_generator) {
1166 PyErr_SetString(PyExc_SyntaxError,
1167 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001168 PyErr_SyntaxLocationEx(st->st_filename,
1169 s->lineno,
1170 s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00001172 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 }
1174 break;
1175 case Delete_kind:
1176 VISIT_SEQ(st, expr, s->v.Delete.targets);
1177 break;
1178 case Assign_kind:
1179 VISIT_SEQ(st, expr, s->v.Assign.targets);
1180 VISIT(st, expr, s->v.Assign.value);
1181 break;
1182 case AugAssign_kind:
1183 VISIT(st, expr, s->v.AugAssign.target);
1184 VISIT(st, expr, s->v.AugAssign.value);
1185 break;
1186 case For_kind:
1187 VISIT(st, expr, s->v.For.target);
1188 VISIT(st, expr, s->v.For.iter);
1189 VISIT_SEQ(st, stmt, s->v.For.body);
1190 if (s->v.For.orelse)
1191 VISIT_SEQ(st, stmt, s->v.For.orelse);
1192 break;
1193 case While_kind:
1194 VISIT(st, expr, s->v.While.test);
1195 VISIT_SEQ(st, stmt, s->v.While.body);
1196 if (s->v.While.orelse)
1197 VISIT_SEQ(st, stmt, s->v.While.orelse);
1198 break;
1199 case If_kind:
1200 /* XXX if 0: and lookup_yield() hacks */
1201 VISIT(st, expr, s->v.If.test);
1202 VISIT_SEQ(st, stmt, s->v.If.body);
1203 if (s->v.If.orelse)
1204 VISIT_SEQ(st, stmt, s->v.If.orelse);
1205 break;
1206 case Raise_kind:
1207 if (s->v.Raise.exc) {
1208 VISIT(st, expr, s->v.Raise.exc);
1209 if (s->v.Raise.cause) {
1210 VISIT(st, expr, s->v.Raise.cause);
1211 }
1212 }
1213 break;
1214 case TryExcept_kind:
1215 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1216 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1217 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1218 break;
1219 case TryFinally_kind:
1220 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1221 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1222 break;
1223 case Assert_kind:
1224 VISIT(st, expr, s->v.Assert.test);
1225 if (s->v.Assert.msg)
1226 VISIT(st, expr, s->v.Assert.msg);
1227 break;
1228 case Import_kind:
1229 VISIT_SEQ(st, alias, s->v.Import.names);
1230 /* XXX Don't have the lineno available inside
1231 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001232 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001234 st->st_cur->ste_opt_col_offset = s->col_offset;
1235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 break;
1237 case ImportFrom_kind:
1238 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1239 /* XXX Don't have the lineno available inside
1240 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001241 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001243 st->st_cur->ste_opt_col_offset = s->col_offset;
1244 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 break;
1246 case Global_kind: {
1247 int i;
1248 asdl_seq *seq = s->v.Global.names;
1249 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1250 identifier name = (identifier)asdl_seq_GET(seq, i);
1251 char *c_name = _PyUnicode_AsString(name);
1252 long cur = symtable_lookup(st, name);
1253 if (cur < 0)
1254 return 0;
1255 if (cur & (DEF_LOCAL | USE)) {
1256 char buf[256];
1257 if (cur & DEF_LOCAL)
1258 PyOS_snprintf(buf, sizeof(buf),
1259 GLOBAL_AFTER_ASSIGN,
1260 c_name);
1261 else
1262 PyOS_snprintf(buf, sizeof(buf),
1263 GLOBAL_AFTER_USE,
1264 c_name);
1265 if (!symtable_warn(st, buf, s->lineno))
1266 return 0;
1267 }
1268 if (!symtable_add_def(st, name, DEF_GLOBAL))
1269 return 0;
1270 }
1271 break;
1272 }
1273 case Nonlocal_kind: {
1274 int i;
1275 asdl_seq *seq = s->v.Nonlocal.names;
1276 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1277 identifier name = (identifier)asdl_seq_GET(seq, i);
1278 char *c_name = _PyUnicode_AsString(name);
1279 long cur = symtable_lookup(st, name);
1280 if (cur < 0)
1281 return 0;
1282 if (cur & (DEF_LOCAL | USE)) {
1283 char buf[256];
1284 if (cur & DEF_LOCAL)
1285 PyOS_snprintf(buf, sizeof(buf),
1286 NONLOCAL_AFTER_ASSIGN,
1287 c_name);
1288 else
1289 PyOS_snprintf(buf, sizeof(buf),
1290 NONLOCAL_AFTER_USE,
1291 c_name);
1292 if (!symtable_warn(st, buf, s->lineno))
1293 return 0;
1294 }
1295 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1296 return 0;
1297 }
1298 break;
1299 }
1300 case Expr_kind:
1301 VISIT(st, expr, s->v.Expr.value);
1302 break;
1303 case Pass_kind:
1304 case Break_kind:
1305 case Continue_kind:
1306 /* nothing to do here */
1307 break;
1308 case With_kind:
1309 VISIT(st, expr, s->v.With.context_expr);
1310 if (s->v.With.optional_vars) {
1311 VISIT(st, expr, s->v.With.optional_vars);
1312 }
1313 VISIT_SEQ(st, stmt, s->v.With.body);
1314 break;
1315 }
1316 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317}
1318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320symtable_visit_expr(struct symtable *st, expr_ty e)
1321{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 switch (e->kind) {
1323 case BoolOp_kind:
1324 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1325 break;
1326 case BinOp_kind:
1327 VISIT(st, expr, e->v.BinOp.left);
1328 VISIT(st, expr, e->v.BinOp.right);
1329 break;
1330 case UnaryOp_kind:
1331 VISIT(st, expr, e->v.UnaryOp.operand);
1332 break;
1333 case Lambda_kind: {
1334 if (!GET_IDENTIFIER(lambda))
1335 return 0;
1336 if (e->v.Lambda.args->defaults)
1337 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001338 if (e->v.Lambda.args->kw_defaults)
1339 VISIT_KWONLYDEFAULTS(st,
1340 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001342 FunctionBlock, (void *)e, e->lineno,
1343 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 return 0;
1345 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1346 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1347 if (!symtable_exit_block(st, (void *)e))
1348 return 0;
1349 break;
1350 }
1351 case IfExp_kind:
1352 VISIT(st, expr, e->v.IfExp.test);
1353 VISIT(st, expr, e->v.IfExp.body);
1354 VISIT(st, expr, e->v.IfExp.orelse);
1355 break;
1356 case Dict_kind:
1357 VISIT_SEQ(st, expr, e->v.Dict.keys);
1358 VISIT_SEQ(st, expr, e->v.Dict.values);
1359 break;
1360 case Set_kind:
1361 VISIT_SEQ(st, expr, e->v.Set.elts);
1362 break;
1363 case GeneratorExp_kind:
1364 if (!symtable_visit_genexp(st, e))
1365 return 0;
1366 break;
1367 case ListComp_kind:
1368 if (!symtable_visit_listcomp(st, e))
1369 return 0;
1370 break;
1371 case SetComp_kind:
1372 if (!symtable_visit_setcomp(st, e))
1373 return 0;
1374 break;
1375 case DictComp_kind:
1376 if (!symtable_visit_dictcomp(st, e))
1377 return 0;
1378 break;
1379 case Yield_kind:
1380 if (e->v.Yield.value)
1381 VISIT(st, expr, e->v.Yield.value);
1382 st->st_cur->ste_generator = 1;
1383 if (st->st_cur->ste_returns_value) {
1384 PyErr_SetString(PyExc_SyntaxError,
1385 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001386 PyErr_SyntaxLocationEx(st->st_filename,
1387 e->lineno, e->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 return 0;
1389 }
1390 break;
1391 case Compare_kind:
1392 VISIT(st, expr, e->v.Compare.left);
1393 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1394 break;
1395 case Call_kind:
1396 VISIT(st, expr, e->v.Call.func);
1397 VISIT_SEQ(st, expr, e->v.Call.args);
1398 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1399 if (e->v.Call.starargs)
1400 VISIT(st, expr, e->v.Call.starargs);
1401 if (e->v.Call.kwargs)
1402 VISIT(st, expr, e->v.Call.kwargs);
1403 break;
1404 case Num_kind:
1405 case Str_kind:
1406 case Bytes_kind:
1407 case Ellipsis_kind:
1408 /* Nothing to do here. */
1409 break;
1410 /* The following exprs can be assignment targets. */
1411 case Attribute_kind:
1412 VISIT(st, expr, e->v.Attribute.value);
1413 break;
1414 case Subscript_kind:
1415 VISIT(st, expr, e->v.Subscript.value);
1416 VISIT(st, slice, e->v.Subscript.slice);
1417 break;
1418 case Starred_kind:
1419 VISIT(st, expr, e->v.Starred.value);
1420 break;
1421 case Name_kind:
1422 if (!symtable_add_def(st, e->v.Name.id,
1423 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1424 return 0;
1425 /* Special-case super: it counts as a use of __class__ */
1426 if (e->v.Name.ctx == Load &&
1427 st->st_cur->ste_type == FunctionBlock &&
1428 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1429 if (!GET_IDENTIFIER(__class__) ||
1430 !symtable_add_def(st, __class__, USE))
1431 return 0;
1432 }
1433 break;
1434 /* child nodes of List and Tuple will have expr_context set */
1435 case List_kind:
1436 VISIT_SEQ(st, expr, e->v.List.elts);
1437 break;
1438 case Tuple_kind:
1439 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1440 break;
1441 }
1442 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443}
1444
1445static int
1446symtable_implicit_arg(struct symtable *st, int pos)
1447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1449 if (id == NULL)
1450 return 0;
1451 if (!symtable_add_def(st, id, DEF_PARAM)) {
1452 Py_DECREF(id);
1453 return 0;
1454 }
1455 Py_DECREF(id);
1456 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457}
1458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001460symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 if (!args)
1465 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 for (i = 0; i < asdl_seq_LEN(args); i++) {
1468 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1469 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1470 return 0;
1471 }
1472
1473 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474}
1475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001477symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 if (!args)
1482 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 for (i = 0; i < asdl_seq_LEN(args); i++) {
1485 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1486 if (arg->annotation)
1487 VISIT(st, expr, arg->annotation);
1488 }
1489
1490 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001491}
1492
Neal Norwitzc1505362006-12-28 06:47:50 +00001493static int
1494symtable_visit_annotations(struct symtable *st, stmt_ty s)
1495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 arguments_ty a = s->v.FunctionDef.args;
1497
1498 if (a->args && !symtable_visit_argannotations(st, a->args))
1499 return 0;
1500 if (a->varargannotation)
1501 VISIT(st, expr, a->varargannotation);
1502 if (a->kwargannotation)
1503 VISIT(st, expr, a->kwargannotation);
1504 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1505 return 0;
1506 if (s->v.FunctionDef.returns)
1507 VISIT(st, expr, s->v.FunctionDef.returns);
1508 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509}
1510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512symtable_visit_arguments(struct symtable *st, arguments_ty a)
1513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 /* skip default arguments inside function block
1515 XXX should ast be different?
1516 */
1517 if (a->args && !symtable_visit_params(st, a->args))
1518 return 0;
1519 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1520 return 0;
1521 if (a->vararg) {
1522 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1523 return 0;
1524 st->st_cur->ste_varargs = 1;
1525 }
1526 if (a->kwarg) {
1527 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1528 return 0;
1529 st->st_cur->ste_varkeywords = 1;
1530 }
1531 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532}
1533
1534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 if (eh->v.ExceptHandler.type)
1539 VISIT(st, expr, eh->v.ExceptHandler.type);
1540 if (eh->v.ExceptHandler.name)
1541 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1542 return 0;
1543 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1544 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545}
1546
1547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549symtable_visit_alias(struct symtable *st, alias_ty a)
1550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001552 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 dotted package name (e.g. spam.eggs)
1554 */
1555 PyObject *store_name;
1556 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1557 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1558 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
1559 if (dot) {
1560 store_name = PyUnicode_FromUnicode(base, dot - base);
1561 if (!store_name)
1562 return 0;
1563 }
1564 else {
1565 store_name = name;
1566 Py_INCREF(store_name);
1567 }
1568 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1569 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1570 Py_DECREF(store_name);
1571 return r;
1572 }
1573 else {
1574 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001575 int lineno = st->st_cur->ste_lineno;
1576 int col_offset = st->st_cur->ste_col_offset;
1577 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1578 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1579 Py_DECREF(store_name);
1580 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 }
1582 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1583 Py_DECREF(store_name);
1584 return 1;
1585 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586}
1587
1588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 VISIT(st, expr, lc->target);
1593 VISIT(st, expr, lc->iter);
1594 VISIT_SEQ(st, expr, lc->ifs);
1595 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596}
1597
1598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600symtable_visit_keyword(struct symtable *st, keyword_ty k)
1601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 VISIT(st, expr, k->value);
1603 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604}
1605
1606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608symtable_visit_slice(struct symtable *st, slice_ty s)
1609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 switch (s->kind) {
1611 case Slice_kind:
1612 if (s->v.Slice.lower)
1613 VISIT(st, expr, s->v.Slice.lower)
1614 if (s->v.Slice.upper)
1615 VISIT(st, expr, s->v.Slice.upper)
1616 if (s->v.Slice.step)
1617 VISIT(st, expr, s->v.Slice.step)
1618 break;
1619 case ExtSlice_kind:
1620 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1621 break;
1622 case Index_kind:
1623 VISIT(st, expr, s->v.Index.value)
1624 break;
1625 }
1626 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627}
1628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001630symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001631 identifier scope_name, asdl_seq *generators,
1632 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 int is_generator = (e->kind == GeneratorExp_kind);
1635 int needs_tmp = !is_generator;
1636 comprehension_ty outermost = ((comprehension_ty)
1637 asdl_seq_GET(generators, 0));
1638 /* Outermost iterator is evaluated in current scope */
1639 VISIT(st, expr, outermost->iter);
1640 /* Create comprehension scope for the rest */
1641 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001642 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1643 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 return 0;
1645 }
1646 st->st_cur->ste_generator = is_generator;
1647 /* Outermost iter is received as an argument */
1648 if (!symtable_implicit_arg(st, 0)) {
1649 symtable_exit_block(st, (void *)e);
1650 return 0;
1651 }
1652 /* Allocate temporary name if needed */
1653 if (needs_tmp && !symtable_new_tmpname(st)) {
1654 symtable_exit_block(st, (void *)e);
1655 return 0;
1656 }
1657 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1658 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1659 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1660 generators, 1, (void*)e);
1661 if (value)
1662 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1663 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1664 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}