blob: f75b9c997f495e3b491aba1c9436c381ef0adb8b [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;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000070 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 if (st->st_cur != NULL &&
73 (st->st_cur->ste_nested ||
74 st->st_cur->ste_type == FunctionBlock))
75 ste->ste_nested = 1;
76 ste->ste_child_free = 0;
77 ste->ste_generator = 0;
78 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
81 goto fail;
82
83 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 Py_XDECREF(ste);
86 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000087}
88
89static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000090ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
93 ste->ste_name,
94 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000095}
96
97static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 ste->ste_table = NULL;
101 Py_XDECREF(ste->ste_id);
102 Py_XDECREF(ste->ste_name);
103 Py_XDECREF(ste->ste_symbols);
104 Py_XDECREF(ste->ste_varnames);
105 Py_XDECREF(ste->ste_children);
106 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000107}
108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000110
Guido van Rossum6f799372001-09-20 20:46:19 +0000111static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 {"id", T_OBJECT, OFF(ste_id), READONLY},
113 {"name", T_OBJECT, OFF(ste_name), READONLY},
114 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
115 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
116 {"children", T_OBJECT, OFF(ste_children), READONLY},
117 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
118 {"nested", T_INT, OFF(ste_nested), READONLY},
119 {"type", T_INT, OFF(ste_type), READONLY},
120 {"lineno", T_INT, OFF(ste_lineno), READONLY},
121 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000122};
123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 PyVarObject_HEAD_INIT(&PyType_Type, 0)
126 "symtable entry",
127 sizeof(PySTEntryObject),
128 0,
129 (destructor)ste_dealloc, /* tp_dealloc */
130 0, /* tp_print */
131 0, /* tp_getattr */
132 0, /* tp_setattr */
133 0, /* tp_reserved */
134 (reprfunc)ste_repr, /* tp_repr */
135 0, /* tp_as_number */
136 0, /* tp_as_sequence */
137 0, /* tp_as_mapping */
138 0, /* tp_hash */
139 0, /* tp_call */
140 0, /* tp_str */
141 PyObject_GenericGetAttr, /* tp_getattro */
142 0, /* tp_setattro */
143 0, /* tp_as_buffer */
144 Py_TPFLAGS_DEFAULT, /* tp_flags */
145 0, /* tp_doc */
146 0, /* tp_traverse */
147 0, /* tp_clear */
148 0, /* tp_richcompare */
149 0, /* tp_weaklistoffset */
150 0, /* tp_iter */
151 0, /* tp_iternext */
152 0, /* tp_methods */
153 ste_memberlist, /* tp_members */
154 0, /* tp_getset */
155 0, /* tp_base */
156 0, /* tp_dict */
157 0, /* tp_descr_get */
158 0, /* tp_descr_set */
159 0, /* tp_dictoffset */
160 0, /* tp_init */
161 0, /* tp_alloc */
162 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000163};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164
165static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000166static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000168 _Py_block_ty block, void *ast, int lineno,
169 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000170static int symtable_exit_block(struct symtable *st, void *ast);
171static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
172static int symtable_visit_expr(struct symtable *st, expr_ty s);
173static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000174static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
175static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000176static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177static int symtable_visit_arguments(struct symtable *st, arguments_ty);
178static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
179static int symtable_visit_alias(struct symtable *st, alias_ty);
180static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
181static int symtable_visit_keyword(struct symtable *st, keyword_ty);
182static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000183static int symtable_visit_params(struct symtable *st, asdl_seq *args);
184static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000186static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
188
Nick Coghlan650f0d02007-04-15 12:05:43 +0000189static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
191 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
193#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000197"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
199static struct symtable *
200symtable_new(void)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
205 if (st == NULL)
206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 st->st_filename = NULL;
209 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if ((st->st_stack = PyList_New(0)) == NULL)
212 goto fail;
213 if ((st->st_blocks = PyDict_New()) == NULL)
214 goto fail;
215 st->st_cur = NULL;
216 st->st_private = NULL;
217 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 PySymtable_Free(st);
220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221}
222
223struct symtable *
224PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 struct symtable *st = symtable_new();
227 asdl_seq *seq;
228 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 if (st == NULL)
231 return st;
232 st->st_filename = filename;
233 st->st_future = future;
234 /* Make the initial symbol information gathering pass */
235 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000236 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 PySymtable_Free(st);
238 return NULL;
239 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 st->st_top = st->st_cur;
242 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
243 switch (mod->kind) {
244 case Module_kind:
245 seq = mod->v.Module.body;
246 for (i = 0; i < asdl_seq_LEN(seq); i++)
247 if (!symtable_visit_stmt(st,
248 (stmt_ty)asdl_seq_GET(seq, i)))
249 goto error;
250 break;
251 case Expression_kind:
252 if (!symtable_visit_expr(st, mod->v.Expression.body))
253 goto error;
254 break;
255 case Interactive_kind:
256 seq = mod->v.Interactive.body;
257 for (i = 0; i < asdl_seq_LEN(seq); i++)
258 if (!symtable_visit_stmt(st,
259 (stmt_ty)asdl_seq_GET(seq, i)))
260 goto error;
261 break;
262 case Suite_kind:
263 PyErr_SetString(PyExc_RuntimeError,
264 "this compiler does not handle Suites");
265 goto error;
266 }
267 if (!symtable_exit_block(st, (void *)mod)) {
268 PySymtable_Free(st);
269 return NULL;
270 }
271 /* Make the second symbol analysis pass */
272 if (symtable_analyze(st))
273 return st;
274 PySymtable_Free(st);
275 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000276 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 (void) symtable_exit_block(st, (void *)mod);
278 PySymtable_Free(st);
279 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280}
281
282void
283PySymtable_Free(struct symtable *st)
284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 Py_XDECREF(st->st_blocks);
286 Py_XDECREF(st->st_stack);
287 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288}
289
290PySTEntryObject *
291PySymtable_Lookup(struct symtable *st, void *key)
292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 k = PyLong_FromVoidPtr(key);
296 if (k == NULL)
297 return NULL;
298 v = PyDict_GetItem(st->st_blocks, k);
299 if (v) {
300 assert(PySTEntry_Check(v));
301 Py_INCREF(v);
302 }
303 else {
304 PyErr_SetString(PyExc_KeyError,
305 "unknown symbol table entry");
306 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 Py_DECREF(k);
309 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310}
311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313PyST_GetScope(PySTEntryObject *ste, PyObject *name)
314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
316 if (!v)
317 return 0;
318 assert(PyLong_Check(v));
319 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320}
321
322
323/* Analyze raw symbol information to determine scope of each name.
324
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000325 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000331 explicit global is declared with the global statement. An implicit
332 global is a free variable for which the compiler has found no binding
333 in an enclosing function scope. The implicit global is either a global
334 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
335 to handle these names to implement slightly odd semantics. In such a
336 block, the name is treated as global until it is assigned to; then it
337 is treated as a local.
338
339 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000340 The first pass collects raw facts from the AST via the symtable_visit_*
341 functions: the name is a parameter here, the name is used but not defined
342 here, etc. The second pass analyzes these facts during a pass over the
343 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344
345 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000347 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000348 Names which are explicitly declared nonlocal must exist in this set of
349 visible names - if they do not, a syntax error is raised. After doing
350 the local analysis, it analyzes each of its child blocks using an
351 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352
Nick Coghlan650f0d02007-04-15 12:05:43 +0000353 The children update the free variable set. If a local variable is added to
354 the free variable set by the child, the variable is marked as a cell. The
355 function object being defined must provide runtime storage for the variable
356 that may outlive the function's frame. Cell variables are removed from the
357 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000358
Nick Coghlan650f0d02007-04-15 12:05:43 +0000359 During analysis, the names are:
360 symbols: dict mapping from symbol names to flag values (including offset scope values)
361 scopes: dict mapping from symbol names to scope values (no offset)
362 local: set of all symbol names local to the current scope
363 bound: set of all symbol names local to a containing function scope
364 free: set of all symbol names referenced but not bound in child scopes
365 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366*/
367
368#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 PyObject *o = PyLong_FromLong(I); \
370 if (!o) \
371 return 0; \
372 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
373 Py_DECREF(o); \
374 return 0; \
375 } \
376 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377}
378
379/* Decide on scope of name, given flags.
380
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000381 The namespace dictionaries may be modified to record information
382 about the new name. For example, a new global will add an entry to
383 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000384*/
385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000387analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 PyObject *bound, PyObject *local, PyObject *free,
389 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (flags & DEF_GLOBAL) {
392 if (flags & DEF_PARAM) {
393 PyErr_Format(PyExc_SyntaxError,
394 "name '%U' is parameter and global",
395 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000396 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
397 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398
399 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000400 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 if (flags & DEF_NONLOCAL) {
402 PyErr_Format(PyExc_SyntaxError,
403 "name '%U' is nonlocal and global",
404 name);
405 return 0;
406 }
407 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
408 if (PySet_Add(global, name) < 0)
409 return 0;
410 if (bound && (PySet_Discard(bound, name) < 0))
411 return 0;
412 return 1;
413 }
414 if (flags & DEF_NONLOCAL) {
415 if (flags & DEF_PARAM) {
416 PyErr_Format(PyExc_SyntaxError,
417 "name '%U' is parameter and nonlocal",
418 name);
419 return 0;
420 }
421 if (!bound) {
422 PyErr_Format(PyExc_SyntaxError,
423 "nonlocal declaration not allowed at module level");
424 return 0;
425 }
426 if (!PySet_Contains(bound, name)) {
427 PyErr_Format(PyExc_SyntaxError,
428 "no binding for nonlocal '%U' found",
429 name);
430
431 return 0;
432 }
433 SET_SCOPE(scopes, name, FREE);
434 ste->ste_free = 1;
435 return PySet_Add(free, name) >= 0;
436 }
437 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000438 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (PySet_Add(local, name) < 0)
440 return 0;
441 if (PySet_Discard(global, name) < 0)
442 return 0;
443 return 1;
444 }
445 /* If an enclosing block has a binding for this name, it
446 is a free variable rather than a global variable.
447 Note that having a non-NULL bound implies that the block
448 is nested.
449 */
450 if (bound && PySet_Contains(bound, name)) {
451 SET_SCOPE(scopes, name, FREE);
452 ste->ste_free = 1;
453 return PySet_Add(free, name) >= 0;
454 }
455 /* If a parent has a global statement, then call it global
456 explicit? It could also be global implicit.
457 */
458 if (global && PySet_Contains(global, name)) {
459 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
460 return 1;
461 }
462 if (ste->ste_nested)
463 ste->ste_free = 1;
464 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
465 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466}
467
468#undef SET_SCOPE
469
470/* If a name is defined in free and also in locals, then this block
471 provides the binding for the free variable. The name should be
472 marked CELL in this block and removed from the free list.
473
474 Note that the current block's free variables are included in free.
475 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000476
Martin v. Löwis2673a572007-10-29 19:54:24 +0000477 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000478 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479*/
480
481static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000482analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 PyObject *name, *v, *v_cell;
485 int success = 0;
486 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 v_cell = PyLong_FromLong(CELL);
489 if (!v_cell)
490 return 0;
491 while (PyDict_Next(scopes, &pos, &name, &v)) {
492 long scope;
493 assert(PyLong_Check(v));
494 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000495 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 continue;
497 if (!PySet_Contains(free, name))
498 continue;
499 if (restricted != NULL &&
500 PyUnicode_CompareWithASCIIString(name, restricted))
501 continue;
502 /* Replace LOCAL with CELL for this name, and remove
503 from free. It is safe to replace the value of name
504 in the dict, because it will not cause a resize.
505 */
506 if (PyDict_SetItem(scopes, name, v_cell) < 0)
507 goto error;
508 if (PySet_Discard(free, name) < 0)
509 goto error;
510 }
511 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 Py_DECREF(v_cell);
514 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515}
516
517/* Check for illegal statements in unoptimized namespaces */
518static int
519check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
523 || !(ste->ste_free || ste->ste_child_free))
524 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 trailer = (ste->ste_child_free ?
527 "contains a nested function with free variables" :
528 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 switch (ste->ste_unoptimized) {
531 case OPT_TOPLEVEL: /* import * at top-level is fine */
532 return 1;
533 case OPT_IMPORT_STAR:
534 PyErr_Format(PyExc_SyntaxError,
535 "import * is not allowed in function '%U' because it %s",
536 ste->ste_name, trailer);
537 break;
538 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000540 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
541 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543}
544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545/* Enter the final scope information into the ste_symbols dict.
546 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 * All arguments are dicts. Modifies symbols, others are read-only.
548*/
549static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 PyObject *name = NULL, *itr = NULL;
554 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
555 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* Update scope information for all symbols in this scope */
558 while (PyDict_Next(symbols, &pos, &name, &v)) {
559 long scope, flags;
560 assert(PyLong_Check(v));
561 flags = PyLong_AS_LONG(v);
562 v_scope = PyDict_GetItem(scopes, name);
563 assert(v_scope && PyLong_Check(v_scope));
564 scope = PyLong_AS_LONG(v_scope);
565 flags |= (scope << SCOPE_OFFSET);
566 v_new = PyLong_FromLong(flags);
567 if (!v_new)
568 return 0;
569 if (PyDict_SetItem(symbols, name, v_new) < 0) {
570 Py_DECREF(v_new);
571 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 Py_DECREF(v_new);
574 }
575
576 /* Record not yet resolved free variables from children (if any) */
577 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
578 if (!v_free)
579 return 0;
580
581 itr = PyObject_GetIter(free);
582 if (!itr)
583 goto error;
584
585 while ((name = PyIter_Next(itr))) {
586 v = PyDict_GetItem(symbols, name);
587
588 /* Handle symbol that already exists in this scope */
589 if (v) {
590 /* Handle a free variable in a method of
591 the class that has the same name as a local
592 or global in the class scope.
593 */
594 if (classflag &&
595 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
596 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
597 v_new = PyLong_FromLong(flags);
598 if (!v_new) {
599 goto error;
600 }
601 if (PyDict_SetItem(symbols, name, v_new) < 0) {
602 Py_DECREF(v_new);
603 goto error;
604 }
605 Py_DECREF(v_new);
606 }
607 /* It's a cell, or already free in this scope */
608 Py_DECREF(name);
609 continue;
610 }
611 /* Handle global symbol */
612 if (!PySet_Contains(bound, name)) {
613 Py_DECREF(name);
614 continue; /* it's a global */
615 }
616 /* Propagate new free symbol up the lexical stack */
617 if (PyDict_SetItem(symbols, name, v_free) < 0) {
618 goto error;
619 }
620 Py_DECREF(name);
621 }
622 Py_DECREF(itr);
623 Py_DECREF(v_free);
624 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000625error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 Py_XDECREF(v_free);
627 Py_XDECREF(itr);
628 Py_XDECREF(name);
629 return 0;
630}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631
632/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000633
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 Arguments:
635 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000636 bound -- set of variables bound in enclosing scopes (input). bound
637 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 free -- set of free variables in enclosed scopes (output)
639 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000640
641 The implementation uses two mutually recursive functions,
642 analyze_block() and analyze_child_block(). analyze_block() is
643 responsible for analyzing the individual names defined in a block.
644 analyze_child_block() prepares temporary namespace dictionaries
645 used to evaluated nested blocks.
646
647 The two functions exist because a child block should see the name
648 bindings of its enclosing blocks, but those bindings should not
649 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650*/
651
652static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
654 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000655
656static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
658 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
661 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
662 PyObject *temp;
663 int i, success = 0;
664 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 local = PySet_New(NULL); /* collect new names bound in block */
667 if (!local)
668 goto error;
669 scopes = PyDict_New(); /* collect scopes defined for each name */
670 if (!scopes)
671 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 /* Allocate new global and bound variable dictionaries. These
674 dictionaries hold the names visible in nested blocks. For
675 ClassBlocks, the bound and global names are initialized
676 before analyzing names, because class bindings aren't
677 visible in methods. For other blocks, they are initialized
678 after names are analyzed.
679 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 /* TODO(jhylton): Package these dicts in a struct so that we
682 can write reasonable helper functions?
683 */
684 newglobal = PySet_New(NULL);
685 if (!newglobal)
686 goto error;
687 newfree = PySet_New(NULL);
688 if (!newfree)
689 goto error;
690 newbound = PySet_New(NULL);
691 if (!newbound)
692 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 /* Class namespace has no effect on names visible in
695 nested functions, so populate the global and bound
696 sets to be passed to child blocks before analyzing
697 this one.
698 */
699 if (ste->ste_type == ClassBlock) {
700 /* Pass down known globals */
701 temp = PyNumber_InPlaceOr(newglobal, global);
702 if (!temp)
703 goto error;
704 Py_DECREF(temp);
705 /* Pass down previously bound symbols */
706 if (bound) {
707 temp = PyNumber_InPlaceOr(newbound, bound);
708 if (!temp)
709 goto error;
710 Py_DECREF(temp);
711 }
712 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
715 long flags = PyLong_AS_LONG(v);
716 if (!analyze_name(ste, scopes, name, flags,
717 bound, local, free, global))
718 goto error;
719 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 /* Populate global and bound sets to be passed to children. */
722 if (ste->ste_type != ClassBlock) {
723 /* Add function locals to bound set */
724 if (ste->ste_type == FunctionBlock) {
725 temp = PyNumber_InPlaceOr(newbound, local);
726 if (!temp)
727 goto error;
728 Py_DECREF(temp);
729 }
730 /* Pass down previously bound symbols */
731 if (bound) {
732 temp = PyNumber_InPlaceOr(newbound, bound);
733 if (!temp)
734 goto error;
735 Py_DECREF(temp);
736 }
737 /* Pass down known globals */
738 temp = PyNumber_InPlaceOr(newglobal, global);
739 if (!temp)
740 goto error;
741 Py_DECREF(temp);
742 }
743 else {
744 /* Special-case __class__ */
745 if (!GET_IDENTIFIER(__class__))
746 goto error;
747 assert(PySet_Contains(local, __class__) == 1);
748 if (PySet_Add(newbound, __class__) < 0)
749 goto error;
750 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 /* Recursively call analyze_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 newbound, newglobal now contain the names visible in
755 nested blocks. The free variables in the children will
756 be collected in allfree.
757 */
758 allfree = PySet_New(NULL);
759 if (!allfree)
760 goto error;
761 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
762 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
763 PySTEntryObject* entry;
764 assert(c && PySTEntry_Check(c));
765 entry = (PySTEntryObject*)c;
766 if (!analyze_child_block(entry, newbound, newfree, newglobal,
767 allfree))
768 goto error;
769 /* Check if any children have free variables */
770 if (entry->ste_free || entry->ste_child_free)
771 ste->ste_child_free = 1;
772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 temp = PyNumber_InPlaceOr(newfree, allfree);
775 if (!temp)
776 goto error;
777 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 /* Check if any local variables must be converted to cell variables */
780 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
781 NULL))
782 goto error;
783 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
784 "__class__"))
785 goto error;
786 /* Records the results of the analysis in the symbol table entry */
787 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
788 ste->ste_type == ClassBlock))
789 goto error;
790 if (!check_unoptimized(ste))
791 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 temp = PyNumber_InPlaceOr(free, newfree);
794 if (!temp)
795 goto error;
796 Py_DECREF(temp);
797 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 Py_XDECREF(scopes);
800 Py_XDECREF(local);
801 Py_XDECREF(newbound);
802 Py_XDECREF(newglobal);
803 Py_XDECREF(newfree);
804 Py_XDECREF(allfree);
805 if (!success)
806 assert(PyErr_Occurred());
807 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808}
809
810static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
812 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
815 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 These dictionary are used by all blocks enclosed by the
820 current block. The analyze_block() call modifies these
821 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 */
824 temp_bound = PySet_New(bound);
825 if (!temp_bound)
826 goto error;
827 temp_free = PySet_New(free);
828 if (!temp_free)
829 goto error;
830 temp_global = PySet_New(global);
831 if (!temp_global)
832 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
835 goto error;
836 temp = PyNumber_InPlaceOr(child_free, temp_free);
837 if (!temp)
838 goto error;
839 Py_DECREF(temp);
840 Py_DECREF(temp_bound);
841 Py_DECREF(temp_free);
842 Py_DECREF(temp_global);
843 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000844 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 Py_XDECREF(temp_bound);
846 Py_XDECREF(temp_free);
847 Py_XDECREF(temp_global);
848 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000849}
850
851static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852symtable_analyze(struct symtable *st)
853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyObject *free, *global;
855 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 free = PySet_New(NULL);
858 if (!free)
859 return 0;
860 global = PySet_New(NULL);
861 if (!global) {
862 Py_DECREF(free);
863 return 0;
864 }
865 r = analyze_block(st->st_top, NULL, free, global);
866 Py_DECREF(free);
867 Py_DECREF(global);
868 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869}
870
871
872static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000873symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
876 lineno, NULL, NULL) < 0) {
877 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
878 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000879 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
880 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 }
882 return 0;
883 }
884 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885}
886
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000887/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 This reference is released when the block is exited, via the DECREF
889 in symtable_exit_block().
890*/
891
892static int
893symtable_exit_block(struct symtable *st, void *ast)
894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 Py_CLEAR(st->st_cur);
898 end = PyList_GET_SIZE(st->st_stack) - 1;
899 if (end >= 0) {
900 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
901 end);
902 if (st->st_cur == NULL)
903 return 0;
904 Py_INCREF(st->st_cur);
905 if (PySequence_DelItem(st->st_stack, end) < 0)
906 return 0;
907 }
908 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909}
910
911static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000913 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 if (st->st_cur) {
918 prev = st->st_cur;
919 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
920 return 0;
921 }
922 Py_DECREF(st->st_cur);
923 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000924 st->st_cur = ste_new(st, name, block, ast, lineno, col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 if (st->st_cur == NULL)
926 return 0;
927 if (name == GET_IDENTIFIER(top))
928 st->st_global = st->st_cur->ste_symbols;
929 if (prev) {
930 if (PyList_Append(prev->ste_children,
931 (PyObject *)st->st_cur) < 0) {
932 return 0;
933 }
934 }
935 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936}
937
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000938static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939symtable_lookup(struct symtable *st, PyObject *name)
940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyObject *o;
942 PyObject *mangled = _Py_Mangle(st->st_private, name);
943 if (!mangled)
944 return 0;
945 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
946 Py_DECREF(mangled);
947 if (!o)
948 return 0;
949 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950}
951
952static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 PyObject *o;
956 PyObject *dict;
957 long val;
958 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959
Jeremy Hylton81e95022007-02-27 06:50:52 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 if (!mangled)
962 return 0;
963 dict = st->st_cur->ste_symbols;
964 if ((o = PyDict_GetItem(dict, mangled))) {
965 val = PyLong_AS_LONG(o);
966 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
967 /* Is it better to use 'mangled' or 'name' here? */
968 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000969 PyErr_SyntaxLocationEx(st->st_filename,
970 st->st_cur->ste_lineno,
971 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 goto error;
973 }
974 val |= flag;
975 } else
976 val = flag;
977 o = PyLong_FromLong(val);
978 if (o == NULL)
979 goto error;
980 if (PyDict_SetItem(dict, mangled, o) < 0) {
981 Py_DECREF(o);
982 goto error;
983 }
984 Py_DECREF(o);
985
986 if (flag & DEF_PARAM) {
987 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
988 goto error;
989 } else if (flag & DEF_GLOBAL) {
990 /* XXX need to update DEF_GLOBAL for other flags too;
991 perhaps only DEF_FREE_GLOBAL */
992 val = flag;
993 if ((o = PyDict_GetItem(st->st_global, mangled))) {
994 val |= PyLong_AS_LONG(o);
995 }
996 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 goto error;
999 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1000 Py_DECREF(o);
1001 goto error;
1002 }
1003 Py_DECREF(o);
1004 }
1005 Py_DECREF(mangled);
1006 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001007
1008error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_DECREF(mangled);
1010 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011}
1012
1013/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1014 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 function.
1016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1018 useful if the first node in the sequence requires special treatment.
1019*/
1020
1021#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 if (!symtable_visit_ ## TYPE((ST), (V))) \
1023 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001024
1025#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1027 symtable_exit_block((ST), (S)); \
1028 return 0; \
1029 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001030
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 int i; \
1033 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1034 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1035 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1036 if (!symtable_visit_ ## TYPE((ST), elt)) \
1037 return 0; \
1038 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001040
1041#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 int i; \
1043 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1044 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1045 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1046 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1047 symtable_exit_block((ST), (S)); \
1048 return 0; \
1049 } \
1050 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001051}
1052
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 int i; \
1055 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1056 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1057 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1058 if (!symtable_visit_ ## TYPE((ST), elt)) \
1059 return 0; \
1060 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001062
1063#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 int i; \
1065 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1066 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1067 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1068 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1069 symtable_exit_block((ST), (S)); \
1070 return 0; \
1071 } \
1072 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001073}
1074
Guido van Rossum4f72a782006-10-27 23:31:49 +00001075#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 int i = 0; \
1077 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1078 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1079 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1080 if (!elt) continue; /* can be NULL */ \
1081 if (!symtable_visit_expr((ST), elt)) \
1082 return 0; \
1083 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001084}
1085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001087symtable_new_tmpname(struct symtable *st)
1088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 char tmpname[256];
1090 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1093 ++st->st_cur->ste_tmpname);
1094 tmp = PyUnicode_InternFromString(tmpname);
1095 if (!tmp)
1096 return 0;
1097 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1098 return 0;
1099 Py_DECREF(tmp);
1100 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001101}
1102
Guido van Rossum4f72a782006-10-27 23:31:49 +00001103
Guido van Rossumc2e20742006-02-27 22:32:47 +00001104static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105symtable_visit_stmt(struct symtable *st, stmt_ty s)
1106{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 switch (s->kind) {
1108 case FunctionDef_kind:
1109 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1110 return 0;
1111 if (s->v.FunctionDef.args->defaults)
1112 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1113 if (s->v.FunctionDef.args->kw_defaults)
1114 VISIT_KWONLYDEFAULTS(st,
1115 s->v.FunctionDef.args->kw_defaults);
1116 if (!symtable_visit_annotations(st, s))
1117 return 0;
1118 if (s->v.FunctionDef.decorator_list)
1119 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1120 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001121 FunctionBlock, (void *)s, s->lineno,
1122 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 return 0;
1124 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1125 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1126 if (!symtable_exit_block(st, s))
1127 return 0;
1128 break;
1129 case ClassDef_kind: {
1130 PyObject *tmp;
1131 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1132 return 0;
1133 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1134 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1135 if (s->v.ClassDef.starargs)
1136 VISIT(st, expr, s->v.ClassDef.starargs);
1137 if (s->v.ClassDef.kwargs)
1138 VISIT(st, expr, s->v.ClassDef.kwargs);
1139 if (s->v.ClassDef.decorator_list)
1140 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1141 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001142 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 return 0;
1144 if (!GET_IDENTIFIER(__class__) ||
1145 !symtable_add_def(st, __class__, DEF_LOCAL) ||
1146 !GET_IDENTIFIER(__locals__) ||
1147 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1148 symtable_exit_block(st, s);
1149 return 0;
1150 }
1151 tmp = st->st_private;
1152 st->st_private = s->v.ClassDef.name;
1153 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1154 st->st_private = tmp;
1155 if (!symtable_exit_block(st, s))
1156 return 0;
1157 break;
1158 }
1159 case Return_kind:
1160 if (s->v.Return.value) {
1161 VISIT(st, expr, s->v.Return.value);
1162 st->st_cur->ste_returns_value = 1;
1163 if (st->st_cur->ste_generator) {
1164 PyErr_SetString(PyExc_SyntaxError,
1165 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001166 PyErr_SyntaxLocationEx(st->st_filename,
1167 s->lineno,
1168 s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00001170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 }
1172 break;
1173 case Delete_kind:
1174 VISIT_SEQ(st, expr, s->v.Delete.targets);
1175 break;
1176 case Assign_kind:
1177 VISIT_SEQ(st, expr, s->v.Assign.targets);
1178 VISIT(st, expr, s->v.Assign.value);
1179 break;
1180 case AugAssign_kind:
1181 VISIT(st, expr, s->v.AugAssign.target);
1182 VISIT(st, expr, s->v.AugAssign.value);
1183 break;
1184 case For_kind:
1185 VISIT(st, expr, s->v.For.target);
1186 VISIT(st, expr, s->v.For.iter);
1187 VISIT_SEQ(st, stmt, s->v.For.body);
1188 if (s->v.For.orelse)
1189 VISIT_SEQ(st, stmt, s->v.For.orelse);
1190 break;
1191 case While_kind:
1192 VISIT(st, expr, s->v.While.test);
1193 VISIT_SEQ(st, stmt, s->v.While.body);
1194 if (s->v.While.orelse)
1195 VISIT_SEQ(st, stmt, s->v.While.orelse);
1196 break;
1197 case If_kind:
1198 /* XXX if 0: and lookup_yield() hacks */
1199 VISIT(st, expr, s->v.If.test);
1200 VISIT_SEQ(st, stmt, s->v.If.body);
1201 if (s->v.If.orelse)
1202 VISIT_SEQ(st, stmt, s->v.If.orelse);
1203 break;
1204 case Raise_kind:
1205 if (s->v.Raise.exc) {
1206 VISIT(st, expr, s->v.Raise.exc);
1207 if (s->v.Raise.cause) {
1208 VISIT(st, expr, s->v.Raise.cause);
1209 }
1210 }
1211 break;
1212 case TryExcept_kind:
1213 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1214 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1215 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1216 break;
1217 case TryFinally_kind:
1218 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1219 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1220 break;
1221 case Assert_kind:
1222 VISIT(st, expr, s->v.Assert.test);
1223 if (s->v.Assert.msg)
1224 VISIT(st, expr, s->v.Assert.msg);
1225 break;
1226 case Import_kind:
1227 VISIT_SEQ(st, alias, s->v.Import.names);
1228 /* XXX Don't have the lineno available inside
1229 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001230 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001232 st->st_cur->ste_opt_col_offset = s->col_offset;
1233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 break;
1235 case ImportFrom_kind:
1236 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1237 /* XXX Don't have the lineno available inside
1238 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001239 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001241 st->st_cur->ste_opt_col_offset = s->col_offset;
1242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 break;
1244 case Global_kind: {
1245 int i;
1246 asdl_seq *seq = s->v.Global.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 GLOBAL_AFTER_ASSIGN,
1258 c_name);
1259 else
1260 PyOS_snprintf(buf, sizeof(buf),
1261 GLOBAL_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_GLOBAL))
1267 return 0;
1268 }
1269 break;
1270 }
1271 case Nonlocal_kind: {
1272 int i;
1273 asdl_seq *seq = s->v.Nonlocal.names;
1274 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1275 identifier name = (identifier)asdl_seq_GET(seq, i);
1276 char *c_name = _PyUnicode_AsString(name);
1277 long cur = symtable_lookup(st, name);
1278 if (cur < 0)
1279 return 0;
1280 if (cur & (DEF_LOCAL | USE)) {
1281 char buf[256];
1282 if (cur & DEF_LOCAL)
1283 PyOS_snprintf(buf, sizeof(buf),
1284 NONLOCAL_AFTER_ASSIGN,
1285 c_name);
1286 else
1287 PyOS_snprintf(buf, sizeof(buf),
1288 NONLOCAL_AFTER_USE,
1289 c_name);
1290 if (!symtable_warn(st, buf, s->lineno))
1291 return 0;
1292 }
1293 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1294 return 0;
1295 }
1296 break;
1297 }
1298 case Expr_kind:
1299 VISIT(st, expr, s->v.Expr.value);
1300 break;
1301 case Pass_kind:
1302 case Break_kind:
1303 case Continue_kind:
1304 /* nothing to do here */
1305 break;
1306 case With_kind:
1307 VISIT(st, expr, s->v.With.context_expr);
1308 if (s->v.With.optional_vars) {
1309 VISIT(st, expr, s->v.With.optional_vars);
1310 }
1311 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")) {
1424 if (!GET_IDENTIFIER(__class__) ||
1425 !symtable_add_def(st, __class__, USE))
1426 return 0;
1427 }
1428 break;
1429 /* child nodes of List and Tuple will have expr_context set */
1430 case List_kind:
1431 VISIT_SEQ(st, expr, e->v.List.elts);
1432 break;
1433 case Tuple_kind:
1434 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1435 break;
1436 }
1437 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438}
1439
1440static int
1441symtable_implicit_arg(struct symtable *st, int pos)
1442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1444 if (id == NULL)
1445 return 0;
1446 if (!symtable_add_def(st, id, DEF_PARAM)) {
1447 Py_DECREF(id);
1448 return 0;
1449 }
1450 Py_DECREF(id);
1451 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452}
1453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001455symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 if (!args)
1460 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 for (i = 0; i < asdl_seq_LEN(args); i++) {
1463 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1464 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1465 return 0;
1466 }
1467
1468 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469}
1470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001472symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 if (!args)
1477 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 for (i = 0; i < asdl_seq_LEN(args); i++) {
1480 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1481 if (arg->annotation)
1482 VISIT(st, expr, arg->annotation);
1483 }
1484
1485 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001486}
1487
Neal Norwitzc1505362006-12-28 06:47:50 +00001488static int
1489symtable_visit_annotations(struct symtable *st, stmt_ty s)
1490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 arguments_ty a = s->v.FunctionDef.args;
1492
1493 if (a->args && !symtable_visit_argannotations(st, a->args))
1494 return 0;
1495 if (a->varargannotation)
1496 VISIT(st, expr, a->varargannotation);
1497 if (a->kwargannotation)
1498 VISIT(st, expr, a->kwargannotation);
1499 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1500 return 0;
1501 if (s->v.FunctionDef.returns)
1502 VISIT(st, expr, s->v.FunctionDef.returns);
1503 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504}
1505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507symtable_visit_arguments(struct symtable *st, arguments_ty a)
1508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 /* skip default arguments inside function block
1510 XXX should ast be different?
1511 */
1512 if (a->args && !symtable_visit_params(st, a->args))
1513 return 0;
1514 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1515 return 0;
1516 if (a->vararg) {
1517 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1518 return 0;
1519 st->st_cur->ste_varargs = 1;
1520 }
1521 if (a->kwarg) {
1522 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1523 return 0;
1524 st->st_cur->ste_varkeywords = 1;
1525 }
1526 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527}
1528
1529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 if (eh->v.ExceptHandler.type)
1534 VISIT(st, expr, eh->v.ExceptHandler.type);
1535 if (eh->v.ExceptHandler.name)
1536 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1537 return 0;
1538 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1539 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540}
1541
1542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544symtable_visit_alias(struct symtable *st, alias_ty a)
1545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001547 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 dotted package name (e.g. spam.eggs)
1549 */
1550 PyObject *store_name;
1551 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1552 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1553 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
1554 if (dot) {
1555 store_name = PyUnicode_FromUnicode(base, dot - base);
1556 if (!store_name)
1557 return 0;
1558 }
1559 else {
1560 store_name = name;
1561 Py_INCREF(store_name);
1562 }
1563 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1564 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1565 Py_DECREF(store_name);
1566 return r;
1567 }
1568 else {
1569 if (st->st_cur->ste_type != ModuleBlock) {
1570 int lineno = st->st_cur->ste_lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001571 int col_offset = st->st_cur->ste_col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001573 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 Py_DECREF(store_name);
1575 return 0;
1576 }
1577 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1578 Py_DECREF(store_name);
1579 return 1;
1580 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581}
1582
1583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 VISIT(st, expr, lc->target);
1588 VISIT(st, expr, lc->iter);
1589 VISIT_SEQ(st, expr, lc->ifs);
1590 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591}
1592
1593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595symtable_visit_keyword(struct symtable *st, keyword_ty k)
1596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 VISIT(st, expr, k->value);
1598 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599}
1600
1601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603symtable_visit_slice(struct symtable *st, slice_ty s)
1604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 switch (s->kind) {
1606 case Slice_kind:
1607 if (s->v.Slice.lower)
1608 VISIT(st, expr, s->v.Slice.lower)
1609 if (s->v.Slice.upper)
1610 VISIT(st, expr, s->v.Slice.upper)
1611 if (s->v.Slice.step)
1612 VISIT(st, expr, s->v.Slice.step)
1613 break;
1614 case ExtSlice_kind:
1615 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1616 break;
1617 case Index_kind:
1618 VISIT(st, expr, s->v.Index.value)
1619 break;
1620 }
1621 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622}
1623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001625symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001626 identifier scope_name, asdl_seq *generators,
1627 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 int is_generator = (e->kind == GeneratorExp_kind);
1630 int needs_tmp = !is_generator;
1631 comprehension_ty outermost = ((comprehension_ty)
1632 asdl_seq_GET(generators, 0));
1633 /* Outermost iterator is evaluated in current scope */
1634 VISIT(st, expr, outermost->iter);
1635 /* Create comprehension scope for the rest */
1636 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001637 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1638 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 return 0;
1640 }
1641 st->st_cur->ste_generator = is_generator;
1642 /* Outermost iter is received as an argument */
1643 if (!symtable_implicit_arg(st, 0)) {
1644 symtable_exit_block(st, (void *)e);
1645 return 0;
1646 }
1647 /* Allocate temporary name if needed */
1648 if (needs_tmp && !symtable_new_tmpname(st)) {
1649 symtable_exit_block(st, (void *)e);
1650 return 0;
1651 }
1652 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1653 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1654 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1655 generators, 1, (void*)e);
1656 if (value)
1657 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1658 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1659 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001663symtable_visit_genexp(struct symtable *st, expr_ty e)
1664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1666 e->v.GeneratorExp.generators,
1667 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001668}
1669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001671symtable_visit_listcomp(struct symtable *st, expr_ty e)
1672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1674 e->v.ListComp.generators,
1675 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001676}
1677
1678static int
1679symtable_visit_setcomp(struct symtable *st, expr_ty e)
1680{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1682 e->v.SetComp.generators,
1683 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001684}
1685
1686static int
1687symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1690 e->v.DictComp.generators,
1691 e->v.DictComp.key,
1692 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001693}