blob: 376ad2c45aa56bf1a310ce5af2b46f0429130800 [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,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000028 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000029{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000030 PySTEntryObject *ste = NULL;
31 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000032
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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;
41 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000043 ste->ste_name = name;
44 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000045
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000046 ste->ste_symbols = NULL;
47 ste->ste_varnames = NULL;
48 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000049
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000050 ste->ste_symbols = PyDict_New();
51 if (ste->ste_symbols == NULL)
52 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000053
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000054 ste->ste_varnames = PyList_New(0);
55 if (ste->ste_varnames == NULL)
56 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000057
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000058 ste->ste_children = PyList_New(0);
59 if (ste->ste_children == NULL)
60 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000061
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000062 ste->ste_type = block;
63 ste->ste_unoptimized = 0;
64 ste->ste_nested = 0;
65 ste->ste_free = 0;
66 ste->ste_varargs = 0;
67 ste->ste_varkeywords = 0;
68 ste->ste_opt_lineno = 0;
69 ste->ste_tmpname = 0;
Benjamin Peterson4d3d08f2010-10-20 21:35:05 +000070 ste->ste_tmpname = 0;
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000071 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000072
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000168static int symtable_enter_block(struct symtable *st, identifier name,
169 _Py_block_ty block, void *ast, int lineno);
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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000202 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000208 st->st_filename = NULL;
209 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000210
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000226 struct symtable *st = symtable_new();
227 asdl_seq *seq;
228 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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) ||
236 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
237 PySymtable_Free(st);
238 return NULL;
239 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000240
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000293 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000308 Py_DECREF(k);
309 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310}
311
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000312int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313PyST_GetScope(PySTEntryObject *ste, PyObject *name)
314{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000328 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000386static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000387analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000388 PyObject *bound, PyObject *local, PyObject *free,
389 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000391 if (flags & DEF_GLOBAL) {
392 if (flags & DEF_PARAM) {
393 PyErr_Format(PyExc_SyntaxError,
394 "name '%U' is parameter and global",
395 name);
396 PyErr_SyntaxLocation(ste->ste_table->st_filename,
397 ste->ste_lineno);
398
399 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000400 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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) {
438 SET_SCOPE(scopes, name, LOCAL);
439 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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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);
495 if (scope != LOCAL)
496 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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000520 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000540 PyErr_SyntaxLocation(ste->ste_table->st_filename,
541 ste->ste_opt_lineno);
542 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543}
544
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000657analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
658 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000752 /* Recursively call analyze_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000753
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000811analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
812 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000813{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000814 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
815 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000816
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000817 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000818
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000854 PyObject *free, *global;
855 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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);
879 PyErr_SyntaxLocation(st->st_filename,
880 st->st_cur->ste_lineno);
881 }
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 Pitrou7f14f0d2010-05-09 16:14:21 +0000895 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000912symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
913 void *ast, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000915 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 }
924 st->st_cur = ste_new(st, name, block, ast, lineno);
925 if (st->st_cur == NULL)
926 return 0;
Benjamin Petersone9633492010-10-16 03:49:22 +0000927 if (block == ModuleBlock)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000928 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 Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +0000953symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +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 Pitrou7f14f0d2010-05-09 16:14:21 +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);
969 PyErr_SyntaxLocation(st->st_filename,
970 st->st_cur->ste_lineno);
971 goto error;
972 }
973 val |= flag;
974 } else
975 val = flag;
976 o = PyLong_FromLong(val);
977 if (o == NULL)
978 goto error;
979 if (PyDict_SetItem(dict, mangled, o) < 0) {
980 Py_DECREF(o);
981 goto error;
982 }
983 Py_DECREF(o);
984
985 if (flag & DEF_PARAM) {
986 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
987 goto error;
988 } else if (flag & DEF_GLOBAL) {
989 /* XXX need to update DEF_GLOBAL for other flags too;
990 perhaps only DEF_FREE_GLOBAL */
991 val = flag;
992 if ((o = PyDict_GetItem(st->st_global, mangled))) {
993 val |= PyLong_AS_LONG(o);
994 }
995 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 if (o == NULL)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000997 goto error;
998 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
999 Py_DECREF(o);
1000 goto error;
1001 }
1002 Py_DECREF(o);
1003 }
1004 Py_DECREF(mangled);
1005 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001006
1007error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001008 Py_DECREF(mangled);
1009 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010}
1011
1012/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1013 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001014 function.
1015
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1017 useful if the first node in the sequence requires special treatment.
1018*/
1019
1020#define VISIT(ST, TYPE, V) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001021 if (!symtable_visit_ ## TYPE((ST), (V))) \
1022 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001023
1024#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001025 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1026 symtable_exit_block((ST), (S)); \
1027 return 0; \
1028 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001029
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001031 int i; \
1032 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1033 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1034 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1035 if (!symtable_visit_ ## TYPE((ST), elt)) \
1036 return 0; \
1037 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001039
1040#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001041 int i; \
1042 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1043 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1044 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1045 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1046 symtable_exit_block((ST), (S)); \
1047 return 0; \
1048 } \
1049 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001050}
1051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001053 int i; \
1054 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1055 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1056 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1057 if (!symtable_visit_ ## TYPE((ST), elt)) \
1058 return 0; \
1059 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001061
1062#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001063 int i; \
1064 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1065 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1066 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1067 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1068 symtable_exit_block((ST), (S)); \
1069 return 0; \
1070 } \
1071 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001072}
1073
Guido van Rossum4f72a782006-10-27 23:31:49 +00001074#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001075 int i = 0; \
1076 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1077 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1078 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1079 if (!elt) continue; /* can be NULL */ \
1080 if (!symtable_visit_expr((ST), elt)) \
1081 return 0; \
1082 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001083}
1084
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001086symtable_new_tmpname(struct symtable *st)
1087{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001088 char tmpname[256];
1089 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001090
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001091 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1092 ++st->st_cur->ste_tmpname);
1093 tmp = PyUnicode_InternFromString(tmpname);
1094 if (!tmp)
1095 return 0;
1096 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1097 return 0;
1098 Py_DECREF(tmp);
1099 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001100}
1101
Guido van Rossum4f72a782006-10-27 23:31:49 +00001102
1103
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 Pitrou7f14f0d2010-05-09 16:14:21 +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,
1121 FunctionBlock, (void *)s, s->lineno))
1122 return 0;
1123 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1124 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1125 if (!symtable_exit_block(st, s))
1126 return 0;
1127 break;
1128 case ClassDef_kind: {
1129 PyObject *tmp;
1130 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1131 return 0;
1132 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1133 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1134 if (s->v.ClassDef.starargs)
1135 VISIT(st, expr, s->v.ClassDef.starargs);
1136 if (s->v.ClassDef.kwargs)
1137 VISIT(st, expr, s->v.ClassDef.kwargs);
1138 if (s->v.ClassDef.decorator_list)
1139 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1140 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1141 (void *)s, s->lineno))
1142 return 0;
1143 if (!GET_IDENTIFIER(__class__) ||
1144 !symtable_add_def(st, __class__, DEF_LOCAL) ||
1145 !GET_IDENTIFIER(__locals__) ||
1146 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1147 symtable_exit_block(st, s);
1148 return 0;
1149 }
1150 tmp = st->st_private;
1151 st->st_private = s->v.ClassDef.name;
1152 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1153 st->st_private = tmp;
1154 if (!symtable_exit_block(st, s))
1155 return 0;
1156 break;
1157 }
1158 case Return_kind:
1159 if (s->v.Return.value) {
1160 VISIT(st, expr, s->v.Return.value);
1161 st->st_cur->ste_returns_value = 1;
1162 if (st->st_cur->ste_generator) {
1163 PyErr_SetString(PyExc_SyntaxError,
1164 RETURN_VAL_IN_GENERATOR);
1165 PyErr_SyntaxLocation(st->st_filename,
1166 s->lineno);
1167 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00001168 }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001169 }
1170 break;
1171 case Delete_kind:
1172 VISIT_SEQ(st, expr, s->v.Delete.targets);
1173 break;
1174 case Assign_kind:
1175 VISIT_SEQ(st, expr, s->v.Assign.targets);
1176 VISIT(st, expr, s->v.Assign.value);
1177 break;
1178 case AugAssign_kind:
1179 VISIT(st, expr, s->v.AugAssign.target);
1180 VISIT(st, expr, s->v.AugAssign.value);
1181 break;
1182 case For_kind:
1183 VISIT(st, expr, s->v.For.target);
1184 VISIT(st, expr, s->v.For.iter);
1185 VISIT_SEQ(st, stmt, s->v.For.body);
1186 if (s->v.For.orelse)
1187 VISIT_SEQ(st, stmt, s->v.For.orelse);
1188 break;
1189 case While_kind:
1190 VISIT(st, expr, s->v.While.test);
1191 VISIT_SEQ(st, stmt, s->v.While.body);
1192 if (s->v.While.orelse)
1193 VISIT_SEQ(st, stmt, s->v.While.orelse);
1194 break;
1195 case If_kind:
1196 /* XXX if 0: and lookup_yield() hacks */
1197 VISIT(st, expr, s->v.If.test);
1198 VISIT_SEQ(st, stmt, s->v.If.body);
1199 if (s->v.If.orelse)
1200 VISIT_SEQ(st, stmt, s->v.If.orelse);
1201 break;
1202 case Raise_kind:
1203 if (s->v.Raise.exc) {
1204 VISIT(st, expr, s->v.Raise.exc);
1205 if (s->v.Raise.cause) {
1206 VISIT(st, expr, s->v.Raise.cause);
1207 }
1208 }
1209 break;
1210 case TryExcept_kind:
1211 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1212 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1213 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1214 break;
1215 case TryFinally_kind:
1216 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1217 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1218 break;
1219 case Assert_kind:
1220 VISIT(st, expr, s->v.Assert.test);
1221 if (s->v.Assert.msg)
1222 VISIT(st, expr, s->v.Assert.msg);
1223 break;
1224 case Import_kind:
1225 VISIT_SEQ(st, alias, s->v.Import.names);
1226 /* XXX Don't have the lineno available inside
1227 visit_alias */
1228 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1229 st->st_cur->ste_opt_lineno = s->lineno;
1230 break;
1231 case ImportFrom_kind:
1232 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1233 /* XXX Don't have the lineno available inside
1234 visit_alias */
1235 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1236 st->st_cur->ste_opt_lineno = s->lineno;
1237 break;
1238 case Global_kind: {
1239 int i;
1240 asdl_seq *seq = s->v.Global.names;
1241 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1242 identifier name = (identifier)asdl_seq_GET(seq, i);
1243 char *c_name = _PyUnicode_AsString(name);
1244 long cur = symtable_lookup(st, name);
1245 if (cur < 0)
1246 return 0;
1247 if (cur & (DEF_LOCAL | USE)) {
1248 char buf[256];
1249 if (cur & DEF_LOCAL)
1250 PyOS_snprintf(buf, sizeof(buf),
1251 GLOBAL_AFTER_ASSIGN,
1252 c_name);
1253 else
1254 PyOS_snprintf(buf, sizeof(buf),
1255 GLOBAL_AFTER_USE,
1256 c_name);
1257 if (!symtable_warn(st, buf, s->lineno))
1258 return 0;
1259 }
1260 if (!symtable_add_def(st, name, DEF_GLOBAL))
1261 return 0;
1262 }
1263 break;
1264 }
1265 case Nonlocal_kind: {
1266 int i;
1267 asdl_seq *seq = s->v.Nonlocal.names;
1268 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1269 identifier name = (identifier)asdl_seq_GET(seq, i);
1270 char *c_name = _PyUnicode_AsString(name);
1271 long cur = symtable_lookup(st, name);
1272 if (cur < 0)
1273 return 0;
1274 if (cur & (DEF_LOCAL | USE)) {
1275 char buf[256];
1276 if (cur & DEF_LOCAL)
1277 PyOS_snprintf(buf, sizeof(buf),
1278 NONLOCAL_AFTER_ASSIGN,
1279 c_name);
1280 else
1281 PyOS_snprintf(buf, sizeof(buf),
1282 NONLOCAL_AFTER_USE,
1283 c_name);
1284 if (!symtable_warn(st, buf, s->lineno))
1285 return 0;
1286 }
1287 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1288 return 0;
1289 }
1290 break;
1291 }
1292 case Expr_kind:
1293 VISIT(st, expr, s->v.Expr.value);
1294 break;
1295 case Pass_kind:
1296 case Break_kind:
1297 case Continue_kind:
1298 /* nothing to do here */
1299 break;
1300 case With_kind:
1301 if (!symtable_new_tmpname(st))
1302 return 0;
1303 VISIT(st, expr, s->v.With.context_expr);
1304 if (s->v.With.optional_vars) {
1305 if (!symtable_new_tmpname(st))
1306 return 0;
1307 VISIT(st, expr, s->v.With.optional_vars);
1308 }
1309 VISIT_SEQ(st, stmt, s->v.With.body);
1310 break;
1311 }
1312 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313}
1314
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001315static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316symtable_visit_expr(struct symtable *st, expr_ty e)
1317{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001318 switch (e->kind) {
1319 case BoolOp_kind:
1320 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1321 break;
1322 case BinOp_kind:
1323 VISIT(st, expr, e->v.BinOp.left);
1324 VISIT(st, expr, e->v.BinOp.right);
1325 break;
1326 case UnaryOp_kind:
1327 VISIT(st, expr, e->v.UnaryOp.operand);
1328 break;
1329 case Lambda_kind: {
1330 if (!GET_IDENTIFIER(lambda) ||
1331 !symtable_add_def(st, lambda, DEF_LOCAL))
1332 return 0;
1333 if (e->v.Lambda.args->defaults)
1334 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1335 if (!symtable_enter_block(st, lambda,
1336 FunctionBlock, (void *)e, e->lineno))
1337 return 0;
1338 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1339 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1340 if (!symtable_exit_block(st, (void *)e))
1341 return 0;
1342 break;
1343 }
1344 case IfExp_kind:
1345 VISIT(st, expr, e->v.IfExp.test);
1346 VISIT(st, expr, e->v.IfExp.body);
1347 VISIT(st, expr, e->v.IfExp.orelse);
1348 break;
1349 case Dict_kind:
1350 VISIT_SEQ(st, expr, e->v.Dict.keys);
1351 VISIT_SEQ(st, expr, e->v.Dict.values);
1352 break;
1353 case Set_kind:
1354 VISIT_SEQ(st, expr, e->v.Set.elts);
1355 break;
1356 case GeneratorExp_kind:
1357 if (!symtable_visit_genexp(st, e))
1358 return 0;
1359 break;
1360 case ListComp_kind:
1361 if (!symtable_visit_listcomp(st, e))
1362 return 0;
1363 break;
1364 case SetComp_kind:
1365 if (!symtable_visit_setcomp(st, e))
1366 return 0;
1367 break;
1368 case DictComp_kind:
1369 if (!symtable_visit_dictcomp(st, e))
1370 return 0;
1371 break;
1372 case Yield_kind:
1373 if (e->v.Yield.value)
1374 VISIT(st, expr, e->v.Yield.value);
1375 st->st_cur->ste_generator = 1;
1376 if (st->st_cur->ste_returns_value) {
1377 PyErr_SetString(PyExc_SyntaxError,
1378 RETURN_VAL_IN_GENERATOR);
1379 PyErr_SyntaxLocation(st->st_filename,
1380 e->lineno);
1381 return 0;
1382 }
1383 break;
1384 case Compare_kind:
1385 VISIT(st, expr, e->v.Compare.left);
1386 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1387 break;
1388 case Call_kind:
1389 VISIT(st, expr, e->v.Call.func);
1390 VISIT_SEQ(st, expr, e->v.Call.args);
1391 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1392 if (e->v.Call.starargs)
1393 VISIT(st, expr, e->v.Call.starargs);
1394 if (e->v.Call.kwargs)
1395 VISIT(st, expr, e->v.Call.kwargs);
1396 break;
1397 case Num_kind:
1398 case Str_kind:
1399 case Bytes_kind:
1400 case Ellipsis_kind:
1401 /* Nothing to do here. */
1402 break;
1403 /* The following exprs can be assignment targets. */
1404 case Attribute_kind:
1405 VISIT(st, expr, e->v.Attribute.value);
1406 break;
1407 case Subscript_kind:
1408 VISIT(st, expr, e->v.Subscript.value);
1409 VISIT(st, slice, e->v.Subscript.slice);
1410 break;
1411 case Starred_kind:
1412 VISIT(st, expr, e->v.Starred.value);
1413 break;
1414 case Name_kind:
1415 if (!symtable_add_def(st, e->v.Name.id,
1416 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1417 return 0;
1418 /* Special-case super: it counts as a use of __class__ */
1419 if (e->v.Name.ctx == Load &&
1420 st->st_cur->ste_type == FunctionBlock &&
1421 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1422 if (!GET_IDENTIFIER(__class__) ||
1423 !symtable_add_def(st, __class__, USE))
1424 return 0;
1425 }
1426 break;
1427 /* child nodes of List and Tuple will have expr_context set */
1428 case List_kind:
1429 VISIT_SEQ(st, expr, e->v.List.elts);
1430 break;
1431 case Tuple_kind:
1432 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1433 break;
1434 }
1435 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436}
1437
1438static int
1439symtable_implicit_arg(struct symtable *st, int pos)
1440{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001441 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1442 if (id == NULL)
1443 return 0;
1444 if (!symtable_add_def(st, id, DEF_PARAM)) {
1445 Py_DECREF(id);
1446 return 0;
1447 }
1448 Py_DECREF(id);
1449 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450}
1451
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001452static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001453symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001455 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001456
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001457 if (!args)
1458 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001460 for (i = 0; i < asdl_seq_LEN(args); i++) {
1461 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1462 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1463 return 0;
1464 }
1465
1466 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467}
1468
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001469static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001470symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001472 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001473
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001474 if (!args)
1475 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001476
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001477 for (i = 0; i < asdl_seq_LEN(args); i++) {
1478 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1479 if (arg->annotation)
1480 VISIT(st, expr, arg->annotation);
1481 }
1482
1483 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001484}
1485
Neal Norwitzc1505362006-12-28 06:47:50 +00001486static int
1487symtable_visit_annotations(struct symtable *st, stmt_ty s)
1488{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001489 arguments_ty a = s->v.FunctionDef.args;
1490
1491 if (a->args && !symtable_visit_argannotations(st, a->args))
1492 return 0;
1493 if (a->varargannotation)
1494 VISIT(st, expr, a->varargannotation);
1495 if (a->kwargannotation)
1496 VISIT(st, expr, a->kwargannotation);
1497 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1498 return 0;
1499 if (s->v.FunctionDef.returns)
1500 VISIT(st, expr, s->v.FunctionDef.returns);
1501 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502}
1503
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001504static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505symtable_visit_arguments(struct symtable *st, arguments_ty a)
1506{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001507 /* skip default arguments inside function block
1508 XXX should ast be different?
1509 */
1510 if (a->args && !symtable_visit_params(st, a->args))
1511 return 0;
1512 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1513 return 0;
1514 if (a->vararg) {
1515 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1516 return 0;
1517 st->st_cur->ste_varargs = 1;
1518 }
1519 if (a->kwarg) {
1520 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1521 return 0;
1522 st->st_cur->ste_varkeywords = 1;
1523 }
1524 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
1526
1527
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001528static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1530{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001531 if (eh->v.ExceptHandler.type)
1532 VISIT(st, expr, eh->v.ExceptHandler.type);
1533 if (eh->v.ExceptHandler.name)
1534 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1535 return 0;
1536 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1537 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538}
1539
1540
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001541static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542symtable_visit_alias(struct symtable *st, alias_ty a)
1543{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001544 /* Compute store_name, the name actually bound by the import
1545 operation. It is diferent than a->name when a->name is a
1546 dotted package name (e.g. spam.eggs)
1547 */
1548 PyObject *store_name;
1549 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1550 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1551 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
1552 if (dot) {
1553 store_name = PyUnicode_FromUnicode(base, dot - base);
1554 if (!store_name)
1555 return 0;
1556 }
1557 else {
1558 store_name = name;
1559 Py_INCREF(store_name);
1560 }
1561 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1562 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1563 Py_DECREF(store_name);
1564 return r;
1565 }
1566 else {
1567 if (st->st_cur->ste_type != ModuleBlock) {
1568 int lineno = st->st_cur->ste_lineno;
1569 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1570 PyErr_SyntaxLocation(st->st_filename, lineno);
1571 Py_DECREF(store_name);
1572 return 0;
1573 }
1574 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1575 Py_DECREF(store_name);
1576 return 1;
1577 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578}
1579
1580
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001581static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1583{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001584 VISIT(st, expr, lc->target);
1585 VISIT(st, expr, lc->iter);
1586 VISIT_SEQ(st, expr, lc->ifs);
1587 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588}
1589
1590
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001591static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592symtable_visit_keyword(struct symtable *st, keyword_ty k)
1593{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001594 VISIT(st, expr, k->value);
1595 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596}
1597
1598
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001599static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600symtable_visit_slice(struct symtable *st, slice_ty s)
1601{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001602 switch (s->kind) {
1603 case Slice_kind:
1604 if (s->v.Slice.lower)
1605 VISIT(st, expr, s->v.Slice.lower)
1606 if (s->v.Slice.upper)
1607 VISIT(st, expr, s->v.Slice.upper)
1608 if (s->v.Slice.step)
1609 VISIT(st, expr, s->v.Slice.step)
1610 break;
1611 case ExtSlice_kind:
1612 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1613 break;
1614 case Index_kind:
1615 VISIT(st, expr, s->v.Index.value)
1616 break;
1617 }
1618 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619}
1620
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001621static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001622symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001623 identifier scope_name, asdl_seq *generators,
1624 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001626 int is_generator = (e->kind == GeneratorExp_kind);
1627 int needs_tmp = !is_generator;
1628 comprehension_ty outermost = ((comprehension_ty)
1629 asdl_seq_GET(generators, 0));
1630 /* Outermost iterator is evaluated in current scope */
1631 VISIT(st, expr, outermost->iter);
1632 /* Create comprehension scope for the rest */
1633 if (!scope_name ||
1634 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, e->lineno)) {
1635 return 0;
1636 }
1637 st->st_cur->ste_generator = is_generator;
1638 /* Outermost iter is received as an argument */
1639 if (!symtable_implicit_arg(st, 0)) {
1640 symtable_exit_block(st, (void *)e);
1641 return 0;
1642 }
1643 /* Allocate temporary name if needed */
1644 if (needs_tmp && !symtable_new_tmpname(st)) {
1645 symtable_exit_block(st, (void *)e);
1646 return 0;
1647 }
1648 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1649 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1650 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1651 generators, 1, (void*)e);
1652 if (value)
1653 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1654 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1655 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001657
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001658static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001659symtable_visit_genexp(struct symtable *st, expr_ty e)
1660{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001661 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1662 e->v.GeneratorExp.generators,
1663 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001664}
1665
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001666static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001667symtable_visit_listcomp(struct symtable *st, expr_ty e)
1668{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001669 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1670 e->v.ListComp.generators,
1671 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001672}
1673
1674static int
1675symtable_visit_setcomp(struct symtable *st, expr_ty e)
1676{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001677 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1678 e->v.SetComp.generators,
1679 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001680}
1681
1682static int
1683symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1684{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001685 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1686 e->v.DictComp.generators,
1687 e->v.DictComp.key,
1688 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001689}