blob: 232f02c5000145475d10c1e0bb6db046b2b028e2 [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;
31 PyObject *k;
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:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 Py_XDECREF(ste);
87 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088}
89
90static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
94 ste->ste_name,
95 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096}
97
98static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 ste->ste_table = NULL;
102 Py_XDECREF(ste->ste_id);
103 Py_XDECREF(ste->ste_name);
104 Py_XDECREF(ste->ste_symbols);
105 Py_XDECREF(ste->ste_varnames);
106 Py_XDECREF(ste->ste_children);
107 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108}
109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000111
Guido van Rossum6f799372001-09-20 20:46:19 +0000112static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 {"id", T_OBJECT, OFF(ste_id), READONLY},
114 {"name", T_OBJECT, OFF(ste_name), READONLY},
115 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
116 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
117 {"children", T_OBJECT, OFF(ste_children), READONLY},
118 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
119 {"nested", T_INT, OFF(ste_nested), READONLY},
120 {"type", T_INT, OFF(ste_type), READONLY},
121 {"lineno", T_INT, OFF(ste_lineno), READONLY},
122 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000123};
124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 PyVarObject_HEAD_INIT(&PyType_Type, 0)
127 "symtable entry",
128 sizeof(PySTEntryObject),
129 0,
130 (destructor)ste_dealloc, /* tp_dealloc */
131 0, /* tp_print */
132 0, /* tp_getattr */
133 0, /* tp_setattr */
134 0, /* tp_reserved */
135 (reprfunc)ste_repr, /* tp_repr */
136 0, /* tp_as_number */
137 0, /* tp_as_sequence */
138 0, /* tp_as_mapping */
139 0, /* tp_hash */
140 0, /* tp_call */
141 0, /* tp_str */
142 PyObject_GenericGetAttr, /* tp_getattro */
143 0, /* tp_setattro */
144 0, /* tp_as_buffer */
145 Py_TPFLAGS_DEFAULT, /* tp_flags */
146 0, /* tp_doc */
147 0, /* tp_traverse */
148 0, /* tp_clear */
149 0, /* tp_richcompare */
150 0, /* tp_weaklistoffset */
151 0, /* tp_iter */
152 0, /* tp_iternext */
153 0, /* tp_methods */
154 ste_memberlist, /* tp_members */
155 0, /* tp_getset */
156 0, /* tp_base */
157 0, /* tp_dict */
158 0, /* tp_descr_get */
159 0, /* tp_descr_set */
160 0, /* tp_dictoffset */
161 0, /* tp_init */
162 0, /* tp_alloc */
163 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000164};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165
166static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000167static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000169 _Py_block_ty block, void *ast, int lineno,
170 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171static int symtable_exit_block(struct symtable *st, void *ast);
172static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
173static int symtable_visit_expr(struct symtable *st, expr_ty s);
174static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000175static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
176static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000177static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178static int symtable_visit_arguments(struct symtable *st, arguments_ty);
179static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
180static int symtable_visit_alias(struct symtable *st, alias_ty);
181static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
182static int symtable_visit_keyword(struct symtable *st, keyword_ty);
183static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000184static int symtable_visit_params(struct symtable *st, asdl_seq *args);
185static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000187static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500188static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
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{
Benjamin Petersonf5ff2232011-06-19 19:42:22 -0500228 struct symtable *st;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 asdl_seq *seq;
230 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231
Benjamin Petersonf5ff2232011-06-19 19:42:22 -0500232 if (__class__ == NULL) {
233 __class__ = PyUnicode_InternFromString("@__class__");
234 if (__class__ == NULL)
235 return NULL;
236 }
237
238 st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 if (st == NULL)
240 return st;
241 st->st_filename = filename;
242 st->st_future = future;
243 /* Make the initial symbol information gathering pass */
244 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000245 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 PySymtable_Free(st);
247 return NULL;
248 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 st->st_top = st->st_cur;
251 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
252 switch (mod->kind) {
253 case Module_kind:
254 seq = mod->v.Module.body;
255 for (i = 0; i < asdl_seq_LEN(seq); i++)
256 if (!symtable_visit_stmt(st,
257 (stmt_ty)asdl_seq_GET(seq, i)))
258 goto error;
259 break;
260 case Expression_kind:
261 if (!symtable_visit_expr(st, mod->v.Expression.body))
262 goto error;
263 break;
264 case Interactive_kind:
265 seq = mod->v.Interactive.body;
266 for (i = 0; i < asdl_seq_LEN(seq); i++)
267 if (!symtable_visit_stmt(st,
268 (stmt_ty)asdl_seq_GET(seq, i)))
269 goto error;
270 break;
271 case Suite_kind:
272 PyErr_SetString(PyExc_RuntimeError,
273 "this compiler does not handle Suites");
274 goto error;
275 }
276 if (!symtable_exit_block(st, (void *)mod)) {
277 PySymtable_Free(st);
278 return NULL;
279 }
280 /* Make the second symbol analysis pass */
281 if (symtable_analyze(st))
282 return st;
283 PySymtable_Free(st);
284 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 (void) symtable_exit_block(st, (void *)mod);
287 PySymtable_Free(st);
288 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289}
290
291void
292PySymtable_Free(struct symtable *st)
293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 Py_XDECREF(st->st_blocks);
295 Py_XDECREF(st->st_stack);
296 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297}
298
299PySTEntryObject *
300PySymtable_Lookup(struct symtable *st, void *key)
301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 k = PyLong_FromVoidPtr(key);
305 if (k == NULL)
306 return NULL;
307 v = PyDict_GetItem(st->st_blocks, k);
308 if (v) {
309 assert(PySTEntry_Check(v));
310 Py_INCREF(v);
311 }
312 else {
313 PyErr_SetString(PyExc_KeyError,
314 "unknown symbol table entry");
315 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 Py_DECREF(k);
318 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319}
320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322PyST_GetScope(PySTEntryObject *ste, PyObject *name)
323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
325 if (!v)
326 return 0;
327 assert(PyLong_Check(v));
328 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329}
330
331
332/* Analyze raw symbol information to determine scope of each name.
333
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000334 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340 explicit global is declared with the global statement. An implicit
341 global is a free variable for which the compiler has found no binding
342 in an enclosing function scope. The implicit global is either a global
343 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
344 to handle these names to implement slightly odd semantics. In such a
345 block, the name is treated as global until it is assigned to; then it
346 is treated as a local.
347
348 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000349 The first pass collects raw facts from the AST via the symtable_visit_*
350 functions: the name is a parameter here, the name is used but not defined
351 here, etc. The second pass analyzes these facts during a pass over the
352 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353
354 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000356 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000357 Names which are explicitly declared nonlocal must exist in this set of
358 visible names - if they do not, a syntax error is raised. After doing
359 the local analysis, it analyzes each of its child blocks using an
360 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361
Nick Coghlan650f0d02007-04-15 12:05:43 +0000362 The children update the free variable set. If a local variable is added to
363 the free variable set by the child, the variable is marked as a cell. The
364 function object being defined must provide runtime storage for the variable
365 that may outlive the function's frame. Cell variables are removed from the
366 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000367
Nick Coghlan650f0d02007-04-15 12:05:43 +0000368 During analysis, the names are:
369 symbols: dict mapping from symbol names to flag values (including offset scope values)
370 scopes: dict mapping from symbol names to scope values (no offset)
371 local: set of all symbol names local to the current scope
372 bound: set of all symbol names local to a containing function scope
373 free: set of all symbol names referenced but not bound in child scopes
374 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375*/
376
377#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 PyObject *o = PyLong_FromLong(I); \
379 if (!o) \
380 return 0; \
381 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
382 Py_DECREF(o); \
383 return 0; \
384 } \
385 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386}
387
388/* Decide on scope of name, given flags.
389
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000390 The namespace dictionaries may be modified to record information
391 about the new name. For example, a new global will add an entry to
392 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393*/
394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000396analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 PyObject *bound, PyObject *local, PyObject *free,
398 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (flags & DEF_GLOBAL) {
401 if (flags & DEF_PARAM) {
402 PyErr_Format(PyExc_SyntaxError,
403 "name '%U' is parameter and global",
404 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000405 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
406 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407
408 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000409 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 if (flags & DEF_NONLOCAL) {
411 PyErr_Format(PyExc_SyntaxError,
412 "name '%U' is nonlocal and global",
413 name);
414 return 0;
415 }
416 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
417 if (PySet_Add(global, name) < 0)
418 return 0;
419 if (bound && (PySet_Discard(bound, name) < 0))
420 return 0;
421 return 1;
422 }
423 if (flags & DEF_NONLOCAL) {
424 if (flags & DEF_PARAM) {
425 PyErr_Format(PyExc_SyntaxError,
426 "name '%U' is parameter and nonlocal",
427 name);
428 return 0;
429 }
430 if (!bound) {
431 PyErr_Format(PyExc_SyntaxError,
432 "nonlocal declaration not allowed at module level");
433 return 0;
434 }
435 if (!PySet_Contains(bound, name)) {
436 PyErr_Format(PyExc_SyntaxError,
437 "no binding for nonlocal '%U' found",
438 name);
439
440 return 0;
441 }
442 SET_SCOPE(scopes, name, FREE);
443 ste->ste_free = 1;
444 return PySet_Add(free, name) >= 0;
445 }
446 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000447 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (PySet_Add(local, name) < 0)
449 return 0;
450 if (PySet_Discard(global, name) < 0)
451 return 0;
452 return 1;
453 }
454 /* If an enclosing block has a binding for this name, it
455 is a free variable rather than a global variable.
456 Note that having a non-NULL bound implies that the block
457 is nested.
458 */
459 if (bound && PySet_Contains(bound, name)) {
460 SET_SCOPE(scopes, name, FREE);
461 ste->ste_free = 1;
462 return PySet_Add(free, name) >= 0;
463 }
464 /* If a parent has a global statement, then call it global
465 explicit? It could also be global implicit.
466 */
467 if (global && PySet_Contains(global, name)) {
468 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
469 return 1;
470 }
471 if (ste->ste_nested)
472 ste->ste_free = 1;
473 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
474 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475}
476
477#undef SET_SCOPE
478
479/* If a name is defined in free and also in locals, then this block
480 provides the binding for the free variable. The name should be
481 marked CELL in this block and removed from the free list.
482
483 Note that the current block's free variables are included in free.
484 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000485
Martin v. Löwis2673a572007-10-29 19:54:24 +0000486 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000487 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488*/
489
490static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000491analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PyObject *name, *v, *v_cell;
494 int success = 0;
495 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 v_cell = PyLong_FromLong(CELL);
498 if (!v_cell)
499 return 0;
500 while (PyDict_Next(scopes, &pos, &name, &v)) {
501 long scope;
502 assert(PyLong_Check(v));
503 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000504 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 continue;
506 if (!PySet_Contains(free, name))
507 continue;
508 if (restricted != NULL &&
509 PyUnicode_CompareWithASCIIString(name, restricted))
510 continue;
511 /* Replace LOCAL with CELL for this name, and remove
512 from free. It is safe to replace the value of name
513 in the dict, because it will not cause a resize.
514 */
515 if (PyDict_SetItem(scopes, name, v_cell) < 0)
516 goto error;
517 if (PySet_Discard(free, name) < 0)
518 goto error;
519 }
520 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 Py_DECREF(v_cell);
523 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524}
525
526/* Check for illegal statements in unoptimized namespaces */
527static int
528check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
532 || !(ste->ste_free || ste->ste_child_free))
533 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 trailer = (ste->ste_child_free ?
536 "contains a nested function with free variables" :
537 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 switch (ste->ste_unoptimized) {
540 case OPT_TOPLEVEL: /* import * at top-level is fine */
541 return 1;
542 case OPT_IMPORT_STAR:
543 PyErr_Format(PyExc_SyntaxError,
544 "import * is not allowed in function '%U' because it %s",
545 ste->ste_name, trailer);
546 break;
547 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000549 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
550 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552}
553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554/* Enter the final scope information into the ste_symbols dict.
555 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 * All arguments are dicts. Modifies symbols, others are read-only.
557*/
558static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000560 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 PyObject *name = NULL, *itr = NULL;
563 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
564 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* Update scope information for all symbols in this scope */
567 while (PyDict_Next(symbols, &pos, &name, &v)) {
568 long scope, flags;
569 assert(PyLong_Check(v));
570 flags = PyLong_AS_LONG(v);
571 v_scope = PyDict_GetItem(scopes, name);
572 assert(v_scope && PyLong_Check(v_scope));
573 scope = PyLong_AS_LONG(v_scope);
574 flags |= (scope << SCOPE_OFFSET);
575 v_new = PyLong_FromLong(flags);
576 if (!v_new)
577 return 0;
578 if (PyDict_SetItem(symbols, name, v_new) < 0) {
579 Py_DECREF(v_new);
580 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 Py_DECREF(v_new);
583 }
584
585 /* Record not yet resolved free variables from children (if any) */
586 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
587 if (!v_free)
588 return 0;
589
590 itr = PyObject_GetIter(free);
591 if (!itr)
592 goto error;
593
594 while ((name = PyIter_Next(itr))) {
595 v = PyDict_GetItem(symbols, name);
596
597 /* Handle symbol that already exists in this scope */
598 if (v) {
599 /* Handle a free variable in a method of
600 the class that has the same name as a local
601 or global in the class scope.
602 */
603 if (classflag &&
604 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
605 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
606 v_new = PyLong_FromLong(flags);
607 if (!v_new) {
608 goto error;
609 }
610 if (PyDict_SetItem(symbols, name, v_new) < 0) {
611 Py_DECREF(v_new);
612 goto error;
613 }
614 Py_DECREF(v_new);
615 }
616 /* It's a cell, or already free in this scope */
617 Py_DECREF(name);
618 continue;
619 }
620 /* Handle global symbol */
621 if (!PySet_Contains(bound, name)) {
622 Py_DECREF(name);
623 continue; /* it's a global */
624 }
625 /* Propagate new free symbol up the lexical stack */
626 if (PyDict_SetItem(symbols, name, v_free) < 0) {
627 goto error;
628 }
629 Py_DECREF(name);
630 }
631 Py_DECREF(itr);
632 Py_DECREF(v_free);
633 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000634error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 Py_XDECREF(v_free);
636 Py_XDECREF(itr);
637 Py_XDECREF(name);
638 return 0;
639}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640
641/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000642
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 Arguments:
644 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000645 bound -- set of variables bound in enclosing scopes (input). bound
646 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 free -- set of free variables in enclosed scopes (output)
648 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000649
650 The implementation uses two mutually recursive functions,
651 analyze_block() and analyze_child_block(). analyze_block() is
652 responsible for analyzing the individual names defined in a block.
653 analyze_child_block() prepares temporary namespace dictionaries
654 used to evaluated nested blocks.
655
656 The two functions exist because a child block should see the name
657 bindings of its enclosing blocks, but those bindings should not
658 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659*/
660
661static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
663 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000664
665static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
667 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
670 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
671 PyObject *temp;
672 int i, success = 0;
673 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 local = PySet_New(NULL); /* collect new names bound in block */
676 if (!local)
677 goto error;
678 scopes = PyDict_New(); /* collect scopes defined for each name */
679 if (!scopes)
680 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 /* Allocate new global and bound variable dictionaries. These
683 dictionaries hold the names visible in nested blocks. For
684 ClassBlocks, the bound and global names are initialized
685 before analyzing names, because class bindings aren't
686 visible in methods. For other blocks, they are initialized
687 after names are analyzed.
688 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 /* TODO(jhylton): Package these dicts in a struct so that we
691 can write reasonable helper functions?
692 */
693 newglobal = PySet_New(NULL);
694 if (!newglobal)
695 goto error;
696 newfree = PySet_New(NULL);
697 if (!newfree)
698 goto error;
699 newbound = PySet_New(NULL);
700 if (!newbound)
701 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 /* Class namespace has no effect on names visible in
704 nested functions, so populate the global and bound
705 sets to be passed to child blocks before analyzing
706 this one.
707 */
708 if (ste->ste_type == ClassBlock) {
709 /* Pass down known globals */
710 temp = PyNumber_InPlaceOr(newglobal, global);
711 if (!temp)
712 goto error;
713 Py_DECREF(temp);
714 /* Pass down previously bound symbols */
715 if (bound) {
716 temp = PyNumber_InPlaceOr(newbound, bound);
717 if (!temp)
718 goto error;
719 Py_DECREF(temp);
720 }
721 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
724 long flags = PyLong_AS_LONG(v);
725 if (!analyze_name(ste, scopes, name, flags,
726 bound, local, free, global))
727 goto error;
728 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 /* Populate global and bound sets to be passed to children. */
731 if (ste->ste_type != ClassBlock) {
732 /* Add function locals to bound set */
733 if (ste->ste_type == FunctionBlock) {
734 temp = PyNumber_InPlaceOr(newbound, local);
735 if (!temp)
736 goto error;
737 Py_DECREF(temp);
738 }
739 /* Pass down previously bound symbols */
740 if (bound) {
741 temp = PyNumber_InPlaceOr(newbound, bound);
742 if (!temp)
743 goto error;
744 Py_DECREF(temp);
745 }
746 /* Pass down known globals */
747 temp = PyNumber_InPlaceOr(newglobal, global);
748 if (!temp)
749 goto error;
750 Py_DECREF(temp);
751 }
752 else {
753 /* Special-case __class__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 assert(PySet_Contains(local, __class__) == 1);
755 if (PySet_Add(newbound, __class__) < 0)
756 goto error;
757 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300759 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 newbound, newglobal now contain the names visible in
762 nested blocks. The free variables in the children will
763 be collected in allfree.
764 */
765 allfree = PySet_New(NULL);
766 if (!allfree)
767 goto error;
768 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
769 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
770 PySTEntryObject* entry;
771 assert(c && PySTEntry_Check(c));
772 entry = (PySTEntryObject*)c;
773 if (!analyze_child_block(entry, newbound, newfree, newglobal,
774 allfree))
775 goto error;
776 /* Check if any children have free variables */
777 if (entry->ste_free || entry->ste_child_free)
778 ste->ste_child_free = 1;
779 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 temp = PyNumber_InPlaceOr(newfree, allfree);
782 if (!temp)
783 goto error;
784 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 /* Check if any local variables must be converted to cell variables */
787 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
788 NULL))
789 goto error;
790 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
Benjamin Petersonf5ff2232011-06-19 19:42:22 -0500791 "@__class__"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 goto error;
793 /* Records the results of the analysis in the symbol table entry */
794 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
795 ste->ste_type == ClassBlock))
796 goto error;
797 if (!check_unoptimized(ste))
798 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 temp = PyNumber_InPlaceOr(free, newfree);
801 if (!temp)
802 goto error;
803 Py_DECREF(temp);
804 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 Py_XDECREF(scopes);
807 Py_XDECREF(local);
808 Py_XDECREF(newbound);
809 Py_XDECREF(newglobal);
810 Py_XDECREF(newfree);
811 Py_XDECREF(allfree);
812 if (!success)
813 assert(PyErr_Occurred());
814 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815}
816
817static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
819 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
822 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 These dictionary are used by all blocks enclosed by the
827 current block. The analyze_block() call modifies these
828 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 */
831 temp_bound = PySet_New(bound);
832 if (!temp_bound)
833 goto error;
834 temp_free = PySet_New(free);
835 if (!temp_free)
836 goto error;
837 temp_global = PySet_New(global);
838 if (!temp_global)
839 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
842 goto error;
843 temp = PyNumber_InPlaceOr(child_free, temp_free);
844 if (!temp)
845 goto error;
846 Py_DECREF(temp);
847 Py_DECREF(temp_bound);
848 Py_DECREF(temp_free);
849 Py_DECREF(temp_global);
850 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000851 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 Py_XDECREF(temp_bound);
853 Py_XDECREF(temp_free);
854 Py_XDECREF(temp_global);
855 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000856}
857
858static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859symtable_analyze(struct symtable *st)
860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 PyObject *free, *global;
862 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 free = PySet_New(NULL);
865 if (!free)
866 return 0;
867 global = PySet_New(NULL);
868 if (!global) {
869 Py_DECREF(free);
870 return 0;
871 }
872 r = analyze_block(st->st_top, NULL, free, global);
873 Py_DECREF(free);
874 Py_DECREF(global);
875 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876}
877
878
879static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000880symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
883 lineno, NULL, NULL) < 0) {
884 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
885 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000886 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
887 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 }
889 return 0;
890 }
891 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892}
893
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000894/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895 This reference is released when the block is exited, via the DECREF
896 in symtable_exit_block().
897*/
898
899static int
900symtable_exit_block(struct symtable *st, void *ast)
901{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 Py_CLEAR(st->st_cur);
905 end = PyList_GET_SIZE(st->st_stack) - 1;
906 if (end >= 0) {
907 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
908 end);
909 if (st->st_cur == NULL)
910 return 0;
911 Py_INCREF(st->st_cur);
912 if (PySequence_DelItem(st->st_stack, end) < 0)
913 return 0;
914 }
915 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916}
917
918static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000920 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 if (st->st_cur) {
925 prev = st->st_cur;
926 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
927 return 0;
928 }
929 Py_DECREF(st->st_cur);
930 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000931 st->st_cur = ste_new(st, name, block, ast, lineno, col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 if (st->st_cur == NULL)
933 return 0;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000934 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 st->st_global = st->st_cur->ste_symbols;
936 if (prev) {
937 if (PyList_Append(prev->ste_children,
938 (PyObject *)st->st_cur) < 0) {
939 return 0;
940 }
941 }
942 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943}
944
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000945static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946symtable_lookup(struct symtable *st, PyObject *name)
947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 PyObject *o;
949 PyObject *mangled = _Py_Mangle(st->st_private, name);
950 if (!mangled)
951 return 0;
952 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
953 Py_DECREF(mangled);
954 if (!o)
955 return 0;
956 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957}
958
959static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 PyObject *o;
963 PyObject *dict;
964 long val;
965 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966
Jeremy Hylton81e95022007-02-27 06:50:52 +0000967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 if (!mangled)
969 return 0;
970 dict = st->st_cur->ste_symbols;
971 if ((o = PyDict_GetItem(dict, mangled))) {
972 val = PyLong_AS_LONG(o);
973 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
974 /* Is it better to use 'mangled' or 'name' here? */
975 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000976 PyErr_SyntaxLocationEx(st->st_filename,
977 st->st_cur->ste_lineno,
978 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 goto error;
980 }
981 val |= flag;
982 } else
983 val = flag;
984 o = PyLong_FromLong(val);
985 if (o == NULL)
986 goto error;
987 if (PyDict_SetItem(dict, mangled, o) < 0) {
988 Py_DECREF(o);
989 goto error;
990 }
991 Py_DECREF(o);
992
993 if (flag & DEF_PARAM) {
994 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
995 goto error;
996 } else if (flag & DEF_GLOBAL) {
997 /* XXX need to update DEF_GLOBAL for other flags too;
998 perhaps only DEF_FREE_GLOBAL */
999 val = flag;
1000 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1001 val |= PyLong_AS_LONG(o);
1002 }
1003 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 goto error;
1006 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1007 Py_DECREF(o);
1008 goto error;
1009 }
1010 Py_DECREF(o);
1011 }
1012 Py_DECREF(mangled);
1013 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001014
1015error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 Py_DECREF(mangled);
1017 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018}
1019
1020/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1021 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 function.
1023
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1025 useful if the first node in the sequence requires special treatment.
1026*/
1027
1028#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (!symtable_visit_ ## TYPE((ST), (V))) \
1030 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001031
1032#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1034 symtable_exit_block((ST), (S)); \
1035 return 0; \
1036 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001037
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 int i; \
1040 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1041 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1042 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1043 if (!symtable_visit_ ## TYPE((ST), elt)) \
1044 return 0; \
1045 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001047
1048#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 int i; \
1050 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1051 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1052 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1053 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1054 symtable_exit_block((ST), (S)); \
1055 return 0; \
1056 } \
1057 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001058}
1059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 int i; \
1062 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1063 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1064 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1065 if (!symtable_visit_ ## TYPE((ST), elt)) \
1066 return 0; \
1067 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001069
1070#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 int i; \
1072 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1073 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1074 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1075 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1076 symtable_exit_block((ST), (S)); \
1077 return 0; \
1078 } \
1079 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001080}
1081
Guido van Rossum4f72a782006-10-27 23:31:49 +00001082#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 int i = 0; \
1084 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1085 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1086 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1087 if (!elt) continue; /* can be NULL */ \
1088 if (!symtable_visit_expr((ST), elt)) \
1089 return 0; \
1090 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001091}
1092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001094symtable_new_tmpname(struct symtable *st)
1095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 char tmpname[256];
1097 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1100 ++st->st_cur->ste_tmpname);
1101 tmp = PyUnicode_InternFromString(tmpname);
1102 if (!tmp)
1103 return 0;
1104 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1105 return 0;
1106 Py_DECREF(tmp);
1107 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001108}
1109
Guido van Rossum4f72a782006-10-27 23:31:49 +00001110
Guido van Rossumc2e20742006-02-27 22:32:47 +00001111static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112symtable_visit_stmt(struct symtable *st, stmt_ty s)
1113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 switch (s->kind) {
1115 case FunctionDef_kind:
1116 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1117 return 0;
1118 if (s->v.FunctionDef.args->defaults)
1119 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1120 if (s->v.FunctionDef.args->kw_defaults)
1121 VISIT_KWONLYDEFAULTS(st,
1122 s->v.FunctionDef.args->kw_defaults);
1123 if (!symtable_visit_annotations(st, s))
1124 return 0;
1125 if (s->v.FunctionDef.decorator_list)
1126 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1127 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001128 FunctionBlock, (void *)s, s->lineno,
1129 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 return 0;
1131 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1132 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1133 if (!symtable_exit_block(st, s))
1134 return 0;
1135 break;
1136 case ClassDef_kind: {
1137 PyObject *tmp;
1138 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1139 return 0;
1140 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1141 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1142 if (s->v.ClassDef.starargs)
1143 VISIT(st, expr, s->v.ClassDef.starargs);
1144 if (s->v.ClassDef.kwargs)
1145 VISIT(st, expr, s->v.ClassDef.kwargs);
1146 if (s->v.ClassDef.decorator_list)
1147 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1148 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001149 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 return 0;
Benjamin Petersonf5ff2232011-06-19 19:42:22 -05001151 if (!symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 !GET_IDENTIFIER(__locals__) ||
1153 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1154 symtable_exit_block(st, s);
1155 return 0;
1156 }
1157 tmp = st->st_private;
1158 st->st_private = s->v.ClassDef.name;
1159 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1160 st->st_private = tmp;
1161 if (!symtable_exit_block(st, s))
1162 return 0;
1163 break;
1164 }
1165 case Return_kind:
1166 if (s->v.Return.value) {
1167 VISIT(st, expr, s->v.Return.value);
1168 st->st_cur->ste_returns_value = 1;
1169 if (st->st_cur->ste_generator) {
1170 PyErr_SetString(PyExc_SyntaxError,
1171 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001172 PyErr_SyntaxLocationEx(st->st_filename,
1173 s->lineno,
1174 s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00001176 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 }
1178 break;
1179 case Delete_kind:
1180 VISIT_SEQ(st, expr, s->v.Delete.targets);
1181 break;
1182 case Assign_kind:
1183 VISIT_SEQ(st, expr, s->v.Assign.targets);
1184 VISIT(st, expr, s->v.Assign.value);
1185 break;
1186 case AugAssign_kind:
1187 VISIT(st, expr, s->v.AugAssign.target);
1188 VISIT(st, expr, s->v.AugAssign.value);
1189 break;
1190 case For_kind:
1191 VISIT(st, expr, s->v.For.target);
1192 VISIT(st, expr, s->v.For.iter);
1193 VISIT_SEQ(st, stmt, s->v.For.body);
1194 if (s->v.For.orelse)
1195 VISIT_SEQ(st, stmt, s->v.For.orelse);
1196 break;
1197 case While_kind:
1198 VISIT(st, expr, s->v.While.test);
1199 VISIT_SEQ(st, stmt, s->v.While.body);
1200 if (s->v.While.orelse)
1201 VISIT_SEQ(st, stmt, s->v.While.orelse);
1202 break;
1203 case If_kind:
1204 /* XXX if 0: and lookup_yield() hacks */
1205 VISIT(st, expr, s->v.If.test);
1206 VISIT_SEQ(st, stmt, s->v.If.body);
1207 if (s->v.If.orelse)
1208 VISIT_SEQ(st, stmt, s->v.If.orelse);
1209 break;
1210 case Raise_kind:
1211 if (s->v.Raise.exc) {
1212 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001213 if (s->v.Raise.cause) {
1214 VISIT(st, expr, s->v.Raise.cause);
1215 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 }
1217 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001218 case Try_kind:
1219 VISIT_SEQ(st, stmt, s->v.Try.body);
1220 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1221 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1222 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 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:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001310 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 VISIT_SEQ(st, stmt, s->v.With.body);
1312 break;
1313 }
1314 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315}
1316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318symtable_visit_expr(struct symtable *st, expr_ty e)
1319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 switch (e->kind) {
1321 case BoolOp_kind:
1322 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1323 break;
1324 case BinOp_kind:
1325 VISIT(st, expr, e->v.BinOp.left);
1326 VISIT(st, expr, e->v.BinOp.right);
1327 break;
1328 case UnaryOp_kind:
1329 VISIT(st, expr, e->v.UnaryOp.operand);
1330 break;
1331 case Lambda_kind: {
1332 if (!GET_IDENTIFIER(lambda))
1333 return 0;
1334 if (e->v.Lambda.args->defaults)
1335 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1336 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001337 FunctionBlock, (void *)e, e->lineno,
1338 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 return 0;
1340 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1341 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1342 if (!symtable_exit_block(st, (void *)e))
1343 return 0;
1344 break;
1345 }
1346 case IfExp_kind:
1347 VISIT(st, expr, e->v.IfExp.test);
1348 VISIT(st, expr, e->v.IfExp.body);
1349 VISIT(st, expr, e->v.IfExp.orelse);
1350 break;
1351 case Dict_kind:
1352 VISIT_SEQ(st, expr, e->v.Dict.keys);
1353 VISIT_SEQ(st, expr, e->v.Dict.values);
1354 break;
1355 case Set_kind:
1356 VISIT_SEQ(st, expr, e->v.Set.elts);
1357 break;
1358 case GeneratorExp_kind:
1359 if (!symtable_visit_genexp(st, e))
1360 return 0;
1361 break;
1362 case ListComp_kind:
1363 if (!symtable_visit_listcomp(st, e))
1364 return 0;
1365 break;
1366 case SetComp_kind:
1367 if (!symtable_visit_setcomp(st, e))
1368 return 0;
1369 break;
1370 case DictComp_kind:
1371 if (!symtable_visit_dictcomp(st, e))
1372 return 0;
1373 break;
1374 case Yield_kind:
1375 if (e->v.Yield.value)
1376 VISIT(st, expr, e->v.Yield.value);
1377 st->st_cur->ste_generator = 1;
1378 if (st->st_cur->ste_returns_value) {
1379 PyErr_SetString(PyExc_SyntaxError,
1380 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001381 PyErr_SyntaxLocationEx(st->st_filename,
1382 e->lineno, e->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 return 0;
1384 }
1385 break;
1386 case Compare_kind:
1387 VISIT(st, expr, e->v.Compare.left);
1388 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1389 break;
1390 case Call_kind:
1391 VISIT(st, expr, e->v.Call.func);
1392 VISIT_SEQ(st, expr, e->v.Call.args);
1393 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1394 if (e->v.Call.starargs)
1395 VISIT(st, expr, e->v.Call.starargs);
1396 if (e->v.Call.kwargs)
1397 VISIT(st, expr, e->v.Call.kwargs);
1398 break;
1399 case Num_kind:
1400 case Str_kind:
1401 case Bytes_kind:
1402 case Ellipsis_kind:
1403 /* Nothing to do here. */
1404 break;
1405 /* The following exprs can be assignment targets. */
1406 case Attribute_kind:
1407 VISIT(st, expr, e->v.Attribute.value);
1408 break;
1409 case Subscript_kind:
1410 VISIT(st, expr, e->v.Subscript.value);
1411 VISIT(st, slice, e->v.Subscript.slice);
1412 break;
1413 case Starred_kind:
1414 VISIT(st, expr, e->v.Starred.value);
1415 break;
1416 case Name_kind:
1417 if (!symtable_add_def(st, e->v.Name.id,
1418 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1419 return 0;
1420 /* Special-case super: it counts as a use of __class__ */
1421 if (e->v.Name.ctx == Load &&
1422 st->st_cur->ste_type == FunctionBlock &&
1423 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Benjamin Petersonf5ff2232011-06-19 19:42:22 -05001424 if (!symtable_add_def(st, __class__, USE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 return 0;
1426 }
1427 break;
1428 /* child nodes of List and Tuple will have expr_context set */
1429 case List_kind:
1430 VISIT_SEQ(st, expr, e->v.List.elts);
1431 break;
1432 case Tuple_kind:
1433 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1434 break;
1435 }
1436 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437}
1438
1439static int
1440symtable_implicit_arg(struct symtable *st, int pos)
1441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1443 if (id == NULL)
1444 return 0;
1445 if (!symtable_add_def(st, id, DEF_PARAM)) {
1446 Py_DECREF(id);
1447 return 0;
1448 }
1449 Py_DECREF(id);
1450 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451}
1452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001454symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 if (!args)
1459 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 for (i = 0; i < asdl_seq_LEN(args); i++) {
1462 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1463 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1464 return 0;
1465 }
1466
1467 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468}
1469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001471symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 if (!args)
1476 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 for (i = 0; i < asdl_seq_LEN(args); i++) {
1479 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1480 if (arg->annotation)
1481 VISIT(st, expr, arg->annotation);
1482 }
1483
1484 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001485}
1486
Neal Norwitzc1505362006-12-28 06:47:50 +00001487static int
1488symtable_visit_annotations(struct symtable *st, stmt_ty s)
1489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 arguments_ty a = s->v.FunctionDef.args;
1491
1492 if (a->args && !symtable_visit_argannotations(st, a->args))
1493 return 0;
1494 if (a->varargannotation)
1495 VISIT(st, expr, a->varargannotation);
1496 if (a->kwargannotation)
1497 VISIT(st, expr, a->kwargannotation);
1498 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1499 return 0;
1500 if (s->v.FunctionDef.returns)
1501 VISIT(st, expr, s->v.FunctionDef.returns);
1502 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503}
1504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506symtable_visit_arguments(struct symtable *st, arguments_ty a)
1507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 /* skip default arguments inside function block
1509 XXX should ast be different?
1510 */
1511 if (a->args && !symtable_visit_params(st, a->args))
1512 return 0;
1513 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1514 return 0;
1515 if (a->vararg) {
1516 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1517 return 0;
1518 st->st_cur->ste_varargs = 1;
1519 }
1520 if (a->kwarg) {
1521 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1522 return 0;
1523 st->st_cur->ste_varkeywords = 1;
1524 }
1525 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526}
1527
1528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 if (eh->v.ExceptHandler.type)
1533 VISIT(st, expr, eh->v.ExceptHandler.type);
1534 if (eh->v.ExceptHandler.name)
1535 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1536 return 0;
1537 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1538 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539}
1540
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001541static int
1542symtable_visit_withitem(struct symtable *st, withitem_ty item)
1543{
1544 VISIT(st, expr, item->context_expr);
1545 if (item->optional_vars) {
1546 VISIT(st, expr, item->optional_vars);
1547 }
1548 return 1;
1549}
1550
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553symtable_visit_alias(struct symtable *st, alias_ty a)
1554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001556 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 dotted package name (e.g. spam.eggs)
1558 */
1559 PyObject *store_name;
1560 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1561 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1562 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
1563 if (dot) {
1564 store_name = PyUnicode_FromUnicode(base, dot - base);
1565 if (!store_name)
1566 return 0;
1567 }
1568 else {
1569 store_name = name;
1570 Py_INCREF(store_name);
1571 }
1572 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1573 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1574 Py_DECREF(store_name);
1575 return r;
1576 }
1577 else {
1578 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001579 int lineno = st->st_cur->ste_lineno;
1580 int col_offset = st->st_cur->ste_col_offset;
1581 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1582 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1583 Py_DECREF(store_name);
1584 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 }
1586 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1587 Py_DECREF(store_name);
1588 return 1;
1589 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590}
1591
1592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 VISIT(st, expr, lc->target);
1597 VISIT(st, expr, lc->iter);
1598 VISIT_SEQ(st, expr, lc->ifs);
1599 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600}
1601
1602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604symtable_visit_keyword(struct symtable *st, keyword_ty k)
1605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 VISIT(st, expr, k->value);
1607 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608}
1609
1610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612symtable_visit_slice(struct symtable *st, slice_ty s)
1613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 switch (s->kind) {
1615 case Slice_kind:
1616 if (s->v.Slice.lower)
1617 VISIT(st, expr, s->v.Slice.lower)
1618 if (s->v.Slice.upper)
1619 VISIT(st, expr, s->v.Slice.upper)
1620 if (s->v.Slice.step)
1621 VISIT(st, expr, s->v.Slice.step)
1622 break;
1623 case ExtSlice_kind:
1624 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1625 break;
1626 case Index_kind:
1627 VISIT(st, expr, s->v.Index.value)
1628 break;
1629 }
1630 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631}
1632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001634symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001635 identifier scope_name, asdl_seq *generators,
1636 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 int is_generator = (e->kind == GeneratorExp_kind);
1639 int needs_tmp = !is_generator;
1640 comprehension_ty outermost = ((comprehension_ty)
1641 asdl_seq_GET(generators, 0));
1642 /* Outermost iterator is evaluated in current scope */
1643 VISIT(st, expr, outermost->iter);
1644 /* Create comprehension scope for the rest */
1645 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001646 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1647 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 return 0;
1649 }
1650 st->st_cur->ste_generator = is_generator;
1651 /* Outermost iter is received as an argument */
1652 if (!symtable_implicit_arg(st, 0)) {
1653 symtable_exit_block(st, (void *)e);
1654 return 0;
1655 }
1656 /* Allocate temporary name if needed */
1657 if (needs_tmp && !symtable_new_tmpname(st)) {
1658 symtable_exit_block(st, (void *)e);
1659 return 0;
1660 }
1661 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1662 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1663 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1664 generators, 1, (void*)e);
1665 if (value)
1666 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1667 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1668 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001672symtable_visit_genexp(struct symtable *st, expr_ty e)
1673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1675 e->v.GeneratorExp.generators,
1676 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001677}
1678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001680symtable_visit_listcomp(struct symtable *st, expr_ty e)
1681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1683 e->v.ListComp.generators,
1684 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001685}
1686
1687static int
1688symtable_visit_setcomp(struct symtable *st, expr_ty e)
1689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1691 e->v.SetComp.generators,
1692 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001693}
1694
1695static int
1696symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1699 e->v.DictComp.generators,
1700 e->v.DictComp.key,
1701 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001702}