blob: 15ba6b5e2f0c4931a22a5d35073d09abd258a3d9 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
3#include "code.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005#include "structmember.h"
6
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#define GLOBAL_AFTER_ASSIGN \
9"name '%.400s' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000010
Jeremy Hylton81e95022007-02-27 06:50:52 +000011#define NONLOCAL_AFTER_ASSIGN \
12"name '%.400s' is assigned to before nonlocal declaration"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_USE \
15"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_USE \
18"name '%.400s' is used prior to nonlocal declaration"
19
Neal Norwitz5d0ad502005-12-19 04:27:42 +000020#define IMPORT_STAR_WARNING "import * only allowed at module level"
21
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000022#define RETURN_VAL_IN_GENERATOR \
23 "'return' with argument inside generator"
Neal Norwitz5d0ad502005-12-19 04:27:42 +000024
Benjamin Peterson55e00f22008-08-17 18:02:44 +000025
Neal Norwitz090b3dd2006-02-28 22:36:46 +000026static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000027ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000028 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PySTEntryObject *ste = NULL;
31 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 k = PyLong_FromVoidPtr(key);
34 if (k == NULL)
35 goto fail;
36 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
37 if (ste == NULL)
38 goto fail;
39 ste->ste_table = st;
40 ste->ste_id = k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 ste->ste_name = name;
43 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 ste->ste_symbols = NULL;
46 ste->ste_varnames = NULL;
47 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 ste->ste_symbols = PyDict_New();
50 if (ste->ste_symbols == NULL)
51 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 ste->ste_varnames = PyList_New(0);
54 if (ste->ste_varnames == NULL)
55 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 ste->ste_children = PyList_New(0);
58 if (ste->ste_children == NULL)
59 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 ste->ste_type = block;
62 ste->ste_unoptimized = 0;
63 ste->ste_nested = 0;
64 ste->ste_free = 0;
65 ste->ste_varargs = 0;
66 ste->ste_varkeywords = 0;
67 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000068 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000069 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000071 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (st->st_cur != NULL &&
74 (st->st_cur->ste_nested ||
75 st->st_cur->ste_type == FunctionBlock))
76 ste->ste_nested = 1;
77 ste->ste_child_free = 0;
78 ste->ste_generator = 0;
79 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
82 goto fail;
83
84 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 Py_XDECREF(ste);
87 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088}
89
90static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
94 ste->ste_name,
95 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096}
97
98static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 ste->ste_table = NULL;
102 Py_XDECREF(ste->ste_id);
103 Py_XDECREF(ste->ste_name);
104 Py_XDECREF(ste->ste_symbols);
105 Py_XDECREF(ste->ste_varnames);
106 Py_XDECREF(ste->ste_children);
107 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108}
109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000111
Guido van Rossum6f799372001-09-20 20:46:19 +0000112static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 {"id", T_OBJECT, OFF(ste_id), READONLY},
114 {"name", T_OBJECT, OFF(ste_name), READONLY},
115 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
116 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
117 {"children", T_OBJECT, OFF(ste_children), READONLY},
118 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
119 {"nested", T_INT, OFF(ste_nested), READONLY},
120 {"type", T_INT, OFF(ste_type), READONLY},
121 {"lineno", T_INT, OFF(ste_lineno), READONLY},
122 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000123};
124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 PyVarObject_HEAD_INIT(&PyType_Type, 0)
127 "symtable entry",
128 sizeof(PySTEntryObject),
129 0,
130 (destructor)ste_dealloc, /* tp_dealloc */
131 0, /* tp_print */
132 0, /* tp_getattr */
133 0, /* tp_setattr */
134 0, /* tp_reserved */
135 (reprfunc)ste_repr, /* tp_repr */
136 0, /* tp_as_number */
137 0, /* tp_as_sequence */
138 0, /* tp_as_mapping */
139 0, /* tp_hash */
140 0, /* tp_call */
141 0, /* tp_str */
142 PyObject_GenericGetAttr, /* tp_getattro */
143 0, /* tp_setattro */
144 0, /* tp_as_buffer */
145 Py_TPFLAGS_DEFAULT, /* tp_flags */
146 0, /* tp_doc */
147 0, /* tp_traverse */
148 0, /* tp_clear */
149 0, /* tp_richcompare */
150 0, /* tp_weaklistoffset */
151 0, /* tp_iter */
152 0, /* tp_iternext */
153 0, /* tp_methods */
154 ste_memberlist, /* tp_members */
155 0, /* tp_getset */
156 0, /* tp_base */
157 0, /* tp_dict */
158 0, /* tp_descr_get */
159 0, /* tp_descr_set */
160 0, /* tp_dictoffset */
161 0, /* tp_init */
162 0, /* tp_alloc */
163 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000164};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165
166static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000167static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000169 _Py_block_ty block, void *ast, int lineno,
170 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171static int symtable_exit_block(struct symtable *st, void *ast);
172static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
173static int symtable_visit_expr(struct symtable *st, expr_ty s);
174static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000175static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
176static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000177static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178static int symtable_visit_arguments(struct symtable *st, arguments_ty);
179static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
180static int symtable_visit_alias(struct symtable *st, alias_ty);
181static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
182static int symtable_visit_keyword(struct symtable *st, keyword_ty);
183static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000184static int symtable_visit_params(struct symtable *st, asdl_seq *args);
185static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000187static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188
189
Nick Coghlan650f0d02007-04-15 12:05:43 +0000190static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
192 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
194#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
197#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000198"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
200static struct symtable *
201symtable_new(void)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
206 if (st == NULL)
207 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 st->st_filename = NULL;
210 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if ((st->st_stack = PyList_New(0)) == NULL)
213 goto fail;
214 if ((st->st_blocks = PyDict_New()) == NULL)
215 goto fail;
216 st->st_cur = NULL;
217 st->st_private = NULL;
218 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 PySymtable_Free(st);
221 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222}
223
224struct symtable *
225PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 struct symtable *st = symtable_new();
228 asdl_seq *seq;
229 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 if (st == NULL)
232 return st;
233 st->st_filename = filename;
234 st->st_future = future;
235 /* Make the initial symbol information gathering pass */
236 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000237 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 PySymtable_Free(st);
239 return NULL;
240 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 st->st_top = st->st_cur;
243 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
244 switch (mod->kind) {
245 case Module_kind:
246 seq = mod->v.Module.body;
247 for (i = 0; i < asdl_seq_LEN(seq); i++)
248 if (!symtable_visit_stmt(st,
249 (stmt_ty)asdl_seq_GET(seq, i)))
250 goto error;
251 break;
252 case Expression_kind:
253 if (!symtable_visit_expr(st, mod->v.Expression.body))
254 goto error;
255 break;
256 case Interactive_kind:
257 seq = mod->v.Interactive.body;
258 for (i = 0; i < asdl_seq_LEN(seq); i++)
259 if (!symtable_visit_stmt(st,
260 (stmt_ty)asdl_seq_GET(seq, i)))
261 goto error;
262 break;
263 case Suite_kind:
264 PyErr_SetString(PyExc_RuntimeError,
265 "this compiler does not handle Suites");
266 goto error;
267 }
268 if (!symtable_exit_block(st, (void *)mod)) {
269 PySymtable_Free(st);
270 return NULL;
271 }
272 /* Make the second symbol analysis pass */
273 if (symtable_analyze(st))
274 return st;
275 PySymtable_Free(st);
276 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 (void) symtable_exit_block(st, (void *)mod);
279 PySymtable_Free(st);
280 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281}
282
283void
284PySymtable_Free(struct symtable *st)
285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 Py_XDECREF(st->st_blocks);
287 Py_XDECREF(st->st_stack);
288 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289}
290
291PySTEntryObject *
292PySymtable_Lookup(struct symtable *st, void *key)
293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 k = PyLong_FromVoidPtr(key);
297 if (k == NULL)
298 return NULL;
299 v = PyDict_GetItem(st->st_blocks, k);
300 if (v) {
301 assert(PySTEntry_Check(v));
302 Py_INCREF(v);
303 }
304 else {
305 PyErr_SetString(PyExc_KeyError,
306 "unknown symbol table entry");
307 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 Py_DECREF(k);
310 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311}
312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314PyST_GetScope(PySTEntryObject *ste, PyObject *name)
315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
317 if (!v)
318 return 0;
319 assert(PyLong_Check(v));
320 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321}
322
323
324/* Analyze raw symbol information to determine scope of each name.
325
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000326 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332 explicit global is declared with the global statement. An implicit
333 global is a free variable for which the compiler has found no binding
334 in an enclosing function scope. The implicit global is either a global
335 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
336 to handle these names to implement slightly odd semantics. In such a
337 block, the name is treated as global until it is assigned to; then it
338 is treated as a local.
339
340 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000341 The first pass collects raw facts from the AST via the symtable_visit_*
342 functions: the name is a parameter here, the name is used but not defined
343 here, etc. The second pass analyzes these facts during a pass over the
344 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345
346 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000348 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000349 Names which are explicitly declared nonlocal must exist in this set of
350 visible names - if they do not, a syntax error is raised. After doing
351 the local analysis, it analyzes each of its child blocks using an
352 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353
Nick Coghlan650f0d02007-04-15 12:05:43 +0000354 The children update the free variable set. If a local variable is added to
355 the free variable set by the child, the variable is marked as a cell. The
356 function object being defined must provide runtime storage for the variable
357 that may outlive the function's frame. Cell variables are removed from the
358 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000359
Nick Coghlan650f0d02007-04-15 12:05:43 +0000360 During analysis, the names are:
361 symbols: dict mapping from symbol names to flag values (including offset scope values)
362 scopes: dict mapping from symbol names to scope values (no offset)
363 local: set of all symbol names local to the current scope
364 bound: set of all symbol names local to a containing function scope
365 free: set of all symbol names referenced but not bound in child scopes
366 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367*/
368
369#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 PyObject *o = PyLong_FromLong(I); \
371 if (!o) \
372 return 0; \
373 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
374 Py_DECREF(o); \
375 return 0; \
376 } \
377 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378}
379
380/* Decide on scope of name, given flags.
381
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000382 The namespace dictionaries may be modified to record information
383 about the new name. For example, a new global will add an entry to
384 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385*/
386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000388analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 PyObject *bound, PyObject *local, PyObject *free,
390 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000391{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (flags & DEF_GLOBAL) {
393 if (flags & DEF_PARAM) {
394 PyErr_Format(PyExc_SyntaxError,
395 "name '%U' is parameter and global",
396 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000397 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
398 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399
400 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000401 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (flags & DEF_NONLOCAL) {
403 PyErr_Format(PyExc_SyntaxError,
404 "name '%U' is nonlocal and global",
405 name);
406 return 0;
407 }
408 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
409 if (PySet_Add(global, name) < 0)
410 return 0;
411 if (bound && (PySet_Discard(bound, name) < 0))
412 return 0;
413 return 1;
414 }
415 if (flags & DEF_NONLOCAL) {
416 if (flags & DEF_PARAM) {
417 PyErr_Format(PyExc_SyntaxError,
418 "name '%U' is parameter and nonlocal",
419 name);
420 return 0;
421 }
422 if (!bound) {
423 PyErr_Format(PyExc_SyntaxError,
424 "nonlocal declaration not allowed at module level");
425 return 0;
426 }
427 if (!PySet_Contains(bound, name)) {
428 PyErr_Format(PyExc_SyntaxError,
429 "no binding for nonlocal '%U' found",
430 name);
431
432 return 0;
433 }
434 SET_SCOPE(scopes, name, FREE);
435 ste->ste_free = 1;
436 return PySet_Add(free, name) >= 0;
437 }
438 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000439 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (PySet_Add(local, name) < 0)
441 return 0;
442 if (PySet_Discard(global, name) < 0)
443 return 0;
444 return 1;
445 }
446 /* If an enclosing block has a binding for this name, it
447 is a free variable rather than a global variable.
448 Note that having a non-NULL bound implies that the block
449 is nested.
450 */
451 if (bound && PySet_Contains(bound, name)) {
452 SET_SCOPE(scopes, name, FREE);
453 ste->ste_free = 1;
454 return PySet_Add(free, name) >= 0;
455 }
456 /* If a parent has a global statement, then call it global
457 explicit? It could also be global implicit.
458 */
459 if (global && PySet_Contains(global, name)) {
460 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
461 return 1;
462 }
463 if (ste->ste_nested)
464 ste->ste_free = 1;
465 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
466 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467}
468
469#undef SET_SCOPE
470
471/* If a name is defined in free and also in locals, then this block
472 provides the binding for the free variable. The name should be
473 marked CELL in this block and removed from the free list.
474
475 Note that the current block's free variables are included in free.
476 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000477
Martin v. Löwis2673a572007-10-29 19:54:24 +0000478 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000479 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480*/
481
482static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000483analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 PyObject *name, *v, *v_cell;
486 int success = 0;
487 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 v_cell = PyLong_FromLong(CELL);
490 if (!v_cell)
491 return 0;
492 while (PyDict_Next(scopes, &pos, &name, &v)) {
493 long scope;
494 assert(PyLong_Check(v));
495 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000496 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 continue;
498 if (!PySet_Contains(free, name))
499 continue;
500 if (restricted != NULL &&
501 PyUnicode_CompareWithASCIIString(name, restricted))
502 continue;
503 /* Replace LOCAL with CELL for this name, and remove
504 from free. It is safe to replace the value of name
505 in the dict, because it will not cause a resize.
506 */
507 if (PyDict_SetItem(scopes, name, v_cell) < 0)
508 goto error;
509 if (PySet_Discard(free, name) < 0)
510 goto error;
511 }
512 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 Py_DECREF(v_cell);
515 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516}
517
518/* Check for illegal statements in unoptimized namespaces */
519static int
520check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
524 || !(ste->ste_free || ste->ste_child_free))
525 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 trailer = (ste->ste_child_free ?
528 "contains a nested function with free variables" :
529 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 switch (ste->ste_unoptimized) {
532 case OPT_TOPLEVEL: /* import * at top-level is fine */
533 return 1;
534 case OPT_IMPORT_STAR:
535 PyErr_Format(PyExc_SyntaxError,
536 "import * is not allowed in function '%U' because it %s",
537 ste->ste_name, trailer);
538 break;
539 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000541 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
542 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544}
545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546/* Enter the final scope information into the ste_symbols dict.
547 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548 * All arguments are dicts. Modifies symbols, others are read-only.
549*/
550static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000552 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 PyObject *name = NULL, *itr = NULL;
555 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
556 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 /* Update scope information for all symbols in this scope */
559 while (PyDict_Next(symbols, &pos, &name, &v)) {
560 long scope, flags;
561 assert(PyLong_Check(v));
562 flags = PyLong_AS_LONG(v);
563 v_scope = PyDict_GetItem(scopes, name);
564 assert(v_scope && PyLong_Check(v_scope));
565 scope = PyLong_AS_LONG(v_scope);
566 flags |= (scope << SCOPE_OFFSET);
567 v_new = PyLong_FromLong(flags);
568 if (!v_new)
569 return 0;
570 if (PyDict_SetItem(symbols, name, v_new) < 0) {
571 Py_DECREF(v_new);
572 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 Py_DECREF(v_new);
575 }
576
577 /* Record not yet resolved free variables from children (if any) */
578 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
579 if (!v_free)
580 return 0;
581
582 itr = PyObject_GetIter(free);
583 if (!itr)
584 goto error;
585
586 while ((name = PyIter_Next(itr))) {
587 v = PyDict_GetItem(symbols, name);
588
589 /* Handle symbol that already exists in this scope */
590 if (v) {
591 /* Handle a free variable in a method of
592 the class that has the same name as a local
593 or global in the class scope.
594 */
595 if (classflag &&
596 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
597 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
598 v_new = PyLong_FromLong(flags);
599 if (!v_new) {
600 goto error;
601 }
602 if (PyDict_SetItem(symbols, name, v_new) < 0) {
603 Py_DECREF(v_new);
604 goto error;
605 }
606 Py_DECREF(v_new);
607 }
608 /* It's a cell, or already free in this scope */
609 Py_DECREF(name);
610 continue;
611 }
612 /* Handle global symbol */
613 if (!PySet_Contains(bound, name)) {
614 Py_DECREF(name);
615 continue; /* it's a global */
616 }
617 /* Propagate new free symbol up the lexical stack */
618 if (PyDict_SetItem(symbols, name, v_free) < 0) {
619 goto error;
620 }
621 Py_DECREF(name);
622 }
623 Py_DECREF(itr);
624 Py_DECREF(v_free);
625 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000626error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 Py_XDECREF(v_free);
628 Py_XDECREF(itr);
629 Py_XDECREF(name);
630 return 0;
631}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632
633/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000634
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635 Arguments:
636 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000637 bound -- set of variables bound in enclosing scopes (input). bound
638 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 free -- set of free variables in enclosed scopes (output)
640 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000641
642 The implementation uses two mutually recursive functions,
643 analyze_block() and analyze_child_block(). analyze_block() is
644 responsible for analyzing the individual names defined in a block.
645 analyze_child_block() prepares temporary namespace dictionaries
646 used to evaluated nested blocks.
647
648 The two functions exist because a child block should see the name
649 bindings of its enclosing blocks, but those bindings should not
650 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651*/
652
653static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
655 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000656
657static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
659 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
662 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
663 PyObject *temp;
664 int i, success = 0;
665 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 local = PySet_New(NULL); /* collect new names bound in block */
668 if (!local)
669 goto error;
670 scopes = PyDict_New(); /* collect scopes defined for each name */
671 if (!scopes)
672 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 /* Allocate new global and bound variable dictionaries. These
675 dictionaries hold the names visible in nested blocks. For
676 ClassBlocks, the bound and global names are initialized
677 before analyzing names, because class bindings aren't
678 visible in methods. For other blocks, they are initialized
679 after names are analyzed.
680 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 /* TODO(jhylton): Package these dicts in a struct so that we
683 can write reasonable helper functions?
684 */
685 newglobal = PySet_New(NULL);
686 if (!newglobal)
687 goto error;
688 newfree = PySet_New(NULL);
689 if (!newfree)
690 goto error;
691 newbound = PySet_New(NULL);
692 if (!newbound)
693 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 /* Class namespace has no effect on names visible in
696 nested functions, so populate the global and bound
697 sets to be passed to child blocks before analyzing
698 this one.
699 */
700 if (ste->ste_type == ClassBlock) {
701 /* Pass down known globals */
702 temp = PyNumber_InPlaceOr(newglobal, global);
703 if (!temp)
704 goto error;
705 Py_DECREF(temp);
706 /* Pass down previously bound symbols */
707 if (bound) {
708 temp = PyNumber_InPlaceOr(newbound, bound);
709 if (!temp)
710 goto error;
711 Py_DECREF(temp);
712 }
713 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
716 long flags = PyLong_AS_LONG(v);
717 if (!analyze_name(ste, scopes, name, flags,
718 bound, local, free, global))
719 goto error;
720 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 /* Populate global and bound sets to be passed to children. */
723 if (ste->ste_type != ClassBlock) {
724 /* Add function locals to bound set */
725 if (ste->ste_type == FunctionBlock) {
726 temp = PyNumber_InPlaceOr(newbound, local);
727 if (!temp)
728 goto error;
729 Py_DECREF(temp);
730 }
731 /* Pass down previously bound symbols */
732 if (bound) {
733 temp = PyNumber_InPlaceOr(newbound, bound);
734 if (!temp)
735 goto error;
736 Py_DECREF(temp);
737 }
738 /* Pass down known globals */
739 temp = PyNumber_InPlaceOr(newglobal, global);
740 if (!temp)
741 goto error;
742 Py_DECREF(temp);
743 }
744 else {
745 /* Special-case __class__ */
746 if (!GET_IDENTIFIER(__class__))
747 goto error;
748 assert(PySet_Contains(local, __class__) == 1);
749 if (PySet_Add(newbound, __class__) < 0)
750 goto error;
751 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 /* Recursively call analyze_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 newbound, newglobal now contain the names visible in
756 nested blocks. The free variables in the children will
757 be collected in allfree.
758 */
759 allfree = PySet_New(NULL);
760 if (!allfree)
761 goto error;
762 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
763 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
764 PySTEntryObject* entry;
765 assert(c && PySTEntry_Check(c));
766 entry = (PySTEntryObject*)c;
767 if (!analyze_child_block(entry, newbound, newfree, newglobal,
768 allfree))
769 goto error;
770 /* Check if any children have free variables */
771 if (entry->ste_free || entry->ste_child_free)
772 ste->ste_child_free = 1;
773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 temp = PyNumber_InPlaceOr(newfree, allfree);
776 if (!temp)
777 goto error;
778 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 /* Check if any local variables must be converted to cell variables */
781 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
782 NULL))
783 goto error;
784 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
785 "__class__"))
786 goto error;
787 /* Records the results of the analysis in the symbol table entry */
788 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
789 ste->ste_type == ClassBlock))
790 goto error;
791 if (!check_unoptimized(ste))
792 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 temp = PyNumber_InPlaceOr(free, newfree);
795 if (!temp)
796 goto error;
797 Py_DECREF(temp);
798 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 Py_XDECREF(scopes);
801 Py_XDECREF(local);
802 Py_XDECREF(newbound);
803 Py_XDECREF(newglobal);
804 Py_XDECREF(newfree);
805 Py_XDECREF(allfree);
806 if (!success)
807 assert(PyErr_Occurred());
808 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809}
810
811static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
813 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
816 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 These dictionary are used by all blocks enclosed by the
821 current block. The analyze_block() call modifies these
822 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 */
825 temp_bound = PySet_New(bound);
826 if (!temp_bound)
827 goto error;
828 temp_free = PySet_New(free);
829 if (!temp_free)
830 goto error;
831 temp_global = PySet_New(global);
832 if (!temp_global)
833 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
836 goto error;
837 temp = PyNumber_InPlaceOr(child_free, temp_free);
838 if (!temp)
839 goto error;
840 Py_DECREF(temp);
841 Py_DECREF(temp_bound);
842 Py_DECREF(temp_free);
843 Py_DECREF(temp_global);
844 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000845 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 Py_XDECREF(temp_bound);
847 Py_XDECREF(temp_free);
848 Py_XDECREF(temp_global);
849 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000850}
851
852static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853symtable_analyze(struct symtable *st)
854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 PyObject *free, *global;
856 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 free = PySet_New(NULL);
859 if (!free)
860 return 0;
861 global = PySet_New(NULL);
862 if (!global) {
863 Py_DECREF(free);
864 return 0;
865 }
866 r = analyze_block(st->st_top, NULL, free, global);
867 Py_DECREF(free);
868 Py_DECREF(global);
869 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870}
871
872
873static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000874symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
877 lineno, NULL, NULL) < 0) {
878 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
879 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000880 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
881 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 }
883 return 0;
884 }
885 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886}
887
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000888/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889 This reference is released when the block is exited, via the DECREF
890 in symtable_exit_block().
891*/
892
893static int
894symtable_exit_block(struct symtable *st, void *ast)
895{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 Py_ssize_t end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 Py_CLEAR(st->st_cur);
899 end = PyList_GET_SIZE(st->st_stack) - 1;
900 if (end >= 0) {
901 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
902 end);
903 if (st->st_cur == NULL)
904 return 0;
905 Py_INCREF(st->st_cur);
906 if (PySequence_DelItem(st->st_stack, end) < 0)
907 return 0;
908 }
909 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910}
911
912static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000914 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 PySTEntryObject *prev = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 if (st->st_cur) {
919 prev = st->st_cur;
920 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
921 return 0;
922 }
923 Py_DECREF(st->st_cur);
924 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000925 st->st_cur = ste_new(st, name, block, ast, lineno, col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 if (st->st_cur == NULL)
927 return 0;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000928 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 st->st_global = st->st_cur->ste_symbols;
930 if (prev) {
931 if (PyList_Append(prev->ste_children,
932 (PyObject *)st->st_cur) < 0) {
933 return 0;
934 }
935 }
936 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937}
938
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000939static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940symtable_lookup(struct symtable *st, PyObject *name)
941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 PyObject *o;
943 PyObject *mangled = _Py_Mangle(st->st_private, name);
944 if (!mangled)
945 return 0;
946 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
947 Py_DECREF(mangled);
948 if (!o)
949 return 0;
950 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951}
952
953static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 PyObject *o;
957 PyObject *dict;
958 long val;
959 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960
Jeremy Hylton81e95022007-02-27 06:50:52 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 if (!mangled)
963 return 0;
964 dict = st->st_cur->ste_symbols;
965 if ((o = PyDict_GetItem(dict, mangled))) {
966 val = PyLong_AS_LONG(o);
967 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
968 /* Is it better to use 'mangled' or 'name' here? */
969 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000970 PyErr_SyntaxLocationEx(st->st_filename,
971 st->st_cur->ste_lineno,
972 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 goto error;
974 }
975 val |= flag;
976 } else
977 val = flag;
978 o = PyLong_FromLong(val);
979 if (o == NULL)
980 goto error;
981 if (PyDict_SetItem(dict, mangled, o) < 0) {
982 Py_DECREF(o);
983 goto error;
984 }
985 Py_DECREF(o);
986
987 if (flag & DEF_PARAM) {
988 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
989 goto error;
990 } else if (flag & DEF_GLOBAL) {
991 /* XXX need to update DEF_GLOBAL for other flags too;
992 perhaps only DEF_FREE_GLOBAL */
993 val = flag;
994 if ((o = PyDict_GetItem(st->st_global, mangled))) {
995 val |= PyLong_AS_LONG(o);
996 }
997 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 goto error;
1000 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1001 Py_DECREF(o);
1002 goto error;
1003 }
1004 Py_DECREF(o);
1005 }
1006 Py_DECREF(mangled);
1007 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001008
1009error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 Py_DECREF(mangled);
1011 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012}
1013
1014/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1015 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 function.
1017
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1019 useful if the first node in the sequence requires special treatment.
1020*/
1021
1022#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 if (!symtable_visit_ ## TYPE((ST), (V))) \
1024 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001025
1026#define VISIT_IN_BLOCK(ST, TYPE, V, S) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 if (!symtable_visit_ ## TYPE((ST), (V))) { \
1028 symtable_exit_block((ST), (S)); \
1029 return 0; \
1030 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001031
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 int i; \
1034 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1035 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1036 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1037 if (!symtable_visit_ ## TYPE((ST), elt)) \
1038 return 0; \
1039 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001041
1042#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 int i; \
1044 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1045 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1046 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1047 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1048 symtable_exit_block((ST), (S)); \
1049 return 0; \
1050 } \
1051 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001052}
1053
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 int i; \
1056 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1057 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1058 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1059 if (!symtable_visit_ ## TYPE((ST), elt)) \
1060 return 0; \
1061 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001063
1064#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 int i; \
1066 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1067 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1068 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1069 if (!symtable_visit_ ## TYPE((ST), elt)) { \
1070 symtable_exit_block((ST), (S)); \
1071 return 0; \
1072 } \
1073 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001074}
1075
Guido van Rossum4f72a782006-10-27 23:31:49 +00001076#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 int i = 0; \
1078 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1079 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1080 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1081 if (!elt) continue; /* can be NULL */ \
1082 if (!symtable_visit_expr((ST), elt)) \
1083 return 0; \
1084 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001085}
1086
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001088symtable_new_tmpname(struct symtable *st)
1089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 char tmpname[256];
1091 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1094 ++st->st_cur->ste_tmpname);
1095 tmp = PyUnicode_InternFromString(tmpname);
1096 if (!tmp)
1097 return 0;
1098 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1099 return 0;
1100 Py_DECREF(tmp);
1101 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001102}
1103
Guido van Rossum4f72a782006-10-27 23:31:49 +00001104
Guido van Rossumc2e20742006-02-27 22:32:47 +00001105static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106symtable_visit_stmt(struct symtable *st, stmt_ty s)
1107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 switch (s->kind) {
1109 case FunctionDef_kind:
1110 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1111 return 0;
1112 if (s->v.FunctionDef.args->defaults)
1113 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1114 if (s->v.FunctionDef.args->kw_defaults)
1115 VISIT_KWONLYDEFAULTS(st,
1116 s->v.FunctionDef.args->kw_defaults);
1117 if (!symtable_visit_annotations(st, s))
1118 return 0;
1119 if (s->v.FunctionDef.decorator_list)
1120 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1121 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001122 FunctionBlock, (void *)s, s->lineno,
1123 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 return 0;
1125 VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s);
1126 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s);
1127 if (!symtable_exit_block(st, s))
1128 return 0;
1129 break;
1130 case ClassDef_kind: {
1131 PyObject *tmp;
1132 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1133 return 0;
1134 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1135 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1136 if (s->v.ClassDef.starargs)
1137 VISIT(st, expr, s->v.ClassDef.starargs);
1138 if (s->v.ClassDef.kwargs)
1139 VISIT(st, expr, s->v.ClassDef.kwargs);
1140 if (s->v.ClassDef.decorator_list)
1141 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1142 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001143 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 return 0;
1145 if (!GET_IDENTIFIER(__class__) ||
1146 !symtable_add_def(st, __class__, DEF_LOCAL) ||
1147 !GET_IDENTIFIER(__locals__) ||
1148 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1149 symtable_exit_block(st, s);
1150 return 0;
1151 }
1152 tmp = st->st_private;
1153 st->st_private = s->v.ClassDef.name;
1154 VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s);
1155 st->st_private = tmp;
1156 if (!symtable_exit_block(st, s))
1157 return 0;
1158 break;
1159 }
1160 case Return_kind:
1161 if (s->v.Return.value) {
1162 VISIT(st, expr, s->v.Return.value);
1163 st->st_cur->ste_returns_value = 1;
1164 if (st->st_cur->ste_generator) {
1165 PyErr_SetString(PyExc_SyntaxError,
1166 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001167 PyErr_SyntaxLocationEx(st->st_filename,
1168 s->lineno,
1169 s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 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 */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001231 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001233 st->st_cur->ste_opt_col_offset = s->col_offset;
1234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 break;
1236 case ImportFrom_kind:
1237 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1238 /* XXX Don't have the lineno available inside
1239 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001240 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001242 st->st_cur->ste_opt_col_offset = s->col_offset;
1243 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 break;
1245 case Global_kind: {
1246 int i;
1247 asdl_seq *seq = s->v.Global.names;
1248 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1249 identifier name = (identifier)asdl_seq_GET(seq, i);
1250 char *c_name = _PyUnicode_AsString(name);
1251 long cur = symtable_lookup(st, name);
1252 if (cur < 0)
1253 return 0;
1254 if (cur & (DEF_LOCAL | USE)) {
1255 char buf[256];
1256 if (cur & DEF_LOCAL)
1257 PyOS_snprintf(buf, sizeof(buf),
1258 GLOBAL_AFTER_ASSIGN,
1259 c_name);
1260 else
1261 PyOS_snprintf(buf, sizeof(buf),
1262 GLOBAL_AFTER_USE,
1263 c_name);
1264 if (!symtable_warn(st, buf, s->lineno))
1265 return 0;
1266 }
1267 if (!symtable_add_def(st, name, DEF_GLOBAL))
1268 return 0;
1269 }
1270 break;
1271 }
1272 case Nonlocal_kind: {
1273 int i;
1274 asdl_seq *seq = s->v.Nonlocal.names;
1275 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1276 identifier name = (identifier)asdl_seq_GET(seq, i);
1277 char *c_name = _PyUnicode_AsString(name);
1278 long cur = symtable_lookup(st, name);
1279 if (cur < 0)
1280 return 0;
1281 if (cur & (DEF_LOCAL | USE)) {
1282 char buf[256];
1283 if (cur & DEF_LOCAL)
1284 PyOS_snprintf(buf, sizeof(buf),
1285 NONLOCAL_AFTER_ASSIGN,
1286 c_name);
1287 else
1288 PyOS_snprintf(buf, sizeof(buf),
1289 NONLOCAL_AFTER_USE,
1290 c_name);
1291 if (!symtable_warn(st, buf, s->lineno))
1292 return 0;
1293 }
1294 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1295 return 0;
1296 }
1297 break;
1298 }
1299 case Expr_kind:
1300 VISIT(st, expr, s->v.Expr.value);
1301 break;
1302 case Pass_kind:
1303 case Break_kind:
1304 case Continue_kind:
1305 /* nothing to do here */
1306 break;
1307 case With_kind:
1308 VISIT(st, expr, s->v.With.context_expr);
1309 if (s->v.With.optional_vars) {
1310 VISIT(st, expr, s->v.With.optional_vars);
1311 }
1312 VISIT_SEQ(st, stmt, s->v.With.body);
1313 break;
1314 }
1315 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316}
1317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319symtable_visit_expr(struct symtable *st, expr_ty e)
1320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 switch (e->kind) {
1322 case BoolOp_kind:
1323 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1324 break;
1325 case BinOp_kind:
1326 VISIT(st, expr, e->v.BinOp.left);
1327 VISIT(st, expr, e->v.BinOp.right);
1328 break;
1329 case UnaryOp_kind:
1330 VISIT(st, expr, e->v.UnaryOp.operand);
1331 break;
1332 case Lambda_kind: {
1333 if (!GET_IDENTIFIER(lambda))
1334 return 0;
1335 if (e->v.Lambda.args->defaults)
1336 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1337 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001338 FunctionBlock, (void *)e, e->lineno,
1339 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 return 0;
1341 VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e);
1342 VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e);
1343 if (!symtable_exit_block(st, (void *)e))
1344 return 0;
1345 break;
1346 }
1347 case IfExp_kind:
1348 VISIT(st, expr, e->v.IfExp.test);
1349 VISIT(st, expr, e->v.IfExp.body);
1350 VISIT(st, expr, e->v.IfExp.orelse);
1351 break;
1352 case Dict_kind:
1353 VISIT_SEQ(st, expr, e->v.Dict.keys);
1354 VISIT_SEQ(st, expr, e->v.Dict.values);
1355 break;
1356 case Set_kind:
1357 VISIT_SEQ(st, expr, e->v.Set.elts);
1358 break;
1359 case GeneratorExp_kind:
1360 if (!symtable_visit_genexp(st, e))
1361 return 0;
1362 break;
1363 case ListComp_kind:
1364 if (!symtable_visit_listcomp(st, e))
1365 return 0;
1366 break;
1367 case SetComp_kind:
1368 if (!symtable_visit_setcomp(st, e))
1369 return 0;
1370 break;
1371 case DictComp_kind:
1372 if (!symtable_visit_dictcomp(st, e))
1373 return 0;
1374 break;
1375 case Yield_kind:
1376 if (e->v.Yield.value)
1377 VISIT(st, expr, e->v.Yield.value);
1378 st->st_cur->ste_generator = 1;
1379 if (st->st_cur->ste_returns_value) {
1380 PyErr_SetString(PyExc_SyntaxError,
1381 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001382 PyErr_SyntaxLocationEx(st->st_filename,
1383 e->lineno, e->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 return 0;
1385 }
1386 break;
1387 case Compare_kind:
1388 VISIT(st, expr, e->v.Compare.left);
1389 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1390 break;
1391 case Call_kind:
1392 VISIT(st, expr, e->v.Call.func);
1393 VISIT_SEQ(st, expr, e->v.Call.args);
1394 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1395 if (e->v.Call.starargs)
1396 VISIT(st, expr, e->v.Call.starargs);
1397 if (e->v.Call.kwargs)
1398 VISIT(st, expr, e->v.Call.kwargs);
1399 break;
1400 case Num_kind:
1401 case Str_kind:
1402 case Bytes_kind:
1403 case Ellipsis_kind:
1404 /* Nothing to do here. */
1405 break;
1406 /* The following exprs can be assignment targets. */
1407 case Attribute_kind:
1408 VISIT(st, expr, e->v.Attribute.value);
1409 break;
1410 case Subscript_kind:
1411 VISIT(st, expr, e->v.Subscript.value);
1412 VISIT(st, slice, e->v.Subscript.slice);
1413 break;
1414 case Starred_kind:
1415 VISIT(st, expr, e->v.Starred.value);
1416 break;
1417 case Name_kind:
1418 if (!symtable_add_def(st, e->v.Name.id,
1419 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1420 return 0;
1421 /* Special-case super: it counts as a use of __class__ */
1422 if (e->v.Name.ctx == Load &&
1423 st->st_cur->ste_type == FunctionBlock &&
1424 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1425 if (!GET_IDENTIFIER(__class__) ||
1426 !symtable_add_def(st, __class__, USE))
1427 return 0;
1428 }
1429 break;
1430 /* child nodes of List and Tuple will have expr_context set */
1431 case List_kind:
1432 VISIT_SEQ(st, expr, e->v.List.elts);
1433 break;
1434 case Tuple_kind:
1435 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1436 break;
1437 }
1438 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439}
1440
1441static int
1442symtable_implicit_arg(struct symtable *st, int pos)
1443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1445 if (id == NULL)
1446 return 0;
1447 if (!symtable_add_def(st, id, DEF_PARAM)) {
1448 Py_DECREF(id);
1449 return 0;
1450 }
1451 Py_DECREF(id);
1452 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453}
1454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001456symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 if (!args)
1461 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 for (i = 0; i < asdl_seq_LEN(args); i++) {
1464 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1465 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1466 return 0;
1467 }
1468
1469 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470}
1471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001473symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 if (!args)
1478 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 for (i = 0; i < asdl_seq_LEN(args); i++) {
1481 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1482 if (arg->annotation)
1483 VISIT(st, expr, arg->annotation);
1484 }
1485
1486 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001487}
1488
Neal Norwitzc1505362006-12-28 06:47:50 +00001489static int
1490symtable_visit_annotations(struct symtable *st, stmt_ty s)
1491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 arguments_ty a = s->v.FunctionDef.args;
1493
1494 if (a->args && !symtable_visit_argannotations(st, a->args))
1495 return 0;
1496 if (a->varargannotation)
1497 VISIT(st, expr, a->varargannotation);
1498 if (a->kwargannotation)
1499 VISIT(st, expr, a->kwargannotation);
1500 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1501 return 0;
1502 if (s->v.FunctionDef.returns)
1503 VISIT(st, expr, s->v.FunctionDef.returns);
1504 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505}
1506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508symtable_visit_arguments(struct symtable *st, arguments_ty a)
1509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 /* skip default arguments inside function block
1511 XXX should ast be different?
1512 */
1513 if (a->args && !symtable_visit_params(st, a->args))
1514 return 0;
1515 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1516 return 0;
1517 if (a->vararg) {
1518 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1519 return 0;
1520 st->st_cur->ste_varargs = 1;
1521 }
1522 if (a->kwarg) {
1523 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1524 return 0;
1525 st->st_cur->ste_varkeywords = 1;
1526 }
1527 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528}
1529
1530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 if (eh->v.ExceptHandler.type)
1535 VISIT(st, expr, eh->v.ExceptHandler.type);
1536 if (eh->v.ExceptHandler.name)
1537 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1538 return 0;
1539 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1540 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541}
1542
1543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545symtable_visit_alias(struct symtable *st, alias_ty a)
1546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001548 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 dotted package name (e.g. spam.eggs)
1550 */
1551 PyObject *store_name;
1552 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1553 const Py_UNICODE *base = PyUnicode_AS_UNICODE(name);
1554 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
1555 if (dot) {
1556 store_name = PyUnicode_FromUnicode(base, dot - base);
1557 if (!store_name)
1558 return 0;
1559 }
1560 else {
1561 store_name = name;
1562 Py_INCREF(store_name);
1563 }
1564 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1565 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1566 Py_DECREF(store_name);
1567 return r;
1568 }
1569 else {
1570 if (st->st_cur->ste_type != ModuleBlock) {
1571 int lineno = st->st_cur->ste_lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001572 int col_offset = st->st_cur->ste_col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001574 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 Py_DECREF(store_name);
1576 return 0;
1577 }
1578 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1579 Py_DECREF(store_name);
1580 return 1;
1581 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582}
1583
1584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 VISIT(st, expr, lc->target);
1589 VISIT(st, expr, lc->iter);
1590 VISIT_SEQ(st, expr, lc->ifs);
1591 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592}
1593
1594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596symtable_visit_keyword(struct symtable *st, keyword_ty k)
1597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 VISIT(st, expr, k->value);
1599 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600}
1601
1602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604symtable_visit_slice(struct symtable *st, slice_ty s)
1605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 switch (s->kind) {
1607 case Slice_kind:
1608 if (s->v.Slice.lower)
1609 VISIT(st, expr, s->v.Slice.lower)
1610 if (s->v.Slice.upper)
1611 VISIT(st, expr, s->v.Slice.upper)
1612 if (s->v.Slice.step)
1613 VISIT(st, expr, s->v.Slice.step)
1614 break;
1615 case ExtSlice_kind:
1616 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1617 break;
1618 case Index_kind:
1619 VISIT(st, expr, s->v.Index.value)
1620 break;
1621 }
1622 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623}
1624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001626symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001627 identifier scope_name, asdl_seq *generators,
1628 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 int is_generator = (e->kind == GeneratorExp_kind);
1631 int needs_tmp = !is_generator;
1632 comprehension_ty outermost = ((comprehension_ty)
1633 asdl_seq_GET(generators, 0));
1634 /* Outermost iterator is evaluated in current scope */
1635 VISIT(st, expr, outermost->iter);
1636 /* Create comprehension scope for the rest */
1637 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001638 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1639 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 return 0;
1641 }
1642 st->st_cur->ste_generator = is_generator;
1643 /* Outermost iter is received as an argument */
1644 if (!symtable_implicit_arg(st, 0)) {
1645 symtable_exit_block(st, (void *)e);
1646 return 0;
1647 }
1648 /* Allocate temporary name if needed */
1649 if (needs_tmp && !symtable_new_tmpname(st)) {
1650 symtable_exit_block(st, (void *)e);
1651 return 0;
1652 }
1653 VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e);
1654 VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e);
1655 VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension,
1656 generators, 1, (void*)e);
1657 if (value)
1658 VISIT_IN_BLOCK(st, expr, value, (void*)e);
1659 VISIT_IN_BLOCK(st, expr, elt, (void*)e);
1660 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001664symtable_visit_genexp(struct symtable *st, expr_ty e)
1665{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1667 e->v.GeneratorExp.generators,
1668 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001669}
1670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001672symtable_visit_listcomp(struct symtable *st, expr_ty e)
1673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1675 e->v.ListComp.generators,
1676 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001677}
1678
1679static int
1680symtable_visit_setcomp(struct symtable *st, expr_ty e)
1681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1683 e->v.SetComp.generators,
1684 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001685}
1686
1687static int
1688symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1691 e->v.DictComp.generators,
1692 e->v.DictComp.key,
1693 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001694}