blob: f86575bdf4c4309885177d9244f081888e8dc051 [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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 int i; \
1034 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1035 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1036 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1037 if (!symtable_visit_ ## TYPE((ST), elt)) \
1038 return 0; \
1039 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 int i; \
1044 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1045 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1046 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1047 if (!symtable_visit_ ## TYPE((ST), elt)) \
1048 return 0; \
1049 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001051
Guido van Rossum4f72a782006-10-27 23:31:49 +00001052#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 int i = 0; \
1054 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1055 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1056 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1057 if (!elt) continue; /* can be NULL */ \
1058 if (!symtable_visit_expr((ST), elt)) \
1059 return 0; \
1060 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001061}
1062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001064symtable_new_tmpname(struct symtable *st)
1065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 char tmpname[256];
1067 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1070 ++st->st_cur->ste_tmpname);
1071 tmp = PyUnicode_InternFromString(tmpname);
1072 if (!tmp)
1073 return 0;
1074 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1075 return 0;
1076 Py_DECREF(tmp);
1077 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001078}
1079
Guido van Rossum4f72a782006-10-27 23:31:49 +00001080
Guido van Rossumc2e20742006-02-27 22:32:47 +00001081static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082symtable_visit_stmt(struct symtable *st, stmt_ty s)
1083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 switch (s->kind) {
1085 case FunctionDef_kind:
1086 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1087 return 0;
1088 if (s->v.FunctionDef.args->defaults)
1089 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1090 if (s->v.FunctionDef.args->kw_defaults)
1091 VISIT_KWONLYDEFAULTS(st,
1092 s->v.FunctionDef.args->kw_defaults);
1093 if (!symtable_visit_annotations(st, s))
1094 return 0;
1095 if (s->v.FunctionDef.decorator_list)
1096 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1097 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001098 FunctionBlock, (void *)s, s->lineno,
1099 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001101 VISIT(st, arguments, s->v.FunctionDef.args);
1102 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 if (!symtable_exit_block(st, s))
1104 return 0;
1105 break;
1106 case ClassDef_kind: {
1107 PyObject *tmp;
1108 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1109 return 0;
1110 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1111 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1112 if (s->v.ClassDef.starargs)
1113 VISIT(st, expr, s->v.ClassDef.starargs);
1114 if (s->v.ClassDef.kwargs)
1115 VISIT(st, expr, s->v.ClassDef.kwargs);
1116 if (s->v.ClassDef.decorator_list)
1117 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1118 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001119 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 return 0;
Benjamin Petersonf5ff2232011-06-19 19:42:22 -05001121 if (!symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 !GET_IDENTIFIER(__locals__) ||
1123 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1124 symtable_exit_block(st, s);
1125 return 0;
1126 }
1127 tmp = st->st_private;
1128 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001129 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 st->st_private = tmp;
1131 if (!symtable_exit_block(st, s))
1132 return 0;
1133 break;
1134 }
1135 case Return_kind:
1136 if (s->v.Return.value) {
1137 VISIT(st, expr, s->v.Return.value);
1138 st->st_cur->ste_returns_value = 1;
1139 if (st->st_cur->ste_generator) {
1140 PyErr_SetString(PyExc_SyntaxError,
1141 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001142 PyErr_SyntaxLocationEx(st->st_filename,
1143 s->lineno,
1144 s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00001146 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 }
1148 break;
1149 case Delete_kind:
1150 VISIT_SEQ(st, expr, s->v.Delete.targets);
1151 break;
1152 case Assign_kind:
1153 VISIT_SEQ(st, expr, s->v.Assign.targets);
1154 VISIT(st, expr, s->v.Assign.value);
1155 break;
1156 case AugAssign_kind:
1157 VISIT(st, expr, s->v.AugAssign.target);
1158 VISIT(st, expr, s->v.AugAssign.value);
1159 break;
1160 case For_kind:
1161 VISIT(st, expr, s->v.For.target);
1162 VISIT(st, expr, s->v.For.iter);
1163 VISIT_SEQ(st, stmt, s->v.For.body);
1164 if (s->v.For.orelse)
1165 VISIT_SEQ(st, stmt, s->v.For.orelse);
1166 break;
1167 case While_kind:
1168 VISIT(st, expr, s->v.While.test);
1169 VISIT_SEQ(st, stmt, s->v.While.body);
1170 if (s->v.While.orelse)
1171 VISIT_SEQ(st, stmt, s->v.While.orelse);
1172 break;
1173 case If_kind:
1174 /* XXX if 0: and lookup_yield() hacks */
1175 VISIT(st, expr, s->v.If.test);
1176 VISIT_SEQ(st, stmt, s->v.If.body);
1177 if (s->v.If.orelse)
1178 VISIT_SEQ(st, stmt, s->v.If.orelse);
1179 break;
1180 case Raise_kind:
1181 if (s->v.Raise.exc) {
1182 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001183 if (s->v.Raise.cause) {
1184 VISIT(st, expr, s->v.Raise.cause);
1185 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 }
1187 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001188 case Try_kind:
1189 VISIT_SEQ(st, stmt, s->v.Try.body);
1190 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1191 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1192 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 break;
1194 case Assert_kind:
1195 VISIT(st, expr, s->v.Assert.test);
1196 if (s->v.Assert.msg)
1197 VISIT(st, expr, s->v.Assert.msg);
1198 break;
1199 case Import_kind:
1200 VISIT_SEQ(st, alias, s->v.Import.names);
1201 /* XXX Don't have the lineno available inside
1202 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001203 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001205 st->st_cur->ste_opt_col_offset = s->col_offset;
1206 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 break;
1208 case ImportFrom_kind:
1209 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1210 /* XXX Don't have the lineno available inside
1211 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001212 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001214 st->st_cur->ste_opt_col_offset = s->col_offset;
1215 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 break;
1217 case Global_kind: {
1218 int i;
1219 asdl_seq *seq = s->v.Global.names;
1220 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1221 identifier name = (identifier)asdl_seq_GET(seq, i);
1222 char *c_name = _PyUnicode_AsString(name);
1223 long cur = symtable_lookup(st, name);
1224 if (cur < 0)
1225 return 0;
1226 if (cur & (DEF_LOCAL | USE)) {
1227 char buf[256];
1228 if (cur & DEF_LOCAL)
1229 PyOS_snprintf(buf, sizeof(buf),
1230 GLOBAL_AFTER_ASSIGN,
1231 c_name);
1232 else
1233 PyOS_snprintf(buf, sizeof(buf),
1234 GLOBAL_AFTER_USE,
1235 c_name);
1236 if (!symtable_warn(st, buf, s->lineno))
1237 return 0;
1238 }
1239 if (!symtable_add_def(st, name, DEF_GLOBAL))
1240 return 0;
1241 }
1242 break;
1243 }
1244 case Nonlocal_kind: {
1245 int i;
1246 asdl_seq *seq = s->v.Nonlocal.names;
1247 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1248 identifier name = (identifier)asdl_seq_GET(seq, i);
1249 char *c_name = _PyUnicode_AsString(name);
1250 long cur = symtable_lookup(st, name);
1251 if (cur < 0)
1252 return 0;
1253 if (cur & (DEF_LOCAL | USE)) {
1254 char buf[256];
1255 if (cur & DEF_LOCAL)
1256 PyOS_snprintf(buf, sizeof(buf),
1257 NONLOCAL_AFTER_ASSIGN,
1258 c_name);
1259 else
1260 PyOS_snprintf(buf, sizeof(buf),
1261 NONLOCAL_AFTER_USE,
1262 c_name);
1263 if (!symtable_warn(st, buf, s->lineno))
1264 return 0;
1265 }
1266 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1267 return 0;
1268 }
1269 break;
1270 }
1271 case Expr_kind:
1272 VISIT(st, expr, s->v.Expr.value);
1273 break;
1274 case Pass_kind:
1275 case Break_kind:
1276 case Continue_kind:
1277 /* nothing to do here */
1278 break;
1279 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001280 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 VISIT_SEQ(st, stmt, s->v.With.body);
1282 break;
1283 }
1284 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285}
1286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288symtable_visit_expr(struct symtable *st, expr_ty e)
1289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 switch (e->kind) {
1291 case BoolOp_kind:
1292 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1293 break;
1294 case BinOp_kind:
1295 VISIT(st, expr, e->v.BinOp.left);
1296 VISIT(st, expr, e->v.BinOp.right);
1297 break;
1298 case UnaryOp_kind:
1299 VISIT(st, expr, e->v.UnaryOp.operand);
1300 break;
1301 case Lambda_kind: {
1302 if (!GET_IDENTIFIER(lambda))
1303 return 0;
1304 if (e->v.Lambda.args->defaults)
1305 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1306 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001307 FunctionBlock, (void *)e, e->lineno,
1308 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001310 VISIT(st, arguments, e->v.Lambda.args);
1311 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 if (!symtable_exit_block(st, (void *)e))
1313 return 0;
1314 break;
1315 }
1316 case IfExp_kind:
1317 VISIT(st, expr, e->v.IfExp.test);
1318 VISIT(st, expr, e->v.IfExp.body);
1319 VISIT(st, expr, e->v.IfExp.orelse);
1320 break;
1321 case Dict_kind:
1322 VISIT_SEQ(st, expr, e->v.Dict.keys);
1323 VISIT_SEQ(st, expr, e->v.Dict.values);
1324 break;
1325 case Set_kind:
1326 VISIT_SEQ(st, expr, e->v.Set.elts);
1327 break;
1328 case GeneratorExp_kind:
1329 if (!symtable_visit_genexp(st, e))
1330 return 0;
1331 break;
1332 case ListComp_kind:
1333 if (!symtable_visit_listcomp(st, e))
1334 return 0;
1335 break;
1336 case SetComp_kind:
1337 if (!symtable_visit_setcomp(st, e))
1338 return 0;
1339 break;
1340 case DictComp_kind:
1341 if (!symtable_visit_dictcomp(st, e))
1342 return 0;
1343 break;
1344 case Yield_kind:
1345 if (e->v.Yield.value)
1346 VISIT(st, expr, e->v.Yield.value);
1347 st->st_cur->ste_generator = 1;
1348 if (st->st_cur->ste_returns_value) {
1349 PyErr_SetString(PyExc_SyntaxError,
1350 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001351 PyErr_SyntaxLocationEx(st->st_filename,
1352 e->lineno, e->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 return 0;
1354 }
1355 break;
1356 case Compare_kind:
1357 VISIT(st, expr, e->v.Compare.left);
1358 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1359 break;
1360 case Call_kind:
1361 VISIT(st, expr, e->v.Call.func);
1362 VISIT_SEQ(st, expr, e->v.Call.args);
1363 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1364 if (e->v.Call.starargs)
1365 VISIT(st, expr, e->v.Call.starargs);
1366 if (e->v.Call.kwargs)
1367 VISIT(st, expr, e->v.Call.kwargs);
1368 break;
1369 case Num_kind:
1370 case Str_kind:
1371 case Bytes_kind:
1372 case Ellipsis_kind:
1373 /* Nothing to do here. */
1374 break;
1375 /* The following exprs can be assignment targets. */
1376 case Attribute_kind:
1377 VISIT(st, expr, e->v.Attribute.value);
1378 break;
1379 case Subscript_kind:
1380 VISIT(st, expr, e->v.Subscript.value);
1381 VISIT(st, slice, e->v.Subscript.slice);
1382 break;
1383 case Starred_kind:
1384 VISIT(st, expr, e->v.Starred.value);
1385 break;
1386 case Name_kind:
1387 if (!symtable_add_def(st, e->v.Name.id,
1388 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1389 return 0;
1390 /* Special-case super: it counts as a use of __class__ */
1391 if (e->v.Name.ctx == Load &&
1392 st->st_cur->ste_type == FunctionBlock &&
1393 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Benjamin Petersonf5ff2232011-06-19 19:42:22 -05001394 if (!symtable_add_def(st, __class__, USE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 return 0;
1396 }
1397 break;
1398 /* child nodes of List and Tuple will have expr_context set */
1399 case List_kind:
1400 VISIT_SEQ(st, expr, e->v.List.elts);
1401 break;
1402 case Tuple_kind:
1403 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1404 break;
1405 }
1406 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407}
1408
1409static int
1410symtable_implicit_arg(struct symtable *st, int pos)
1411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1413 if (id == NULL)
1414 return 0;
1415 if (!symtable_add_def(st, id, DEF_PARAM)) {
1416 Py_DECREF(id);
1417 return 0;
1418 }
1419 Py_DECREF(id);
1420 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421}
1422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001424symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 if (!args)
1429 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 for (i = 0; i < asdl_seq_LEN(args); i++) {
1432 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1433 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1434 return 0;
1435 }
1436
1437 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438}
1439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001441symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 if (!args)
1446 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 for (i = 0; i < asdl_seq_LEN(args); i++) {
1449 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1450 if (arg->annotation)
1451 VISIT(st, expr, arg->annotation);
1452 }
1453
1454 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001455}
1456
Neal Norwitzc1505362006-12-28 06:47:50 +00001457static int
1458symtable_visit_annotations(struct symtable *st, stmt_ty s)
1459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 arguments_ty a = s->v.FunctionDef.args;
1461
1462 if (a->args && !symtable_visit_argannotations(st, a->args))
1463 return 0;
1464 if (a->varargannotation)
1465 VISIT(st, expr, a->varargannotation);
1466 if (a->kwargannotation)
1467 VISIT(st, expr, a->kwargannotation);
1468 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1469 return 0;
1470 if (s->v.FunctionDef.returns)
1471 VISIT(st, expr, s->v.FunctionDef.returns);
1472 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473}
1474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476symtable_visit_arguments(struct symtable *st, arguments_ty a)
1477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001478 /* skip default arguments inside function block
1479 XXX should ast be different?
1480 */
1481 if (a->args && !symtable_visit_params(st, a->args))
1482 return 0;
1483 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1484 return 0;
1485 if (a->vararg) {
1486 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1487 return 0;
1488 st->st_cur->ste_varargs = 1;
1489 }
1490 if (a->kwarg) {
1491 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1492 return 0;
1493 st->st_cur->ste_varkeywords = 1;
1494 }
1495 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496}
1497
1498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 if (eh->v.ExceptHandler.type)
1503 VISIT(st, expr, eh->v.ExceptHandler.type);
1504 if (eh->v.ExceptHandler.name)
1505 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1506 return 0;
1507 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1508 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509}
1510
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001511static int
1512symtable_visit_withitem(struct symtable *st, withitem_ty item)
1513{
1514 VISIT(st, expr, item->context_expr);
1515 if (item->optional_vars) {
1516 VISIT(st, expr, item->optional_vars);
1517 }
1518 return 1;
1519}
1520
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523symtable_visit_alias(struct symtable *st, alias_ty a)
1524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001526 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 dotted package name (e.g. spam.eggs)
1528 */
1529 PyObject *store_name;
1530 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1531 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1532 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
1533 if (dot) {
1534 store_name = PyUnicode_FromUnicode(base, dot - base);
1535 if (!store_name)
1536 return 0;
1537 }
1538 else {
1539 store_name = name;
1540 Py_INCREF(store_name);
1541 }
1542 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1543 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1544 Py_DECREF(store_name);
1545 return r;
1546 }
1547 else {
1548 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001549 int lineno = st->st_cur->ste_lineno;
1550 int col_offset = st->st_cur->ste_col_offset;
1551 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1552 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1553 Py_DECREF(store_name);
1554 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 }
1556 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1557 Py_DECREF(store_name);
1558 return 1;
1559 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560}
1561
1562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 VISIT(st, expr, lc->target);
1567 VISIT(st, expr, lc->iter);
1568 VISIT_SEQ(st, expr, lc->ifs);
1569 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570}
1571
1572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574symtable_visit_keyword(struct symtable *st, keyword_ty k)
1575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 VISIT(st, expr, k->value);
1577 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578}
1579
1580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582symtable_visit_slice(struct symtable *st, slice_ty s)
1583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 switch (s->kind) {
1585 case Slice_kind:
1586 if (s->v.Slice.lower)
1587 VISIT(st, expr, s->v.Slice.lower)
1588 if (s->v.Slice.upper)
1589 VISIT(st, expr, s->v.Slice.upper)
1590 if (s->v.Slice.step)
1591 VISIT(st, expr, s->v.Slice.step)
1592 break;
1593 case ExtSlice_kind:
1594 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1595 break;
1596 case Index_kind:
1597 VISIT(st, expr, s->v.Index.value)
1598 break;
1599 }
1600 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601}
1602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001604symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001605 identifier scope_name, asdl_seq *generators,
1606 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 int is_generator = (e->kind == GeneratorExp_kind);
1609 int needs_tmp = !is_generator;
1610 comprehension_ty outermost = ((comprehension_ty)
1611 asdl_seq_GET(generators, 0));
1612 /* Outermost iterator is evaluated in current scope */
1613 VISIT(st, expr, outermost->iter);
1614 /* Create comprehension scope for the rest */
1615 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001616 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1617 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 return 0;
1619 }
1620 st->st_cur->ste_generator = is_generator;
1621 /* Outermost iter is received as an argument */
1622 if (!symtable_implicit_arg(st, 0)) {
1623 symtable_exit_block(st, (void *)e);
1624 return 0;
1625 }
1626 /* Allocate temporary name if needed */
1627 if (needs_tmp && !symtable_new_tmpname(st)) {
1628 symtable_exit_block(st, (void *)e);
1629 return 0;
1630 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001631 VISIT(st, expr, outermost->target);
1632 VISIT_SEQ(st, expr, outermost->ifs);
1633 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001635 VISIT(st, expr, value);
1636 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001641symtable_visit_genexp(struct symtable *st, expr_ty e)
1642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1644 e->v.GeneratorExp.generators,
1645 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001646}
1647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001649symtable_visit_listcomp(struct symtable *st, expr_ty e)
1650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1652 e->v.ListComp.generators,
1653 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001654}
1655
1656static int
1657symtable_visit_setcomp(struct symtable *st, expr_ty e)
1658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1660 e->v.SetComp.generators,
1661 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001662}
1663
1664static int
1665symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1668 e->v.DictComp.generators,
1669 e->v.DictComp.key,
1670 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001671}