blob: 3578b0c692a448bf90b17406d4ee55c765f1691c [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
3#include "code.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00004#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005#include "structmember.h"
6
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008#define GLOBAL_AFTER_ASSIGN \
9"name '%.400s' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000010
Jeremy Hylton81e95022007-02-27 06:50:52 +000011#define NONLOCAL_AFTER_ASSIGN \
12"name '%.400s' is assigned to before nonlocal declaration"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_USE \
15"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_USE \
18"name '%.400s' is used prior to nonlocal declaration"
19
Neal Norwitz5d0ad502005-12-19 04:27:42 +000020#define IMPORT_STAR_WARNING "import * only allowed at module level"
21
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000022#define RETURN_VAL_IN_GENERATOR \
23 "'return' with argument inside generator"
Neal Norwitz5d0ad502005-12-19 04:27:42 +000024
Benjamin Peterson55e00f22008-08-17 18:02:44 +000025
Neal Norwitz090b3dd2006-02-28 22:36:46 +000026static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000027ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000028 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PySTEntryObject *ste = NULL;
31 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 k = PyLong_FromVoidPtr(key);
34 if (k == NULL)
35 goto fail;
36 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
37 if (ste == NULL)
38 goto fail;
39 ste->ste_table = st;
40 ste->ste_id = k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 ste->ste_name = name;
43 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 ste->ste_symbols = NULL;
46 ste->ste_varnames = NULL;
47 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 ste->ste_symbols = PyDict_New();
50 if (ste->ste_symbols == NULL)
51 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 ste->ste_varnames = PyList_New(0);
54 if (ste->ste_varnames == NULL)
55 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 ste->ste_children = PyList_New(0);
58 if (ste->ste_children == NULL)
59 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 ste->ste_type = block;
62 ste->ste_unoptimized = 0;
63 ste->ste_nested = 0;
64 ste->ste_free = 0;
65 ste->ste_varargs = 0;
66 ste->ste_varkeywords = 0;
67 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000068 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000069 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000071 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (st->st_cur != NULL &&
74 (st->st_cur->ste_nested ||
75 st->st_cur->ste_type == FunctionBlock))
76 ste->ste_nested = 1;
77 ste->ste_child_free = 0;
78 ste->ste_generator = 0;
79 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
82 goto fail;
83
84 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 Py_XDECREF(ste);
87 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088}
89
90static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
94 ste->ste_name,
95 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096}
97
98static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 ste->ste_table = NULL;
102 Py_XDECREF(ste->ste_id);
103 Py_XDECREF(ste->ste_name);
104 Py_XDECREF(ste->ste_symbols);
105 Py_XDECREF(ste->ste_varnames);
106 Py_XDECREF(ste->ste_children);
107 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108}
109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000111
Guido van Rossum6f799372001-09-20 20:46:19 +0000112static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 {"id", T_OBJECT, OFF(ste_id), READONLY},
114 {"name", T_OBJECT, OFF(ste_name), READONLY},
115 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
116 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
117 {"children", T_OBJECT, OFF(ste_children), READONLY},
118 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
119 {"nested", T_INT, OFF(ste_nested), READONLY},
120 {"type", T_INT, OFF(ste_type), READONLY},
121 {"lineno", T_INT, OFF(ste_lineno), READONLY},
122 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000123};
124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000125PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 PyVarObject_HEAD_INIT(&PyType_Type, 0)
127 "symtable entry",
128 sizeof(PySTEntryObject),
129 0,
130 (destructor)ste_dealloc, /* tp_dealloc */
131 0, /* tp_print */
132 0, /* tp_getattr */
133 0, /* tp_setattr */
134 0, /* tp_reserved */
135 (reprfunc)ste_repr, /* tp_repr */
136 0, /* tp_as_number */
137 0, /* tp_as_sequence */
138 0, /* tp_as_mapping */
139 0, /* tp_hash */
140 0, /* tp_call */
141 0, /* tp_str */
142 PyObject_GenericGetAttr, /* tp_getattro */
143 0, /* tp_setattro */
144 0, /* tp_as_buffer */
145 Py_TPFLAGS_DEFAULT, /* tp_flags */
146 0, /* tp_doc */
147 0, /* tp_traverse */
148 0, /* tp_clear */
149 0, /* tp_richcompare */
150 0, /* tp_weaklistoffset */
151 0, /* tp_iter */
152 0, /* tp_iternext */
153 0, /* tp_methods */
154 ste_memberlist, /* tp_members */
155 0, /* tp_getset */
156 0, /* tp_base */
157 0, /* tp_dict */
158 0, /* tp_descr_get */
159 0, /* tp_descr_set */
160 0, /* tp_dictoffset */
161 0, /* tp_init */
162 0, /* tp_alloc */
163 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000164};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165
166static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000167static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000169 _Py_block_ty block, void *ast, int lineno,
170 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171static int symtable_exit_block(struct symtable *st, void *ast);
172static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
173static int symtable_visit_expr(struct symtable *st, expr_ty s);
174static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000175static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
176static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000177static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178static int symtable_visit_arguments(struct symtable *st, arguments_ty);
179static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
180static int symtable_visit_alias(struct symtable *st, alias_ty);
181static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
182static int symtable_visit_keyword(struct symtable *st, keyword_ty);
183static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000184static int symtable_visit_params(struct symtable *st, asdl_seq *args);
185static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000187static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500188static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189
190
Nick Coghlan650f0d02007-04-15 12:05:43 +0000191static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
193 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194
195#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
198#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000199"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
201static struct symtable *
202symtable_new(void)
203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
207 if (st == NULL)
208 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 st->st_filename = NULL;
211 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if ((st->st_stack = PyList_New(0)) == NULL)
214 goto fail;
215 if ((st->st_blocks = PyDict_New()) == NULL)
216 goto fail;
217 st->st_cur = NULL;
218 st->st_private = NULL;
219 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 PySymtable_Free(st);
222 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223}
224
225struct symtable *
226PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
227{
Benjamin Petersonf5ff2232011-06-19 19:42:22 -0500228 struct symtable *st;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 asdl_seq *seq;
230 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231
Benjamin Petersonf5ff2232011-06-19 19:42:22 -0500232 if (__class__ == NULL) {
233 __class__ = PyUnicode_InternFromString("@__class__");
234 if (__class__ == NULL)
235 return NULL;
236 }
237
238 st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 if (st == NULL)
240 return st;
241 st->st_filename = filename;
242 st->st_future = future;
243 /* Make the initial symbol information gathering pass */
244 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000245 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 PySymtable_Free(st);
247 return NULL;
248 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 st->st_top = st->st_cur;
251 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
252 switch (mod->kind) {
253 case Module_kind:
254 seq = mod->v.Module.body;
255 for (i = 0; i < asdl_seq_LEN(seq); i++)
256 if (!symtable_visit_stmt(st,
257 (stmt_ty)asdl_seq_GET(seq, i)))
258 goto error;
259 break;
260 case Expression_kind:
261 if (!symtable_visit_expr(st, mod->v.Expression.body))
262 goto error;
263 break;
264 case Interactive_kind:
265 seq = mod->v.Interactive.body;
266 for (i = 0; i < asdl_seq_LEN(seq); i++)
267 if (!symtable_visit_stmt(st,
268 (stmt_ty)asdl_seq_GET(seq, i)))
269 goto error;
270 break;
271 case Suite_kind:
272 PyErr_SetString(PyExc_RuntimeError,
273 "this compiler does not handle Suites");
274 goto error;
275 }
276 if (!symtable_exit_block(st, (void *)mod)) {
277 PySymtable_Free(st);
278 return NULL;
279 }
280 /* Make the second symbol analysis pass */
281 if (symtable_analyze(st))
282 return st;
283 PySymtable_Free(st);
284 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 (void) symtable_exit_block(st, (void *)mod);
287 PySymtable_Free(st);
288 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289}
290
291void
292PySymtable_Free(struct symtable *st)
293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 Py_XDECREF(st->st_blocks);
295 Py_XDECREF(st->st_stack);
296 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297}
298
299PySTEntryObject *
300PySymtable_Lookup(struct symtable *st, void *key)
301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 k = PyLong_FromVoidPtr(key);
305 if (k == NULL)
306 return NULL;
307 v = PyDict_GetItem(st->st_blocks, k);
308 if (v) {
309 assert(PySTEntry_Check(v));
310 Py_INCREF(v);
311 }
312 else {
313 PyErr_SetString(PyExc_KeyError,
314 "unknown symbol table entry");
315 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 Py_DECREF(k);
318 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319}
320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322PyST_GetScope(PySTEntryObject *ste, PyObject *name)
323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
325 if (!v)
326 return 0;
327 assert(PyLong_Check(v));
328 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329}
330
331
332/* Analyze raw symbol information to determine scope of each name.
333
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000334 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340 explicit global is declared with the global statement. An implicit
341 global is a free variable for which the compiler has found no binding
342 in an enclosing function scope. The implicit global is either a global
343 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
344 to handle these names to implement slightly odd semantics. In such a
345 block, the name is treated as global until it is assigned to; then it
346 is treated as a local.
347
348 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000349 The first pass collects raw facts from the AST via the symtable_visit_*
350 functions: the name is a parameter here, the name is used but not defined
351 here, etc. The second pass analyzes these facts during a pass over the
352 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353
354 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000356 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000357 Names which are explicitly declared nonlocal must exist in this set of
358 visible names - if they do not, a syntax error is raised. After doing
359 the local analysis, it analyzes each of its child blocks using an
360 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361
Nick Coghlan650f0d02007-04-15 12:05:43 +0000362 The children update the free variable set. If a local variable is added to
363 the free variable set by the child, the variable is marked as a cell. The
364 function object being defined must provide runtime storage for the variable
365 that may outlive the function's frame. Cell variables are removed from the
366 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000367
Nick Coghlan650f0d02007-04-15 12:05:43 +0000368 During analysis, the names are:
369 symbols: dict mapping from symbol names to flag values (including offset scope values)
370 scopes: dict mapping from symbol names to scope values (no offset)
371 local: set of all symbol names local to the current scope
372 bound: set of all symbol names local to a containing function scope
373 free: set of all symbol names referenced but not bound in child scopes
374 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375*/
376
377#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 PyObject *o = PyLong_FromLong(I); \
379 if (!o) \
380 return 0; \
381 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
382 Py_DECREF(o); \
383 return 0; \
384 } \
385 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386}
387
388/* Decide on scope of name, given flags.
389
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000390 The namespace dictionaries may be modified to record information
391 about the new name. For example, a new global will add an entry to
392 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393*/
394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000396analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 PyObject *bound, PyObject *local, PyObject *free,
398 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (flags & DEF_GLOBAL) {
401 if (flags & DEF_PARAM) {
402 PyErr_Format(PyExc_SyntaxError,
403 "name '%U' is parameter and global",
404 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000405 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
406 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407
408 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000409 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 if (flags & DEF_NONLOCAL) {
411 PyErr_Format(PyExc_SyntaxError,
412 "name '%U' is nonlocal and global",
413 name);
414 return 0;
415 }
416 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
417 if (PySet_Add(global, name) < 0)
418 return 0;
419 if (bound && (PySet_Discard(bound, name) < 0))
420 return 0;
421 return 1;
422 }
423 if (flags & DEF_NONLOCAL) {
424 if (flags & DEF_PARAM) {
425 PyErr_Format(PyExc_SyntaxError,
426 "name '%U' is parameter and nonlocal",
427 name);
428 return 0;
429 }
430 if (!bound) {
431 PyErr_Format(PyExc_SyntaxError,
432 "nonlocal declaration not allowed at module level");
433 return 0;
434 }
435 if (!PySet_Contains(bound, name)) {
436 PyErr_Format(PyExc_SyntaxError,
437 "no binding for nonlocal '%U' found",
438 name);
439
440 return 0;
441 }
442 SET_SCOPE(scopes, name, FREE);
443 ste->ste_free = 1;
444 return PySet_Add(free, name) >= 0;
445 }
446 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000447 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (PySet_Add(local, name) < 0)
449 return 0;
450 if (PySet_Discard(global, name) < 0)
451 return 0;
452 return 1;
453 }
454 /* If an enclosing block has a binding for this name, it
455 is a free variable rather than a global variable.
456 Note that having a non-NULL bound implies that the block
457 is nested.
458 */
459 if (bound && PySet_Contains(bound, name)) {
460 SET_SCOPE(scopes, name, FREE);
461 ste->ste_free = 1;
462 return PySet_Add(free, name) >= 0;
463 }
464 /* If a parent has a global statement, then call it global
465 explicit? It could also be global implicit.
466 */
467 if (global && PySet_Contains(global, name)) {
468 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
469 return 1;
470 }
471 if (ste->ste_nested)
472 ste->ste_free = 1;
473 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
474 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475}
476
477#undef SET_SCOPE
478
479/* If a name is defined in free and also in locals, then this block
480 provides the binding for the free variable. The name should be
481 marked CELL in this block and removed from the free list.
482
483 Note that the current block's free variables are included in free.
484 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000485
Martin v. Löwis2673a572007-10-29 19:54:24 +0000486 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000487 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488*/
489
490static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000491analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PyObject *name, *v, *v_cell;
494 int success = 0;
495 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 v_cell = PyLong_FromLong(CELL);
498 if (!v_cell)
499 return 0;
500 while (PyDict_Next(scopes, &pos, &name, &v)) {
501 long scope;
502 assert(PyLong_Check(v));
503 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000504 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 continue;
506 if (!PySet_Contains(free, name))
507 continue;
508 if (restricted != NULL &&
509 PyUnicode_CompareWithASCIIString(name, restricted))
510 continue;
511 /* Replace LOCAL with CELL for this name, and remove
512 from free. It is safe to replace the value of name
513 in the dict, because it will not cause a resize.
514 */
515 if (PyDict_SetItem(scopes, name, v_cell) < 0)
516 goto error;
517 if (PySet_Discard(free, name) < 0)
518 goto error;
519 }
520 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 Py_DECREF(v_cell);
523 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524}
525
526/* Check for illegal statements in unoptimized namespaces */
527static int
528check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
532 || !(ste->ste_free || ste->ste_child_free))
533 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 trailer = (ste->ste_child_free ?
536 "contains a nested function with free variables" :
537 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 switch (ste->ste_unoptimized) {
540 case OPT_TOPLEVEL: /* import * at top-level is fine */
541 return 1;
542 case OPT_IMPORT_STAR:
543 PyErr_Format(PyExc_SyntaxError,
544 "import * is not allowed in function '%U' because it %s",
545 ste->ste_name, trailer);
546 break;
547 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000549 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
550 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552}
553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554/* Enter the final scope information into the ste_symbols dict.
555 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 * All arguments are dicts. Modifies symbols, others are read-only.
557*/
558static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000560 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 PyObject *name = NULL, *itr = NULL;
563 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
564 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* Update scope information for all symbols in this scope */
567 while (PyDict_Next(symbols, &pos, &name, &v)) {
568 long scope, flags;
569 assert(PyLong_Check(v));
570 flags = PyLong_AS_LONG(v);
571 v_scope = PyDict_GetItem(scopes, name);
572 assert(v_scope && PyLong_Check(v_scope));
573 scope = PyLong_AS_LONG(v_scope);
574 flags |= (scope << SCOPE_OFFSET);
575 v_new = PyLong_FromLong(flags);
576 if (!v_new)
577 return 0;
578 if (PyDict_SetItem(symbols, name, v_new) < 0) {
579 Py_DECREF(v_new);
580 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 Py_DECREF(v_new);
583 }
584
585 /* Record not yet resolved free variables from children (if any) */
586 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
587 if (!v_free)
588 return 0;
589
590 itr = PyObject_GetIter(free);
591 if (!itr)
592 goto error;
593
594 while ((name = PyIter_Next(itr))) {
595 v = PyDict_GetItem(symbols, name);
596
597 /* Handle symbol that already exists in this scope */
598 if (v) {
599 /* Handle a free variable in a method of
600 the class that has the same name as a local
601 or global in the class scope.
602 */
603 if (classflag &&
604 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
605 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
606 v_new = PyLong_FromLong(flags);
607 if (!v_new) {
608 goto error;
609 }
610 if (PyDict_SetItem(symbols, name, v_new) < 0) {
611 Py_DECREF(v_new);
612 goto error;
613 }
614 Py_DECREF(v_new);
615 }
616 /* It's a cell, or already free in this scope */
617 Py_DECREF(name);
618 continue;
619 }
620 /* Handle global symbol */
621 if (!PySet_Contains(bound, name)) {
622 Py_DECREF(name);
623 continue; /* it's a global */
624 }
625 /* Propagate new free symbol up the lexical stack */
626 if (PyDict_SetItem(symbols, name, v_free) < 0) {
627 goto error;
628 }
629 Py_DECREF(name);
630 }
631 Py_DECREF(itr);
632 Py_DECREF(v_free);
633 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000634error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 Py_XDECREF(v_free);
636 Py_XDECREF(itr);
637 Py_XDECREF(name);
638 return 0;
639}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640
641/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000642
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 Arguments:
644 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000645 bound -- set of variables bound in enclosing scopes (input). bound
646 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 free -- set of free variables in enclosed scopes (output)
648 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000649
650 The implementation uses two mutually recursive functions,
651 analyze_block() and analyze_child_block(). analyze_block() is
652 responsible for analyzing the individual names defined in a block.
653 analyze_child_block() prepares temporary namespace dictionaries
654 used to evaluated nested blocks.
655
656 The two functions exist because a child block should see the name
657 bindings of its enclosing blocks, but those bindings should not
658 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659*/
660
661static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
663 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000664
665static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
667 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
670 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
671 PyObject *temp;
672 int i, success = 0;
673 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 local = PySet_New(NULL); /* collect new names bound in block */
676 if (!local)
677 goto error;
678 scopes = PyDict_New(); /* collect scopes defined for each name */
679 if (!scopes)
680 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 /* Allocate new global and bound variable dictionaries. These
683 dictionaries hold the names visible in nested blocks. For
684 ClassBlocks, the bound and global names are initialized
685 before analyzing names, because class bindings aren't
686 visible in methods. For other blocks, they are initialized
687 after names are analyzed.
688 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 /* TODO(jhylton): Package these dicts in a struct so that we
691 can write reasonable helper functions?
692 */
693 newglobal = PySet_New(NULL);
694 if (!newglobal)
695 goto error;
696 newfree = PySet_New(NULL);
697 if (!newfree)
698 goto error;
699 newbound = PySet_New(NULL);
700 if (!newbound)
701 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 /* Class namespace has no effect on names visible in
704 nested functions, so populate the global and bound
705 sets to be passed to child blocks before analyzing
706 this one.
707 */
708 if (ste->ste_type == ClassBlock) {
709 /* Pass down known globals */
710 temp = PyNumber_InPlaceOr(newglobal, global);
711 if (!temp)
712 goto error;
713 Py_DECREF(temp);
714 /* Pass down previously bound symbols */
715 if (bound) {
716 temp = PyNumber_InPlaceOr(newbound, bound);
717 if (!temp)
718 goto error;
719 Py_DECREF(temp);
720 }
721 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
724 long flags = PyLong_AS_LONG(v);
725 if (!analyze_name(ste, scopes, name, flags,
726 bound, local, free, global))
727 goto error;
728 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 /* Populate global and bound sets to be passed to children. */
731 if (ste->ste_type != ClassBlock) {
732 /* Add function locals to bound set */
733 if (ste->ste_type == FunctionBlock) {
734 temp = PyNumber_InPlaceOr(newbound, local);
735 if (!temp)
736 goto error;
737 Py_DECREF(temp);
738 }
739 /* Pass down previously bound symbols */
740 if (bound) {
741 temp = PyNumber_InPlaceOr(newbound, bound);
742 if (!temp)
743 goto error;
744 Py_DECREF(temp);
745 }
746 /* Pass down known globals */
747 temp = PyNumber_InPlaceOr(newglobal, global);
748 if (!temp)
749 goto error;
750 Py_DECREF(temp);
751 }
752 else {
753 /* Special-case __class__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 assert(PySet_Contains(local, __class__) == 1);
755 if (PySet_Add(newbound, __class__) < 0)
756 goto error;
757 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300759 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 newbound, newglobal now contain the names visible in
762 nested blocks. The free variables in the children will
763 be collected in allfree.
764 */
765 allfree = PySet_New(NULL);
766 if (!allfree)
767 goto error;
768 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
769 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
770 PySTEntryObject* entry;
771 assert(c && PySTEntry_Check(c));
772 entry = (PySTEntryObject*)c;
773 if (!analyze_child_block(entry, newbound, newfree, newglobal,
774 allfree))
775 goto error;
776 /* Check if any children have free variables */
777 if (entry->ste_free || entry->ste_child_free)
778 ste->ste_child_free = 1;
779 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 temp = PyNumber_InPlaceOr(newfree, allfree);
782 if (!temp)
783 goto error;
784 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 /* Check if any local variables must be converted to cell variables */
787 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
788 NULL))
789 goto error;
790 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
Benjamin Petersonf5ff2232011-06-19 19:42:22 -0500791 "@__class__"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 goto error;
793 /* Records the results of the analysis in the symbol table entry */
794 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
795 ste->ste_type == ClassBlock))
796 goto error;
797 if (!check_unoptimized(ste))
798 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 temp = PyNumber_InPlaceOr(free, newfree);
801 if (!temp)
802 goto error;
803 Py_DECREF(temp);
804 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 Py_XDECREF(scopes);
807 Py_XDECREF(local);
808 Py_XDECREF(newbound);
809 Py_XDECREF(newglobal);
810 Py_XDECREF(newfree);
811 Py_XDECREF(allfree);
812 if (!success)
813 assert(PyErr_Occurred());
814 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000815}
816
817static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
819 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
822 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 These dictionary are used by all blocks enclosed by the
827 current block. The analyze_block() call modifies these
828 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 */
831 temp_bound = PySet_New(bound);
832 if (!temp_bound)
833 goto error;
834 temp_free = PySet_New(free);
835 if (!temp_free)
836 goto error;
837 temp_global = PySet_New(global);
838 if (!temp_global)
839 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
842 goto error;
843 temp = PyNumber_InPlaceOr(child_free, temp_free);
844 if (!temp)
845 goto error;
846 Py_DECREF(temp);
847 Py_DECREF(temp_bound);
848 Py_DECREF(temp_free);
849 Py_DECREF(temp_global);
850 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000851 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 Py_XDECREF(temp_bound);
853 Py_XDECREF(temp_free);
854 Py_XDECREF(temp_global);
855 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000856}
857
858static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859symtable_analyze(struct symtable *st)
860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 PyObject *free, *global;
862 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 free = PySet_New(NULL);
865 if (!free)
866 return 0;
867 global = PySet_New(NULL);
868 if (!global) {
869 Py_DECREF(free);
870 return 0;
871 }
872 r = analyze_block(st->st_top, NULL, free, global);
873 Py_DECREF(free);
874 Py_DECREF(global);
875 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876}
877
878
879static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000880symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
883 lineno, NULL, NULL) < 0) {
884 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
885 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000886 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
887 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 }
889 return 0;
890 }
891 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892}
893
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000894/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895 This reference is released when the block is exited, via the DECREF
896 in symtable_exit_block().
897*/
898
899static int
900symtable_exit_block(struct symtable *st, void *ast)
901{
Benjamin Peterson609da582011-06-29 22:52:39 -0500902 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903
Benjamin Peterson609da582011-06-29 22:52:39 -0500904 st->st_cur = NULL;
905 size = PyList_GET_SIZE(st->st_stack);
906 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500907 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500909 if (--size)
910 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 }
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,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000917 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918{
Benjamin Peterson609da582011-06-29 22:52:39 -0500919 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920
Benjamin Peterson609da582011-06-29 22:52:39 -0500921 ste = ste_new(st, name, block, ast, lineno, col_offset);
922 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500924 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
925 Py_DECREF(ste);
926 return 0;
927 }
928 prev = st->st_cur;
929 /* The entry is owned by the stack. Borrow it for st_cur. */
930 Py_DECREF(ste);
931 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000932 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 st->st_global = st->st_cur->ste_symbols;
934 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500935 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 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);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000973 PyErr_SyntaxLocationEx(st->st_filename,
974 st->st_cur->ste_lineno,
975 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 goto error;
977 }
978 val |= flag;
979 } else
980 val = flag;
981 o = PyLong_FromLong(val);
982 if (o == NULL)
983 goto error;
984 if (PyDict_SetItem(dict, mangled, o) < 0) {
985 Py_DECREF(o);
986 goto error;
987 }
988 Py_DECREF(o);
989
990 if (flag & DEF_PARAM) {
991 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
992 goto error;
993 } else if (flag & DEF_GLOBAL) {
994 /* XXX need to update DEF_GLOBAL for other flags too;
995 perhaps only DEF_FREE_GLOBAL */
996 val = flag;
997 if ((o = PyDict_GetItem(st->st_global, mangled))) {
998 val |= PyLong_AS_LONG(o);
999 }
1000 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 goto error;
1003 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1004 Py_DECREF(o);
1005 goto error;
1006 }
1007 Py_DECREF(o);
1008 }
1009 Py_DECREF(mangled);
1010 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001011
1012error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 Py_DECREF(mangled);
1014 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015}
1016
1017/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1018 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 function.
1020
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1022 useful if the first node in the sequence requires special treatment.
1023*/
1024
1025#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (!symtable_visit_ ## TYPE((ST), (V))) \
1027 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001028
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 int i; \
1031 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1032 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1033 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1034 if (!symtable_visit_ ## TYPE((ST), elt)) \
1035 return 0; \
1036 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001038
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 int i; \
1041 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1042 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1043 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1044 if (!symtable_visit_ ## TYPE((ST), elt)) \
1045 return 0; \
1046 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001048
Guido van Rossum4f72a782006-10-27 23:31:49 +00001049#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 int i = 0; \
1051 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1052 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1053 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1054 if (!elt) continue; /* can be NULL */ \
1055 if (!symtable_visit_expr((ST), elt)) \
1056 return 0; \
1057 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001058}
1059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001061symtable_new_tmpname(struct symtable *st)
1062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 char tmpname[256];
1064 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1067 ++st->st_cur->ste_tmpname);
1068 tmp = PyUnicode_InternFromString(tmpname);
1069 if (!tmp)
1070 return 0;
1071 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1072 return 0;
1073 Py_DECREF(tmp);
1074 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001075}
1076
Guido van Rossum4f72a782006-10-27 23:31:49 +00001077
Guido van Rossumc2e20742006-02-27 22:32:47 +00001078static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079symtable_visit_stmt(struct symtable *st, stmt_ty s)
1080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 switch (s->kind) {
1082 case FunctionDef_kind:
1083 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1084 return 0;
1085 if (s->v.FunctionDef.args->defaults)
1086 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1087 if (s->v.FunctionDef.args->kw_defaults)
1088 VISIT_KWONLYDEFAULTS(st,
1089 s->v.FunctionDef.args->kw_defaults);
1090 if (!symtable_visit_annotations(st, s))
1091 return 0;
1092 if (s->v.FunctionDef.decorator_list)
1093 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1094 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001095 FunctionBlock, (void *)s, s->lineno,
1096 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001098 VISIT(st, arguments, s->v.FunctionDef.args);
1099 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 if (!symtable_exit_block(st, s))
1101 return 0;
1102 break;
1103 case ClassDef_kind: {
1104 PyObject *tmp;
1105 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1106 return 0;
1107 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1108 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1109 if (s->v.ClassDef.starargs)
1110 VISIT(st, expr, s->v.ClassDef.starargs);
1111 if (s->v.ClassDef.kwargs)
1112 VISIT(st, expr, s->v.ClassDef.kwargs);
1113 if (s->v.ClassDef.decorator_list)
1114 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1115 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001116 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 return 0;
Benjamin Petersonf5ff2232011-06-19 19:42:22 -05001118 if (!symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 !GET_IDENTIFIER(__locals__) ||
1120 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1121 symtable_exit_block(st, s);
1122 return 0;
1123 }
1124 tmp = st->st_private;
1125 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001126 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 st->st_private = tmp;
1128 if (!symtable_exit_block(st, s))
1129 return 0;
1130 break;
1131 }
1132 case Return_kind:
1133 if (s->v.Return.value) {
1134 VISIT(st, expr, s->v.Return.value);
1135 st->st_cur->ste_returns_value = 1;
1136 if (st->st_cur->ste_generator) {
1137 PyErr_SetString(PyExc_SyntaxError,
1138 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001139 PyErr_SyntaxLocationEx(st->st_filename,
1140 s->lineno,
1141 s->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00001143 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 }
1145 break;
1146 case Delete_kind:
1147 VISIT_SEQ(st, expr, s->v.Delete.targets);
1148 break;
1149 case Assign_kind:
1150 VISIT_SEQ(st, expr, s->v.Assign.targets);
1151 VISIT(st, expr, s->v.Assign.value);
1152 break;
1153 case AugAssign_kind:
1154 VISIT(st, expr, s->v.AugAssign.target);
1155 VISIT(st, expr, s->v.AugAssign.value);
1156 break;
1157 case For_kind:
1158 VISIT(st, expr, s->v.For.target);
1159 VISIT(st, expr, s->v.For.iter);
1160 VISIT_SEQ(st, stmt, s->v.For.body);
1161 if (s->v.For.orelse)
1162 VISIT_SEQ(st, stmt, s->v.For.orelse);
1163 break;
1164 case While_kind:
1165 VISIT(st, expr, s->v.While.test);
1166 VISIT_SEQ(st, stmt, s->v.While.body);
1167 if (s->v.While.orelse)
1168 VISIT_SEQ(st, stmt, s->v.While.orelse);
1169 break;
1170 case If_kind:
1171 /* XXX if 0: and lookup_yield() hacks */
1172 VISIT(st, expr, s->v.If.test);
1173 VISIT_SEQ(st, stmt, s->v.If.body);
1174 if (s->v.If.orelse)
1175 VISIT_SEQ(st, stmt, s->v.If.orelse);
1176 break;
1177 case Raise_kind:
1178 if (s->v.Raise.exc) {
1179 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001180 if (s->v.Raise.cause) {
1181 VISIT(st, expr, s->v.Raise.cause);
1182 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 }
1184 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001185 case Try_kind:
1186 VISIT_SEQ(st, stmt, s->v.Try.body);
1187 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1188 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1189 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 break;
1191 case Assert_kind:
1192 VISIT(st, expr, s->v.Assert.test);
1193 if (s->v.Assert.msg)
1194 VISIT(st, expr, s->v.Assert.msg);
1195 break;
1196 case Import_kind:
1197 VISIT_SEQ(st, alias, s->v.Import.names);
1198 /* XXX Don't have the lineno available inside
1199 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001200 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001202 st->st_cur->ste_opt_col_offset = s->col_offset;
1203 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 break;
1205 case ImportFrom_kind:
1206 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1207 /* XXX Don't have the lineno available inside
1208 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001209 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001211 st->st_cur->ste_opt_col_offset = s->col_offset;
1212 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 break;
1214 case Global_kind: {
1215 int i;
1216 asdl_seq *seq = s->v.Global.names;
1217 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1218 identifier name = (identifier)asdl_seq_GET(seq, i);
1219 char *c_name = _PyUnicode_AsString(name);
1220 long cur = symtable_lookup(st, name);
1221 if (cur < 0)
1222 return 0;
1223 if (cur & (DEF_LOCAL | USE)) {
1224 char buf[256];
1225 if (cur & DEF_LOCAL)
1226 PyOS_snprintf(buf, sizeof(buf),
1227 GLOBAL_AFTER_ASSIGN,
1228 c_name);
1229 else
1230 PyOS_snprintf(buf, sizeof(buf),
1231 GLOBAL_AFTER_USE,
1232 c_name);
1233 if (!symtable_warn(st, buf, s->lineno))
1234 return 0;
1235 }
1236 if (!symtable_add_def(st, name, DEF_GLOBAL))
1237 return 0;
1238 }
1239 break;
1240 }
1241 case Nonlocal_kind: {
1242 int i;
1243 asdl_seq *seq = s->v.Nonlocal.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 NONLOCAL_AFTER_ASSIGN,
1255 c_name);
1256 else
1257 PyOS_snprintf(buf, sizeof(buf),
1258 NONLOCAL_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_NONLOCAL))
1264 return 0;
1265 }
1266 break;
1267 }
1268 case Expr_kind:
1269 VISIT(st, expr, s->v.Expr.value);
1270 break;
1271 case Pass_kind:
1272 case Break_kind:
1273 case Continue_kind:
1274 /* nothing to do here */
1275 break;
1276 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001277 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 VISIT_SEQ(st, stmt, s->v.With.body);
1279 break;
1280 }
1281 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282}
1283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285symtable_visit_expr(struct symtable *st, expr_ty e)
1286{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 switch (e->kind) {
1288 case BoolOp_kind:
1289 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1290 break;
1291 case BinOp_kind:
1292 VISIT(st, expr, e->v.BinOp.left);
1293 VISIT(st, expr, e->v.BinOp.right);
1294 break;
1295 case UnaryOp_kind:
1296 VISIT(st, expr, e->v.UnaryOp.operand);
1297 break;
1298 case Lambda_kind: {
1299 if (!GET_IDENTIFIER(lambda))
1300 return 0;
1301 if (e->v.Lambda.args->defaults)
1302 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1303 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001304 FunctionBlock, (void *)e, e->lineno,
1305 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001307 VISIT(st, arguments, e->v.Lambda.args);
1308 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 if (!symtable_exit_block(st, (void *)e))
1310 return 0;
1311 break;
1312 }
1313 case IfExp_kind:
1314 VISIT(st, expr, e->v.IfExp.test);
1315 VISIT(st, expr, e->v.IfExp.body);
1316 VISIT(st, expr, e->v.IfExp.orelse);
1317 break;
1318 case Dict_kind:
1319 VISIT_SEQ(st, expr, e->v.Dict.keys);
1320 VISIT_SEQ(st, expr, e->v.Dict.values);
1321 break;
1322 case Set_kind:
1323 VISIT_SEQ(st, expr, e->v.Set.elts);
1324 break;
1325 case GeneratorExp_kind:
1326 if (!symtable_visit_genexp(st, e))
1327 return 0;
1328 break;
1329 case ListComp_kind:
1330 if (!symtable_visit_listcomp(st, e))
1331 return 0;
1332 break;
1333 case SetComp_kind:
1334 if (!symtable_visit_setcomp(st, e))
1335 return 0;
1336 break;
1337 case DictComp_kind:
1338 if (!symtable_visit_dictcomp(st, e))
1339 return 0;
1340 break;
1341 case Yield_kind:
1342 if (e->v.Yield.value)
1343 VISIT(st, expr, e->v.Yield.value);
1344 st->st_cur->ste_generator = 1;
1345 if (st->st_cur->ste_returns_value) {
1346 PyErr_SetString(PyExc_SyntaxError,
1347 RETURN_VAL_IN_GENERATOR);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001348 PyErr_SyntaxLocationEx(st->st_filename,
1349 e->lineno, e->col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 return 0;
1351 }
1352 break;
1353 case Compare_kind:
1354 VISIT(st, expr, e->v.Compare.left);
1355 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1356 break;
1357 case Call_kind:
1358 VISIT(st, expr, e->v.Call.func);
1359 VISIT_SEQ(st, expr, e->v.Call.args);
1360 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1361 if (e->v.Call.starargs)
1362 VISIT(st, expr, e->v.Call.starargs);
1363 if (e->v.Call.kwargs)
1364 VISIT(st, expr, e->v.Call.kwargs);
1365 break;
1366 case Num_kind:
1367 case Str_kind:
1368 case Bytes_kind:
1369 case Ellipsis_kind:
1370 /* Nothing to do here. */
1371 break;
1372 /* The following exprs can be assignment targets. */
1373 case Attribute_kind:
1374 VISIT(st, expr, e->v.Attribute.value);
1375 break;
1376 case Subscript_kind:
1377 VISIT(st, expr, e->v.Subscript.value);
1378 VISIT(st, slice, e->v.Subscript.slice);
1379 break;
1380 case Starred_kind:
1381 VISIT(st, expr, e->v.Starred.value);
1382 break;
1383 case Name_kind:
1384 if (!symtable_add_def(st, e->v.Name.id,
1385 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1386 return 0;
1387 /* Special-case super: it counts as a use of __class__ */
1388 if (e->v.Name.ctx == Load &&
1389 st->st_cur->ste_type == FunctionBlock &&
1390 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Benjamin Petersonf5ff2232011-06-19 19:42:22 -05001391 if (!symtable_add_def(st, __class__, USE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 return 0;
1393 }
1394 break;
1395 /* child nodes of List and Tuple will have expr_context set */
1396 case List_kind:
1397 VISIT_SEQ(st, expr, e->v.List.elts);
1398 break;
1399 case Tuple_kind:
1400 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1401 break;
1402 }
1403 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404}
1405
1406static int
1407symtable_implicit_arg(struct symtable *st, int pos)
1408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1410 if (id == NULL)
1411 return 0;
1412 if (!symtable_add_def(st, id, DEF_PARAM)) {
1413 Py_DECREF(id);
1414 return 0;
1415 }
1416 Py_DECREF(id);
1417 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418}
1419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001421symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 if (!args)
1426 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 for (i = 0; i < asdl_seq_LEN(args); i++) {
1429 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1430 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1431 return 0;
1432 }
1433
1434 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435}
1436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001438symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if (!args)
1443 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 for (i = 0; i < asdl_seq_LEN(args); i++) {
1446 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1447 if (arg->annotation)
1448 VISIT(st, expr, arg->annotation);
1449 }
1450
1451 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001452}
1453
Neal Norwitzc1505362006-12-28 06:47:50 +00001454static int
1455symtable_visit_annotations(struct symtable *st, stmt_ty s)
1456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 arguments_ty a = s->v.FunctionDef.args;
1458
1459 if (a->args && !symtable_visit_argannotations(st, a->args))
1460 return 0;
1461 if (a->varargannotation)
1462 VISIT(st, expr, a->varargannotation);
1463 if (a->kwargannotation)
1464 VISIT(st, expr, a->kwargannotation);
1465 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1466 return 0;
1467 if (s->v.FunctionDef.returns)
1468 VISIT(st, expr, s->v.FunctionDef.returns);
1469 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470}
1471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473symtable_visit_arguments(struct symtable *st, arguments_ty a)
1474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 /* skip default arguments inside function block
1476 XXX should ast be different?
1477 */
1478 if (a->args && !symtable_visit_params(st, a->args))
1479 return 0;
1480 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1481 return 0;
1482 if (a->vararg) {
1483 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1484 return 0;
1485 st->st_cur->ste_varargs = 1;
1486 }
1487 if (a->kwarg) {
1488 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1489 return 0;
1490 st->st_cur->ste_varkeywords = 1;
1491 }
1492 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493}
1494
1495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 if (eh->v.ExceptHandler.type)
1500 VISIT(st, expr, eh->v.ExceptHandler.type);
1501 if (eh->v.ExceptHandler.name)
1502 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1503 return 0;
1504 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1505 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506}
1507
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001508static int
1509symtable_visit_withitem(struct symtable *st, withitem_ty item)
1510{
1511 VISIT(st, expr, item->context_expr);
1512 if (item->optional_vars) {
1513 VISIT(st, expr, item->optional_vars);
1514 }
1515 return 1;
1516}
1517
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520symtable_visit_alias(struct symtable *st, alias_ty a)
1521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001523 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 dotted package name (e.g. spam.eggs)
1525 */
1526 PyObject *store_name;
1527 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001528 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1529 PyUnicode_GET_LENGTH(name), 1);
1530 if (dot != -1) {
1531 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 if (!store_name)
1533 return 0;
1534 }
1535 else {
1536 store_name = name;
1537 Py_INCREF(store_name);
1538 }
1539 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1540 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1541 Py_DECREF(store_name);
1542 return r;
1543 }
1544 else {
1545 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001546 int lineno = st->st_cur->ste_lineno;
1547 int col_offset = st->st_cur->ste_col_offset;
1548 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1549 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1550 Py_DECREF(store_name);
1551 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 }
1553 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1554 Py_DECREF(store_name);
1555 return 1;
1556 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557}
1558
1559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 VISIT(st, expr, lc->target);
1564 VISIT(st, expr, lc->iter);
1565 VISIT_SEQ(st, expr, lc->ifs);
1566 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567}
1568
1569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571symtable_visit_keyword(struct symtable *st, keyword_ty k)
1572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 VISIT(st, expr, k->value);
1574 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575}
1576
1577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579symtable_visit_slice(struct symtable *st, slice_ty s)
1580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 switch (s->kind) {
1582 case Slice_kind:
1583 if (s->v.Slice.lower)
1584 VISIT(st, expr, s->v.Slice.lower)
1585 if (s->v.Slice.upper)
1586 VISIT(st, expr, s->v.Slice.upper)
1587 if (s->v.Slice.step)
1588 VISIT(st, expr, s->v.Slice.step)
1589 break;
1590 case ExtSlice_kind:
1591 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1592 break;
1593 case Index_kind:
1594 VISIT(st, expr, s->v.Index.value)
1595 break;
1596 }
1597 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598}
1599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001601symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001602 identifier scope_name, asdl_seq *generators,
1603 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 int is_generator = (e->kind == GeneratorExp_kind);
1606 int needs_tmp = !is_generator;
1607 comprehension_ty outermost = ((comprehension_ty)
1608 asdl_seq_GET(generators, 0));
1609 /* Outermost iterator is evaluated in current scope */
1610 VISIT(st, expr, outermost->iter);
1611 /* Create comprehension scope for the rest */
1612 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001613 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1614 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 return 0;
1616 }
1617 st->st_cur->ste_generator = is_generator;
1618 /* Outermost iter is received as an argument */
1619 if (!symtable_implicit_arg(st, 0)) {
1620 symtable_exit_block(st, (void *)e);
1621 return 0;
1622 }
1623 /* Allocate temporary name if needed */
1624 if (needs_tmp && !symtable_new_tmpname(st)) {
1625 symtable_exit_block(st, (void *)e);
1626 return 0;
1627 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001628 VISIT(st, expr, outermost->target);
1629 VISIT_SEQ(st, expr, outermost->ifs);
1630 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001632 VISIT(st, expr, value);
1633 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001638symtable_visit_genexp(struct symtable *st, expr_ty e)
1639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1641 e->v.GeneratorExp.generators,
1642 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001643}
1644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001646symtable_visit_listcomp(struct symtable *st, expr_ty e)
1647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1649 e->v.ListComp.generators,
1650 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001651}
1652
1653static int
1654symtable_visit_setcomp(struct symtable *st, expr_ty e)
1655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1657 e->v.SetComp.generators,
1658 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001659}
1660
1661static int
1662symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1663{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1665 e->v.DictComp.generators,
1666 e->v.DictComp.key,
1667 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001668}