blob: 37bdf2a1051a4727b8fcf586267808f8e0fdf6fd [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 Pitrouf95a1b32010-05-09 15:52:27 +000028 void *key, int lineno)
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;
68 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 if (st->st_cur != NULL &&
71 (st->st_cur->ste_nested ||
72 st->st_cur->ste_type == FunctionBlock))
73 ste->ste_nested = 1;
74 ste->ste_child_free = 0;
75 ste->ste_generator = 0;
76 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
79 goto fail;
80
81 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 Py_XDECREF(ste);
84 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085}
86
87static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
91 ste->ste_name,
92 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093}
94
95static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 ste->ste_table = NULL;
99 Py_XDECREF(ste->ste_id);
100 Py_XDECREF(ste->ste_name);
101 Py_XDECREF(ste->ste_symbols);
102 Py_XDECREF(ste->ste_varnames);
103 Py_XDECREF(ste->ste_children);
104 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000105}
106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108
Guido van Rossum6f799372001-09-20 20:46:19 +0000109static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 {"id", T_OBJECT, OFF(ste_id), READONLY},
111 {"name", T_OBJECT, OFF(ste_name), READONLY},
112 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
113 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
114 {"children", T_OBJECT, OFF(ste_children), READONLY},
115 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
116 {"nested", T_INT, OFF(ste_nested), READONLY},
117 {"type", T_INT, OFF(ste_type), READONLY},
118 {"lineno", T_INT, OFF(ste_lineno), READONLY},
119 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120};
121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 PyVarObject_HEAD_INIT(&PyType_Type, 0)
124 "symtable entry",
125 sizeof(PySTEntryObject),
126 0,
127 (destructor)ste_dealloc, /* tp_dealloc */
128 0, /* tp_print */
129 0, /* tp_getattr */
130 0, /* tp_setattr */
131 0, /* tp_reserved */
132 (reprfunc)ste_repr, /* tp_repr */
133 0, /* tp_as_number */
134 0, /* tp_as_sequence */
135 0, /* tp_as_mapping */
136 0, /* tp_hash */
137 0, /* tp_call */
138 0, /* tp_str */
139 PyObject_GenericGetAttr, /* tp_getattro */
140 0, /* tp_setattro */
141 0, /* tp_as_buffer */
142 Py_TPFLAGS_DEFAULT, /* tp_flags */
143 0, /* tp_doc */
144 0, /* tp_traverse */
145 0, /* tp_clear */
146 0, /* tp_richcompare */
147 0, /* tp_weaklistoffset */
148 0, /* tp_iter */
149 0, /* tp_iternext */
150 0, /* tp_methods */
151 ste_memberlist, /* tp_members */
152 0, /* tp_getset */
153 0, /* tp_base */
154 0, /* tp_dict */
155 0, /* tp_descr_get */
156 0, /* tp_descr_set */
157 0, /* tp_dictoffset */
158 0, /* tp_init */
159 0, /* tp_alloc */
160 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000161};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162
163static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000164static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165static int symtable_enter_block(struct symtable *st, identifier name,
166 _Py_block_ty block, void *ast, int lineno);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_exit_block(struct symtable *st, void *ast);
168static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
169static int symtable_visit_expr(struct symtable *st, expr_ty s);
170static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000171static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
172static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000173static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static int symtable_visit_arguments(struct symtable *st, arguments_ty);
175static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
176static int symtable_visit_alias(struct symtable *st, alias_ty);
177static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
178static int symtable_visit_keyword(struct symtable *st, keyword_ty);
179static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000180static int symtable_visit_params(struct symtable *st, asdl_seq *args);
181static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000183static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184
185
Nick Coghlan650f0d02007-04-15 12:05:43 +0000186static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
188 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189
190#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
193#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000194"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196static struct symtable *
197symtable_new(void)
198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
202 if (st == NULL)
203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 st->st_filename = NULL;
206 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 if ((st->st_stack = PyList_New(0)) == NULL)
209 goto fail;
210 if ((st->st_blocks = PyDict_New()) == NULL)
211 goto fail;
212 st->st_cur = NULL;
213 st->st_private = NULL;
214 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 PySymtable_Free(st);
217 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218}
219
220struct symtable *
221PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 struct symtable *st = symtable_new();
224 asdl_seq *seq;
225 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 if (st == NULL)
228 return st;
229 st->st_filename = filename;
230 st->st_future = future;
231 /* Make the initial symbol information gathering pass */
232 if (!GET_IDENTIFIER(top) ||
233 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) {
234 PySymtable_Free(st);
235 return NULL;
236 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 st->st_top = st->st_cur;
239 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
240 switch (mod->kind) {
241 case Module_kind:
242 seq = mod->v.Module.body;
243 for (i = 0; i < asdl_seq_LEN(seq); i++)
244 if (!symtable_visit_stmt(st,
245 (stmt_ty)asdl_seq_GET(seq, i)))
246 goto error;
247 break;
248 case Expression_kind:
249 if (!symtable_visit_expr(st, mod->v.Expression.body))
250 goto error;
251 break;
252 case Interactive_kind:
253 seq = mod->v.Interactive.body;
254 for (i = 0; i < asdl_seq_LEN(seq); i++)
255 if (!symtable_visit_stmt(st,
256 (stmt_ty)asdl_seq_GET(seq, i)))
257 goto error;
258 break;
259 case Suite_kind:
260 PyErr_SetString(PyExc_RuntimeError,
261 "this compiler does not handle Suites");
262 goto error;
263 }
264 if (!symtable_exit_block(st, (void *)mod)) {
265 PySymtable_Free(st);
266 return NULL;
267 }
268 /* Make the second symbol analysis pass */
269 if (symtable_analyze(st))
270 return st;
271 PySymtable_Free(st);
272 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 (void) symtable_exit_block(st, (void *)mod);
275 PySymtable_Free(st);
276 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277}
278
279void
280PySymtable_Free(struct symtable *st)
281{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 Py_XDECREF(st->st_blocks);
283 Py_XDECREF(st->st_stack);
284 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285}
286
287PySTEntryObject *
288PySymtable_Lookup(struct symtable *st, void *key)
289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 k = PyLong_FromVoidPtr(key);
293 if (k == NULL)
294 return NULL;
295 v = PyDict_GetItem(st->st_blocks, k);
296 if (v) {
297 assert(PySTEntry_Check(v));
298 Py_INCREF(v);
299 }
300 else {
301 PyErr_SetString(PyExc_KeyError,
302 "unknown symbol table entry");
303 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 Py_DECREF(k);
306 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307}
308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310PyST_GetScope(PySTEntryObject *ste, PyObject *name)
311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
313 if (!v)
314 return 0;
315 assert(PyLong_Check(v));
316 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317}
318
319
320/* Analyze raw symbol information to determine scope of each name.
321
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000322 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328 explicit global is declared with the global statement. An implicit
329 global is a free variable for which the compiler has found no binding
330 in an enclosing function scope. The implicit global is either a global
331 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
332 to handle these names to implement slightly odd semantics. In such a
333 block, the name is treated as global until it is assigned to; then it
334 is treated as a local.
335
336 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000337 The first pass collects raw facts from the AST via the symtable_visit_*
338 functions: the name is a parameter here, the name is used but not defined
339 here, etc. The second pass analyzes these facts during a pass over the
340 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341
342 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000344 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000345 Names which are explicitly declared nonlocal must exist in this set of
346 visible names - if they do not, a syntax error is raised. After doing
347 the local analysis, it analyzes each of its child blocks using an
348 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
Nick Coghlan650f0d02007-04-15 12:05:43 +0000350 The children update the free variable set. If a local variable is added to
351 the free variable set by the child, the variable is marked as a cell. The
352 function object being defined must provide runtime storage for the variable
353 that may outlive the function's frame. Cell variables are removed from the
354 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000355
Nick Coghlan650f0d02007-04-15 12:05:43 +0000356 During analysis, the names are:
357 symbols: dict mapping from symbol names to flag values (including offset scope values)
358 scopes: dict mapping from symbol names to scope values (no offset)
359 local: set of all symbol names local to the current scope
360 bound: set of all symbol names local to a containing function scope
361 free: set of all symbol names referenced but not bound in child scopes
362 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363*/
364
365#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 PyObject *o = PyLong_FromLong(I); \
367 if (!o) \
368 return 0; \
369 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
370 Py_DECREF(o); \
371 return 0; \
372 } \
373 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374}
375
376/* Decide on scope of name, given flags.
377
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000378 The namespace dictionaries may be modified to record information
379 about the new name. For example, a new global will add an entry to
380 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381*/
382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000384analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 PyObject *bound, PyObject *local, PyObject *free,
386 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (flags & DEF_GLOBAL) {
389 if (flags & DEF_PARAM) {
390 PyErr_Format(PyExc_SyntaxError,
391 "name '%U' is parameter and global",
392 name);
393 PyErr_SyntaxLocation(ste->ste_table->st_filename,
394 ste->ste_lineno);
395
396 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000397 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (flags & DEF_NONLOCAL) {
399 PyErr_Format(PyExc_SyntaxError,
400 "name '%U' is nonlocal and global",
401 name);
402 return 0;
403 }
404 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
405 if (PySet_Add(global, name) < 0)
406 return 0;
407 if (bound && (PySet_Discard(bound, name) < 0))
408 return 0;
409 return 1;
410 }
411 if (flags & DEF_NONLOCAL) {
412 if (flags & DEF_PARAM) {
413 PyErr_Format(PyExc_SyntaxError,
414 "name '%U' is parameter and nonlocal",
415 name);
416 return 0;
417 }
418 if (!bound) {
419 PyErr_Format(PyExc_SyntaxError,
420 "nonlocal declaration not allowed at module level");
421 return 0;
422 }
423 if (!PySet_Contains(bound, name)) {
424 PyErr_Format(PyExc_SyntaxError,
425 "no binding for nonlocal '%U' found",
426 name);
427
428 return 0;
429 }
430 SET_SCOPE(scopes, name, FREE);
431 ste->ste_free = 1;
432 return PySet_Add(free, name) >= 0;
433 }
434 if (flags & DEF_BOUND) {
Benjamin Peterson013783c2010-07-20 22:37:19 +0000435 if (ste->ste_type == ClassBlock &&
436 !(flags & DEF_PARAM) &&
437 bound && PySet_Contains(bound, name)) {
438 SET_SCOPE(scopes, name, LOCAL_ONLY);
439 }
440 else {
441 SET_SCOPE(scopes, name, LOCAL);
442 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (PySet_Add(local, name) < 0)
444 return 0;
445 if (PySet_Discard(global, name) < 0)
446 return 0;
447 return 1;
448 }
449 /* If an enclosing block has a binding for this name, it
450 is a free variable rather than a global variable.
451 Note that having a non-NULL bound implies that the block
452 is nested.
453 */
454 if (bound && PySet_Contains(bound, name)) {
455 SET_SCOPE(scopes, name, FREE);
456 ste->ste_free = 1;
457 return PySet_Add(free, name) >= 0;
458 }
459 /* If a parent has a global statement, then call it global
460 explicit? It could also be global implicit.
461 */
462 if (global && PySet_Contains(global, name)) {
463 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
464 return 1;
465 }
466 if (ste->ste_nested)
467 ste->ste_free = 1;
468 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
469 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470}
471
472#undef SET_SCOPE
473
474/* If a name is defined in free and also in locals, then this block
475 provides the binding for the free variable. The name should be
476 marked CELL in this block and removed from the free list.
477
478 Note that the current block's free variables are included in free.
479 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000480
Martin v. Löwis2673a572007-10-29 19:54:24 +0000481 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000482 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483*/
484
485static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000486analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyObject *name, *v, *v_cell;
489 int success = 0;
490 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 v_cell = PyLong_FromLong(CELL);
493 if (!v_cell)
494 return 0;
495 while (PyDict_Next(scopes, &pos, &name, &v)) {
496 long scope;
497 assert(PyLong_Check(v));
498 scope = PyLong_AS_LONG(v);
Benjamin Peterson013783c2010-07-20 22:37:19 +0000499 if (scope != LOCAL && scope != LOCAL_ONLY)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 continue;
501 if (!PySet_Contains(free, name))
502 continue;
503 if (restricted != NULL &&
504 PyUnicode_CompareWithASCIIString(name, restricted))
505 continue;
506 /* Replace LOCAL with CELL for this name, and remove
507 from free. It is safe to replace the value of name
508 in the dict, because it will not cause a resize.
509 */
510 if (PyDict_SetItem(scopes, name, v_cell) < 0)
511 goto error;
512 if (PySet_Discard(free, name) < 0)
513 goto error;
514 }
515 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 Py_DECREF(v_cell);
518 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519}
520
521/* Check for illegal statements in unoptimized namespaces */
522static int
523check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
527 || !(ste->ste_free || ste->ste_child_free))
528 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 trailer = (ste->ste_child_free ?
531 "contains a nested function with free variables" :
532 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 switch (ste->ste_unoptimized) {
535 case OPT_TOPLEVEL: /* import * at top-level is fine */
536 return 1;
537 case OPT_IMPORT_STAR:
538 PyErr_Format(PyExc_SyntaxError,
539 "import * is not allowed in function '%U' because it %s",
540 ste->ste_name, trailer);
541 break;
542 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 PyErr_SyntaxLocation(ste->ste_table->st_filename,
545 ste->ste_opt_lineno);
546 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547}
548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549/* Enter the final scope information into the ste_symbols dict.
550 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551 * All arguments are dicts. Modifies symbols, others are read-only.
552*/
553static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000555 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 PyObject *name = NULL, *itr = NULL;
558 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
559 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 /* Update scope information for all symbols in this scope */
562 while (PyDict_Next(symbols, &pos, &name, &v)) {
563 long scope, flags;
564 assert(PyLong_Check(v));
565 flags = PyLong_AS_LONG(v);
566 v_scope = PyDict_GetItem(scopes, name);
567 assert(v_scope && PyLong_Check(v_scope));
568 scope = PyLong_AS_LONG(v_scope);
569 flags |= (scope << SCOPE_OFFSET);
570 v_new = PyLong_FromLong(flags);
571 if (!v_new)
572 return 0;
573 if (PyDict_SetItem(symbols, name, v_new) < 0) {
574 Py_DECREF(v_new);
575 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 Py_DECREF(v_new);
578 }
579
580 /* Record not yet resolved free variables from children (if any) */
581 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
582 if (!v_free)
583 return 0;
584
585 itr = PyObject_GetIter(free);
586 if (!itr)
587 goto error;
588
589 while ((name = PyIter_Next(itr))) {
590 v = PyDict_GetItem(symbols, name);
591
592 /* Handle symbol that already exists in this scope */
593 if (v) {
594 /* Handle a free variable in a method of
595 the class that has the same name as a local
596 or global in the class scope.
597 */
598 if (classflag &&
599 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
600 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
601 v_new = PyLong_FromLong(flags);
602 if (!v_new) {
603 goto error;
604 }
605 if (PyDict_SetItem(symbols, name, v_new) < 0) {
606 Py_DECREF(v_new);
607 goto error;
608 }
609 Py_DECREF(v_new);
610 }
611 /* It's a cell, or already free in this scope */
612 Py_DECREF(name);
613 continue;
614 }
615 /* Handle global symbol */
616 if (!PySet_Contains(bound, name)) {
617 Py_DECREF(name);
618 continue; /* it's a global */
619 }
620 /* Propagate new free symbol up the lexical stack */
621 if (PyDict_SetItem(symbols, name, v_free) < 0) {
622 goto error;
623 }
624 Py_DECREF(name);
625 }
626 Py_DECREF(itr);
627 Py_DECREF(v_free);
628 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000629error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 Py_XDECREF(v_free);
631 Py_XDECREF(itr);
632 Py_XDECREF(name);
633 return 0;
634}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635
636/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000637
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 Arguments:
639 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000640 bound -- set of variables bound in enclosing scopes (input). bound
641 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642 free -- set of free variables in enclosed scopes (output)
643 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000644
645 The implementation uses two mutually recursive functions,
646 analyze_block() and analyze_child_block(). analyze_block() is
647 responsible for analyzing the individual names defined in a block.
648 analyze_child_block() prepares temporary namespace dictionaries
649 used to evaluated nested blocks.
650
651 The two functions exist because a child block should see the name
652 bindings of its enclosing blocks, but those bindings should not
653 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654*/
655
656static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
658 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000659
660static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
662 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
665 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
666 PyObject *temp;
667 int i, success = 0;
668 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 local = PySet_New(NULL); /* collect new names bound in block */
671 if (!local)
672 goto error;
673 scopes = PyDict_New(); /* collect scopes defined for each name */
674 if (!scopes)
675 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 /* Allocate new global and bound variable dictionaries. These
678 dictionaries hold the names visible in nested blocks. For
679 ClassBlocks, the bound and global names are initialized
680 before analyzing names, because class bindings aren't
681 visible in methods. For other blocks, they are initialized
682 after names are analyzed.
683 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 /* TODO(jhylton): Package these dicts in a struct so that we
686 can write reasonable helper functions?
687 */
688 newglobal = PySet_New(NULL);
689 if (!newglobal)
690 goto error;
691 newfree = PySet_New(NULL);
692 if (!newfree)
693 goto error;
694 newbound = PySet_New(NULL);
695 if (!newbound)
696 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 /* Class namespace has no effect on names visible in
699 nested functions, so populate the global and bound
700 sets to be passed to child blocks before analyzing
701 this one.
702 */
703 if (ste->ste_type == ClassBlock) {
704 /* Pass down known globals */
705 temp = PyNumber_InPlaceOr(newglobal, global);
706 if (!temp)
707 goto error;
708 Py_DECREF(temp);
709 /* Pass down previously bound symbols */
710 if (bound) {
711 temp = PyNumber_InPlaceOr(newbound, bound);
712 if (!temp)
713 goto error;
714 Py_DECREF(temp);
715 }
716 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
719 long flags = PyLong_AS_LONG(v);
720 if (!analyze_name(ste, scopes, name, flags,
721 bound, local, free, global))
722 goto error;
723 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 /* Populate global and bound sets to be passed to children. */
726 if (ste->ste_type != ClassBlock) {
727 /* Add function locals to bound set */
728 if (ste->ste_type == FunctionBlock) {
729 temp = PyNumber_InPlaceOr(newbound, local);
730 if (!temp)
731 goto error;
732 Py_DECREF(temp);
733 }
734 /* Pass down previously bound symbols */
735 if (bound) {
736 temp = PyNumber_InPlaceOr(newbound, bound);
737 if (!temp)
738 goto error;
739 Py_DECREF(temp);
740 }
741 /* Pass down known globals */
742 temp = PyNumber_InPlaceOr(newglobal, global);
743 if (!temp)
744 goto error;
745 Py_DECREF(temp);
746 }
747 else {
748 /* Special-case __class__ */
749 if (!GET_IDENTIFIER(__class__))
750 goto error;
751 assert(PySet_Contains(local, __class__) == 1);
752 if (PySet_Add(newbound, __class__) < 0)
753 goto error;
754 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 /* Recursively call analyze_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 newbound, newglobal now contain the names visible in
759 nested blocks. The free variables in the children will
760 be collected in allfree.
761 */
762 allfree = PySet_New(NULL);
763 if (!allfree)
764 goto error;
765 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
766 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
767 PySTEntryObject* entry;
768 assert(c && PySTEntry_Check(c));
769 entry = (PySTEntryObject*)c;
770 if (!analyze_child_block(entry, newbound, newfree, newglobal,
771 allfree))
772 goto error;
773 /* Check if any children have free variables */
774 if (entry->ste_free || entry->ste_child_free)
775 ste->ste_child_free = 1;
776 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 temp = PyNumber_InPlaceOr(newfree, allfree);
779 if (!temp)
780 goto error;
781 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 /* Check if any local variables must be converted to cell variables */
784 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
785 NULL))
786 goto error;
787 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
788 "__class__"))
789 goto error;
790 /* Records the results of the analysis in the symbol table entry */
791 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
792 ste->ste_type == ClassBlock))
793 goto error;
794 if (!check_unoptimized(ste))
795 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 temp = PyNumber_InPlaceOr(free, newfree);
798 if (!temp)
799 goto error;
800 Py_DECREF(temp);
801 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 Py_XDECREF(scopes);
804 Py_XDECREF(local);
805 Py_XDECREF(newbound);
806 Py_XDECREF(newglobal);
807 Py_XDECREF(newfree);
808 Py_XDECREF(allfree);
809 if (!success)
810 assert(PyErr_Occurred());
811 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812}
813
814static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
816 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
819 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 These dictionary are used by all blocks enclosed by the
824 current block. The analyze_block() call modifies these
825 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 */
828 temp_bound = PySet_New(bound);
829 if (!temp_bound)
830 goto error;
831 temp_free = PySet_New(free);
832 if (!temp_free)
833 goto error;
834 temp_global = PySet_New(global);
835 if (!temp_global)
836 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
839 goto error;
840 temp = PyNumber_InPlaceOr(child_free, temp_free);
841 if (!temp)
842 goto error;
843 Py_DECREF(temp);
844 Py_DECREF(temp_bound);
845 Py_DECREF(temp_free);
846 Py_DECREF(temp_global);
847 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000848 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 Py_XDECREF(temp_bound);
850 Py_XDECREF(temp_free);
851 Py_XDECREF(temp_global);
852 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000853}
854
855static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856symtable_analyze(struct symtable *st)
857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 PyObject *free, *global;
859 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 free = PySet_New(NULL);
862 if (!free)
863 return 0;
864 global = PySet_New(NULL);
865 if (!global) {
866 Py_DECREF(free);
867 return 0;
868 }
869 r = analyze_block(st->st_top, NULL, free, global);
870 Py_DECREF(free);
871 Py_DECREF(global);
872 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873}
874
875
876static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000877symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
880 lineno, NULL, NULL) < 0) {
881 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
882 PyErr_SetString(PyExc_SyntaxError, msg);
883 PyErr_SyntaxLocation(st->st_filename,
884 st->st_cur->ste_lineno);
885 }
886 return 0;
887 }
888 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889}
890
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000891/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892 This reference is released when the block is exited, via the DECREF
893 in symtable_exit_block().
894*/
895
896static int
897symtable_exit_block(struct symtable *st, void *ast)
898{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 Py_CLEAR(st->st_cur);
902 end = PyList_GET_SIZE(st->st_stack) - 1;
903 if (end >= 0) {
904 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
905 end);
906 if (st->st_cur == NULL)
907 return 0;
908 Py_INCREF(st->st_cur);
909 if (PySequence_DelItem(st->st_stack, end) < 0)
910 return 0;
911 }
912 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913}
914
915static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
917 void *ast, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 if (st->st_cur) {
922 prev = st->st_cur;
923 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
924 return 0;
925 }
926 Py_DECREF(st->st_cur);
927 }
928 st->st_cur = ste_new(st, name, block, ast, lineno);
929 if (st->st_cur == NULL)
930 return 0;
931 if (name == GET_IDENTIFIER(top))
932 st->st_global = st->st_cur->ste_symbols;
933 if (prev) {
934 if (PyList_Append(prev->ste_children,
935 (PyObject *)st->st_cur) < 0) {
936 return 0;
937 }
938 }
939 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940}
941
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000942static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943symtable_lookup(struct symtable *st, PyObject *name)
944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 PyObject *o;
946 PyObject *mangled = _Py_Mangle(st->st_private, name);
947 if (!mangled)
948 return 0;
949 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
950 Py_DECREF(mangled);
951 if (!o)
952 return 0;
953 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954}
955
956static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 PyObject *o;
960 PyObject *dict;
961 long val;
962 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963
Jeremy Hylton81e95022007-02-27 06:50:52 +0000964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 if (!mangled)
966 return 0;
967 dict = st->st_cur->ste_symbols;
968 if ((o = PyDict_GetItem(dict, mangled))) {
969 val = PyLong_AS_LONG(o);
970 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
971 /* Is it better to use 'mangled' or 'name' here? */
972 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
973 PyErr_SyntaxLocation(st->st_filename,
974 st->st_cur->ste_lineno);
975 goto error;
976 }
977 val |= flag;
978 } else
979 val = flag;
980 o = PyLong_FromLong(val);
981 if (o == NULL)
982 goto error;
983 if (PyDict_SetItem(dict, mangled, o) < 0) {
984 Py_DECREF(o);
985 goto error;
986 }
987 Py_DECREF(o);
988
989 if (flag & DEF_PARAM) {
990 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
991 goto error;
992 } else if (flag & DEF_GLOBAL) {
993 /* XXX need to update DEF_GLOBAL for other flags too;
994 perhaps only DEF_FREE_GLOBAL */
995 val = flag;
996 if ((o = PyDict_GetItem(st->st_global, mangled))) {
997 val |= PyLong_AS_LONG(o);
998 }
999 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 goto error;
1002 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1003 Py_DECREF(o);
1004 goto error;
1005 }
1006 Py_DECREF(o);
1007 }
1008 Py_DECREF(mangled);
1009 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001010
1011error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 Py_DECREF(mangled);
1013 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014}
1015
1016/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1017 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 function.
1019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1021 useful if the first node in the sequence requires special treatment.
1022*/
1023
1024#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 if (!symtable_visit_ ## TYPE((ST), (V))) \
1026 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001027
1028#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1030 symtable_exit_block((ST), (S)); \
1031 return 0; \
1032 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001033
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 int i; \
1036 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1037 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1038 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1039 if (!symtable_visit_ ## TYPE((ST), elt)) \
1040 return 0; \
1041 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001043
1044#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 int i; \
1046 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1047 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1048 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1049 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1050 symtable_exit_block((ST), (S)); \
1051 return 0; \
1052 } \
1053 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001054}
1055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 int i; \
1058 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1059 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1060 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1061 if (!symtable_visit_ ## TYPE((ST), elt)) \
1062 return 0; \
1063 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001065
1066#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 int i; \
1068 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1069 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1070 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1071 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1072 symtable_exit_block((ST), (S)); \
1073 return 0; \
1074 } \
1075 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001076}
1077
Guido van Rossum4f72a782006-10-27 23:31:49 +00001078#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 int i = 0; \
1080 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1081 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1082 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1083 if (!elt) continue; /* can be NULL */ \
1084 if (!symtable_visit_expr((ST), elt)) \
1085 return 0; \
1086 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001087}
1088
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001090symtable_new_tmpname(struct symtable *st)
1091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 char tmpname[256];
1093 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1096 ++st->st_cur->ste_tmpname);
1097 tmp = PyUnicode_InternFromString(tmpname);
1098 if (!tmp)
1099 return 0;
1100 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1101 return 0;
1102 Py_DECREF(tmp);
1103 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001104}
1105
Guido van Rossum4f72a782006-10-27 23:31:49 +00001106
Guido van Rossumc2e20742006-02-27 22:32:47 +00001107static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108symtable_visit_stmt(struct symtable *st, stmt_ty s)
1109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 switch (s->kind) {
1111 case FunctionDef_kind:
1112 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1113 return 0;
1114 if (s->v.FunctionDef.args->defaults)
1115 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1116 if (s->v.FunctionDef.args->kw_defaults)
1117 VISIT_KWONLYDEFAULTS(st,
1118 s->v.FunctionDef.args->kw_defaults);
1119 if (!symtable_visit_annotations(st, s))
1120 return 0;
1121 if (s->v.FunctionDef.decorator_list)
1122 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1123 if (!symtable_enter_block(st, s->v.FunctionDef.name,
1124 FunctionBlock, (void *)s, s->lineno))
1125 return 0;
1126 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1127 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1128 if (!symtable_exit_block(st, s))
1129 return 0;
1130 break;
1131 case ClassDef_kind: {
1132 PyObject *tmp;
1133 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1134 return 0;
1135 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1136 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1137 if (s->v.ClassDef.starargs)
1138 VISIT(st, expr, s->v.ClassDef.starargs);
1139 if (s->v.ClassDef.kwargs)
1140 VISIT(st, expr, s->v.ClassDef.kwargs);
1141 if (s->v.ClassDef.decorator_list)
1142 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1143 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1144 (void *)s, s->lineno))
1145 return 0;
1146 if (!GET_IDENTIFIER(__class__) ||
1147 !symtable_add_def(st, __class__, DEF_LOCAL) ||
1148 !GET_IDENTIFIER(__locals__) ||
1149 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1150 symtable_exit_block(st, s);
1151 return 0;
1152 }
1153 tmp = st->st_private;
1154 st->st_private = s->v.ClassDef.name;
1155 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1156 st->st_private = tmp;
1157 if (!symtable_exit_block(st, s))
1158 return 0;
1159 break;
1160 }
1161 case Return_kind:
1162 if (s->v.Return.value) {
1163 VISIT(st, expr, s->v.Return.value);
1164 st->st_cur->ste_returns_value = 1;
1165 if (st->st_cur->ste_generator) {
1166 PyErr_SetString(PyExc_SyntaxError,
1167 RETURN_VAL_IN_GENERATOR);
1168 PyErr_SyntaxLocation(st->st_filename,
1169 s->lineno);
1170 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00001171 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 }
1173 break;
1174 case Delete_kind:
1175 VISIT_SEQ(st, expr, s->v.Delete.targets);
1176 break;
1177 case Assign_kind:
1178 VISIT_SEQ(st, expr, s->v.Assign.targets);
1179 VISIT(st, expr, s->v.Assign.value);
1180 break;
1181 case AugAssign_kind:
1182 VISIT(st, expr, s->v.AugAssign.target);
1183 VISIT(st, expr, s->v.AugAssign.value);
1184 break;
1185 case For_kind:
1186 VISIT(st, expr, s->v.For.target);
1187 VISIT(st, expr, s->v.For.iter);
1188 VISIT_SEQ(st, stmt, s->v.For.body);
1189 if (s->v.For.orelse)
1190 VISIT_SEQ(st, stmt, s->v.For.orelse);
1191 break;
1192 case While_kind:
1193 VISIT(st, expr, s->v.While.test);
1194 VISIT_SEQ(st, stmt, s->v.While.body);
1195 if (s->v.While.orelse)
1196 VISIT_SEQ(st, stmt, s->v.While.orelse);
1197 break;
1198 case If_kind:
1199 /* XXX if 0: and lookup_yield() hacks */
1200 VISIT(st, expr, s->v.If.test);
1201 VISIT_SEQ(st, stmt, s->v.If.body);
1202 if (s->v.If.orelse)
1203 VISIT_SEQ(st, stmt, s->v.If.orelse);
1204 break;
1205 case Raise_kind:
1206 if (s->v.Raise.exc) {
1207 VISIT(st, expr, s->v.Raise.exc);
1208 if (s->v.Raise.cause) {
1209 VISIT(st, expr, s->v.Raise.cause);
1210 }
1211 }
1212 break;
1213 case TryExcept_kind:
1214 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
1215 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
1216 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
1217 break;
1218 case TryFinally_kind:
1219 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
1220 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
1221 break;
1222 case Assert_kind:
1223 VISIT(st, expr, s->v.Assert.test);
1224 if (s->v.Assert.msg)
1225 VISIT(st, expr, s->v.Assert.msg);
1226 break;
1227 case Import_kind:
1228 VISIT_SEQ(st, alias, s->v.Import.names);
1229 /* XXX Don't have the lineno available inside
1230 visit_alias */
1231 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1232 st->st_cur->ste_opt_lineno = s->lineno;
1233 break;
1234 case ImportFrom_kind:
1235 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1236 /* XXX Don't have the lineno available inside
1237 visit_alias */
1238 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
1239 st->st_cur->ste_opt_lineno = s->lineno;
1240 break;
1241 case Global_kind: {
1242 int i;
1243 asdl_seq *seq = s->v.Global.names;
1244 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1245 identifier name = (identifier)asdl_seq_GET(seq, i);
1246 char *c_name = _PyUnicode_AsString(name);
1247 long cur = symtable_lookup(st, name);
1248 if (cur < 0)
1249 return 0;
1250 if (cur & (DEF_LOCAL | USE)) {
1251 char buf[256];
1252 if (cur & DEF_LOCAL)
1253 PyOS_snprintf(buf, sizeof(buf),
1254 GLOBAL_AFTER_ASSIGN,
1255 c_name);
1256 else
1257 PyOS_snprintf(buf, sizeof(buf),
1258 GLOBAL_AFTER_USE,
1259 c_name);
1260 if (!symtable_warn(st, buf, s->lineno))
1261 return 0;
1262 }
1263 if (!symtable_add_def(st, name, DEF_GLOBAL))
1264 return 0;
1265 }
1266 break;
1267 }
1268 case Nonlocal_kind: {
1269 int i;
1270 asdl_seq *seq = s->v.Nonlocal.names;
1271 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1272 identifier name = (identifier)asdl_seq_GET(seq, i);
1273 char *c_name = _PyUnicode_AsString(name);
1274 long cur = symtable_lookup(st, name);
1275 if (cur < 0)
1276 return 0;
1277 if (cur & (DEF_LOCAL | USE)) {
1278 char buf[256];
1279 if (cur & DEF_LOCAL)
1280 PyOS_snprintf(buf, sizeof(buf),
1281 NONLOCAL_AFTER_ASSIGN,
1282 c_name);
1283 else
1284 PyOS_snprintf(buf, sizeof(buf),
1285 NONLOCAL_AFTER_USE,
1286 c_name);
1287 if (!symtable_warn(st, buf, s->lineno))
1288 return 0;
1289 }
1290 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1291 return 0;
1292 }
1293 break;
1294 }
1295 case Expr_kind:
1296 VISIT(st, expr, s->v.Expr.value);
1297 break;
1298 case Pass_kind:
1299 case Break_kind:
1300 case Continue_kind:
1301 /* nothing to do here */
1302 break;
1303 case With_kind:
1304 VISIT(st, expr, s->v.With.context_expr);
1305 if (s->v.With.optional_vars) {
1306 VISIT(st, expr, s->v.With.optional_vars);
1307 }
1308 VISIT_SEQ(st, stmt, s->v.With.body);
1309 break;
1310 }
1311 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312}
1313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315symtable_visit_expr(struct symtable *st, expr_ty e)
1316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 switch (e->kind) {
1318 case BoolOp_kind:
1319 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1320 break;
1321 case BinOp_kind:
1322 VISIT(st, expr, e->v.BinOp.left);
1323 VISIT(st, expr, e->v.BinOp.right);
1324 break;
1325 case UnaryOp_kind:
1326 VISIT(st, expr, e->v.UnaryOp.operand);
1327 break;
1328 case Lambda_kind: {
1329 if (!GET_IDENTIFIER(lambda))
1330 return 0;
1331 if (e->v.Lambda.args->defaults)
1332 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1333 if (!symtable_enter_block(st, lambda,
1334 FunctionBlock, (void *)e, e->lineno))
1335 return 0;
1336 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1337 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1338 if (!symtable_exit_block(st, (void *)e))
1339 return 0;
1340 break;
1341 }
1342 case IfExp_kind:
1343 VISIT(st, expr, e->v.IfExp.test);
1344 VISIT(st, expr, e->v.IfExp.body);
1345 VISIT(st, expr, e->v.IfExp.orelse);
1346 break;
1347 case Dict_kind:
1348 VISIT_SEQ(st, expr, e->v.Dict.keys);
1349 VISIT_SEQ(st, expr, e->v.Dict.values);
1350 break;
1351 case Set_kind:
1352 VISIT_SEQ(st, expr, e->v.Set.elts);
1353 break;
1354 case GeneratorExp_kind:
1355 if (!symtable_visit_genexp(st, e))
1356 return 0;
1357 break;
1358 case ListComp_kind:
1359 if (!symtable_visit_listcomp(st, e))
1360 return 0;
1361 break;
1362 case SetComp_kind:
1363 if (!symtable_visit_setcomp(st, e))
1364 return 0;
1365 break;
1366 case DictComp_kind:
1367 if (!symtable_visit_dictcomp(st, e))
1368 return 0;
1369 break;
1370 case Yield_kind:
1371 if (e->v.Yield.value)
1372 VISIT(st, expr, e->v.Yield.value);
1373 st->st_cur->ste_generator = 1;
1374 if (st->st_cur->ste_returns_value) {
1375 PyErr_SetString(PyExc_SyntaxError,
1376 RETURN_VAL_IN_GENERATOR);
1377 PyErr_SyntaxLocation(st->st_filename,
1378 e->lineno);
1379 return 0;
1380 }
1381 break;
1382 case Compare_kind:
1383 VISIT(st, expr, e->v.Compare.left);
1384 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1385 break;
1386 case Call_kind:
1387 VISIT(st, expr, e->v.Call.func);
1388 VISIT_SEQ(st, expr, e->v.Call.args);
1389 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1390 if (e->v.Call.starargs)
1391 VISIT(st, expr, e->v.Call.starargs);
1392 if (e->v.Call.kwargs)
1393 VISIT(st, expr, e->v.Call.kwargs);
1394 break;
1395 case Num_kind:
1396 case Str_kind:
1397 case Bytes_kind:
1398 case Ellipsis_kind:
1399 /* Nothing to do here. */
1400 break;
1401 /* The following exprs can be assignment targets. */
1402 case Attribute_kind:
1403 VISIT(st, expr, e->v.Attribute.value);
1404 break;
1405 case Subscript_kind:
1406 VISIT(st, expr, e->v.Subscript.value);
1407 VISIT(st, slice, e->v.Subscript.slice);
1408 break;
1409 case Starred_kind:
1410 VISIT(st, expr, e->v.Starred.value);
1411 break;
1412 case Name_kind:
1413 if (!symtable_add_def(st, e->v.Name.id,
1414 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1415 return 0;
1416 /* Special-case super: it counts as a use of __class__ */
1417 if (e->v.Name.ctx == Load &&
1418 st->st_cur->ste_type == FunctionBlock &&
1419 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1420 if (!GET_IDENTIFIER(__class__) ||
1421 !symtable_add_def(st, __class__, USE))
1422 return 0;
1423 }
1424 break;
1425 /* child nodes of List and Tuple will have expr_context set */
1426 case List_kind:
1427 VISIT_SEQ(st, expr, e->v.List.elts);
1428 break;
1429 case Tuple_kind:
1430 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1431 break;
1432 }
1433 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434}
1435
1436static int
1437symtable_implicit_arg(struct symtable *st, int pos)
1438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1440 if (id == NULL)
1441 return 0;
1442 if (!symtable_add_def(st, id, DEF_PARAM)) {
1443 Py_DECREF(id);
1444 return 0;
1445 }
1446 Py_DECREF(id);
1447 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448}
1449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001451symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 if (!args)
1456 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 for (i = 0; i < asdl_seq_LEN(args); i++) {
1459 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1460 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1461 return 0;
1462 }
1463
1464 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465}
1466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001468symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 if (!args)
1473 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 for (i = 0; i < asdl_seq_LEN(args); i++) {
1476 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1477 if (arg->annotation)
1478 VISIT(st, expr, arg->annotation);
1479 }
1480
1481 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001482}
1483
Neal Norwitzc1505362006-12-28 06:47:50 +00001484static int
1485symtable_visit_annotations(struct symtable *st, stmt_ty s)
1486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 arguments_ty a = s->v.FunctionDef.args;
1488
1489 if (a->args && !symtable_visit_argannotations(st, a->args))
1490 return 0;
1491 if (a->varargannotation)
1492 VISIT(st, expr, a->varargannotation);
1493 if (a->kwargannotation)
1494 VISIT(st, expr, a->kwargannotation);
1495 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1496 return 0;
1497 if (s->v.FunctionDef.returns)
1498 VISIT(st, expr, s->v.FunctionDef.returns);
1499 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500}
1501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503symtable_visit_arguments(struct symtable *st, arguments_ty a)
1504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 /* skip default arguments inside function block
1506 XXX should ast be different?
1507 */
1508 if (a->args && !symtable_visit_params(st, a->args))
1509 return 0;
1510 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1511 return 0;
1512 if (a->vararg) {
1513 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1514 return 0;
1515 st->st_cur->ste_varargs = 1;
1516 }
1517 if (a->kwarg) {
1518 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1519 return 0;
1520 st->st_cur->ste_varkeywords = 1;
1521 }
1522 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523}
1524
1525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 if (eh->v.ExceptHandler.type)
1530 VISIT(st, expr, eh->v.ExceptHandler.type);
1531 if (eh->v.ExceptHandler.name)
1532 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1533 return 0;
1534 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1535 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536}
1537
1538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540symtable_visit_alias(struct symtable *st, alias_ty a)
1541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001543 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 dotted package name (e.g. spam.eggs)
1545 */
1546 PyObject *store_name;
1547 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1548 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1549 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
1550 if (dot) {
1551 store_name = PyUnicode_FromUnicode(base, dot - base);
1552 if (!store_name)
1553 return 0;
1554 }
1555 else {
1556 store_name = name;
1557 Py_INCREF(store_name);
1558 }
1559 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1560 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1561 Py_DECREF(store_name);
1562 return r;
1563 }
1564 else {
1565 if (st->st_cur->ste_type != ModuleBlock) {
1566 int lineno = st->st_cur->ste_lineno;
1567 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1568 PyErr_SyntaxLocation(st->st_filename, lineno);
1569 Py_DECREF(store_name);
1570 return 0;
1571 }
1572 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1573 Py_DECREF(store_name);
1574 return 1;
1575 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576}
1577
1578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 VISIT(st, expr, lc->target);
1583 VISIT(st, expr, lc->iter);
1584 VISIT_SEQ(st, expr, lc->ifs);
1585 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586}
1587
1588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590symtable_visit_keyword(struct symtable *st, keyword_ty k)
1591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 VISIT(st, expr, k->value);
1593 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594}
1595
1596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598symtable_visit_slice(struct symtable *st, slice_ty s)
1599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 switch (s->kind) {
1601 case Slice_kind:
1602 if (s->v.Slice.lower)
1603 VISIT(st, expr, s->v.Slice.lower)
1604 if (s->v.Slice.upper)
1605 VISIT(st, expr, s->v.Slice.upper)
1606 if (s->v.Slice.step)
1607 VISIT(st, expr, s->v.Slice.step)
1608 break;
1609 case ExtSlice_kind:
1610 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1611 break;
1612 case Index_kind:
1613 VISIT(st, expr, s->v.Index.value)
1614 break;
1615 }
1616 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617}
1618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001620symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001621 identifier scope_name, asdl_seq *generators,
1622 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 int is_generator = (e->kind == GeneratorExp_kind);
1625 int needs_tmp = !is_generator;
1626 comprehension_ty outermost = ((comprehension_ty)
1627 asdl_seq_GET(generators, 0));
1628 /* Outermost iterator is evaluated in current scope */
1629 VISIT(st, expr, outermost->iter);
1630 /* Create comprehension scope for the rest */
1631 if (!scope_name ||
1632 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, e->lineno)) {
1633 return 0;
1634 }
1635 st->st_cur->ste_generator = is_generator;
1636 /* Outermost iter is received as an argument */
1637 if (!symtable_implicit_arg(st, 0)) {
1638 symtable_exit_block(st, (void *)e);
1639 return 0;
1640 }
1641 /* Allocate temporary name if needed */
1642 if (needs_tmp && !symtable_new_tmpname(st)) {
1643 symtable_exit_block(st, (void *)e);
1644 return 0;
1645 }
1646 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1647 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1648 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1649 generators, 1, (void*)e);
1650 if (value)
1651 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1652 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1653 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001657symtable_visit_genexp(struct symtable *st, expr_ty e)
1658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1660 e->v.GeneratorExp.generators,
1661 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001662}
1663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001665symtable_visit_listcomp(struct symtable *st, expr_ty e)
1666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1668 e->v.ListComp.generators,
1669 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001670}
1671
1672static int
1673symtable_visit_setcomp(struct symtable *st, expr_ty e)
1674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1676 e->v.SetComp.generators,
1677 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001678}
1679
1680static int
1681symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1684 e->v.DictComp.generators,
1685 e->v.DictComp.key,
1686 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001687}