blob: b6228356bf093c7225f478f82fc21e6c2fbb5d3d [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
Neal Norwitz090b3dd2006-02-28 22:36:46 +000022static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000023ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000024 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 PySTEntryObject *ste = NULL;
27 PyObject *k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 k = PyLong_FromVoidPtr(key);
30 if (k == NULL)
31 goto fail;
32 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
33 if (ste == NULL)
34 goto fail;
35 ste->ste_table = st;
36 ste->ste_id = k;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 ste->ste_name = name;
39 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 ste->ste_symbols = NULL;
42 ste->ste_varnames = NULL;
43 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 ste->ste_symbols = PyDict_New();
46 if (ste->ste_symbols == NULL)
47 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 ste->ste_varnames = PyList_New(0);
50 if (ste->ste_varnames == NULL)
51 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 ste->ste_children = PyList_New(0);
54 if (ste->ste_children == NULL)
55 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 ste->ste_type = block;
58 ste->ste_unoptimized = 0;
59 ste->ste_nested = 0;
60 ste->ste_free = 0;
61 ste->ste_varargs = 0;
62 ste->ste_varkeywords = 0;
63 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000064 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000065 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000067 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 if (st->st_cur != NULL &&
70 (st->st_cur->ste_nested ||
71 st->st_cur->ste_type == FunctionBlock))
72 ste->ste_nested = 1;
73 ste->ste_child_free = 0;
74 ste->ste_generator = 0;
75 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
78 goto fail;
79
80 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000081 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 Py_XDECREF(ste);
83 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084}
85
86static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
90 ste->ste_name,
91 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092}
93
94static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000095ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 ste->ste_table = NULL;
98 Py_XDECREF(ste->ste_id);
99 Py_XDECREF(ste->ste_name);
100 Py_XDECREF(ste->ste_symbols);
101 Py_XDECREF(ste->ste_varnames);
102 Py_XDECREF(ste->ste_children);
103 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000104}
105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000106#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000107
Guido van Rossum6f799372001-09-20 20:46:19 +0000108static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 {"id", T_OBJECT, OFF(ste_id), READONLY},
110 {"name", T_OBJECT, OFF(ste_name), READONLY},
111 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
112 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
113 {"children", T_OBJECT, OFF(ste_children), READONLY},
114 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
115 {"nested", T_INT, OFF(ste_nested), READONLY},
116 {"type", T_INT, OFF(ste_type), READONLY},
117 {"lineno", T_INT, OFF(ste_lineno), READONLY},
118 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119};
120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 PyVarObject_HEAD_INIT(&PyType_Type, 0)
123 "symtable entry",
124 sizeof(PySTEntryObject),
125 0,
126 (destructor)ste_dealloc, /* tp_dealloc */
127 0, /* tp_print */
128 0, /* tp_getattr */
129 0, /* tp_setattr */
130 0, /* tp_reserved */
131 (reprfunc)ste_repr, /* tp_repr */
132 0, /* tp_as_number */
133 0, /* tp_as_sequence */
134 0, /* tp_as_mapping */
135 0, /* tp_hash */
136 0, /* tp_call */
137 0, /* tp_str */
138 PyObject_GenericGetAttr, /* tp_getattro */
139 0, /* tp_setattro */
140 0, /* tp_as_buffer */
141 Py_TPFLAGS_DEFAULT, /* tp_flags */
142 0, /* tp_doc */
143 0, /* tp_traverse */
144 0, /* tp_clear */
145 0, /* tp_richcompare */
146 0, /* tp_weaklistoffset */
147 0, /* tp_iter */
148 0, /* tp_iternext */
149 0, /* tp_methods */
150 ste_memberlist, /* tp_members */
151 0, /* tp_getset */
152 0, /* tp_base */
153 0, /* tp_dict */
154 0, /* tp_descr_get */
155 0, /* tp_descr_set */
156 0, /* tp_dictoffset */
157 0, /* tp_init */
158 0, /* tp_alloc */
159 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000160};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161
162static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000163static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000165 _Py_block_ty block, void *ast, int lineno,
166 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static int symtable_exit_block(struct symtable *st, void *ast);
168static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
169static int symtable_visit_expr(struct symtable *st, expr_ty s);
170static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000171static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
172static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000173static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static int symtable_visit_arguments(struct symtable *st, arguments_ty);
175static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
176static int symtable_visit_alias(struct symtable *st, alias_ty);
177static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
178static int symtable_visit_keyword(struct symtable *st, keyword_ty);
179static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000180static int symtable_visit_params(struct symtable *st, asdl_seq *args);
181static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000183static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500184static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185
186
Nick Coghlan650f0d02007-04-15 12:05:43 +0000187static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
189 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
194#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000195"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
197static struct symtable *
198symtable_new(void)
199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
203 if (st == NULL)
204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 st->st_filename = NULL;
207 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 if ((st->st_stack = PyList_New(0)) == NULL)
210 goto fail;
211 if ((st->st_blocks = PyDict_New()) == NULL)
212 goto fail;
213 st->st_cur = NULL;
214 st->st_private = NULL;
215 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 PySymtable_Free(st);
218 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219}
220
221struct symtable *
222PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
223{
Benjamin Petersonf5ff2232011-06-19 19:42:22 -0500224 struct symtable *st;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 asdl_seq *seq;
226 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227
Benjamin Petersonf5ff2232011-06-19 19:42:22 -0500228 if (__class__ == NULL) {
229 __class__ = PyUnicode_InternFromString("@__class__");
230 if (__class__ == NULL)
231 return NULL;
232 }
233
234 st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 if (st == NULL)
236 return st;
237 st->st_filename = filename;
238 st->st_future = future;
239 /* Make the initial symbol information gathering pass */
240 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000241 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 PySymtable_Free(st);
243 return NULL;
244 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 st->st_top = st->st_cur;
247 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
248 switch (mod->kind) {
249 case Module_kind:
250 seq = mod->v.Module.body;
251 for (i = 0; i < asdl_seq_LEN(seq); i++)
252 if (!symtable_visit_stmt(st,
253 (stmt_ty)asdl_seq_GET(seq, i)))
254 goto error;
255 break;
256 case Expression_kind:
257 if (!symtable_visit_expr(st, mod->v.Expression.body))
258 goto error;
259 break;
260 case Interactive_kind:
261 seq = mod->v.Interactive.body;
262 for (i = 0; i < asdl_seq_LEN(seq); i++)
263 if (!symtable_visit_stmt(st,
264 (stmt_ty)asdl_seq_GET(seq, i)))
265 goto error;
266 break;
267 case Suite_kind:
268 PyErr_SetString(PyExc_RuntimeError,
269 "this compiler does not handle Suites");
270 goto error;
271 }
272 if (!symtable_exit_block(st, (void *)mod)) {
273 PySymtable_Free(st);
274 return NULL;
275 }
276 /* Make the second symbol analysis pass */
277 if (symtable_analyze(st))
278 return st;
279 PySymtable_Free(st);
280 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 (void) symtable_exit_block(st, (void *)mod);
283 PySymtable_Free(st);
284 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285}
286
287void
288PySymtable_Free(struct symtable *st)
289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 Py_XDECREF(st->st_blocks);
291 Py_XDECREF(st->st_stack);
292 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293}
294
295PySTEntryObject *
296PySymtable_Lookup(struct symtable *st, void *key)
297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 k = PyLong_FromVoidPtr(key);
301 if (k == NULL)
302 return NULL;
303 v = PyDict_GetItem(st->st_blocks, k);
304 if (v) {
305 assert(PySTEntry_Check(v));
306 Py_INCREF(v);
307 }
308 else {
309 PyErr_SetString(PyExc_KeyError,
310 "unknown symbol table entry");
311 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 Py_DECREF(k);
314 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315}
316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318PyST_GetScope(PySTEntryObject *ste, PyObject *name)
319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
321 if (!v)
322 return 0;
323 assert(PyLong_Check(v));
324 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325}
326
327
328/* Analyze raw symbol information to determine scope of each name.
329
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000330 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336 explicit global is declared with the global statement. An implicit
337 global is a free variable for which the compiler has found no binding
338 in an enclosing function scope. The implicit global is either a global
339 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
340 to handle these names to implement slightly odd semantics. In such a
341 block, the name is treated as global until it is assigned to; then it
342 is treated as a local.
343
344 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000345 The first pass collects raw facts from the AST via the symtable_visit_*
346 functions: the name is a parameter here, the name is used but not defined
347 here, etc. The second pass analyzes these facts during a pass over the
348 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
350 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000352 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000353 Names which are explicitly declared nonlocal must exist in this set of
354 visible names - if they do not, a syntax error is raised. After doing
355 the local analysis, it analyzes each of its child blocks using an
356 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357
Nick Coghlan650f0d02007-04-15 12:05:43 +0000358 The children update the free variable set. If a local variable is added to
359 the free variable set by the child, the variable is marked as a cell. The
360 function object being defined must provide runtime storage for the variable
361 that may outlive the function's frame. Cell variables are removed from the
362 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000363
Nick Coghlan650f0d02007-04-15 12:05:43 +0000364 During analysis, the names are:
365 symbols: dict mapping from symbol names to flag values (including offset scope values)
366 scopes: dict mapping from symbol names to scope values (no offset)
367 local: set of all symbol names local to the current scope
368 bound: set of all symbol names local to a containing function scope
369 free: set of all symbol names referenced but not bound in child scopes
370 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371*/
372
373#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 PyObject *o = PyLong_FromLong(I); \
375 if (!o) \
376 return 0; \
377 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
378 Py_DECREF(o); \
379 return 0; \
380 } \
381 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382}
383
384/* Decide on scope of name, given flags.
385
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000386 The namespace dictionaries may be modified to record information
387 about the new name. For example, a new global will add an entry to
388 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389*/
390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000392analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 PyObject *bound, PyObject *local, PyObject *free,
394 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if (flags & DEF_GLOBAL) {
397 if (flags & DEF_PARAM) {
398 PyErr_Format(PyExc_SyntaxError,
399 "name '%U' is parameter and global",
400 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000401 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
402 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403
404 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000405 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 if (flags & DEF_NONLOCAL) {
407 PyErr_Format(PyExc_SyntaxError,
408 "name '%U' is nonlocal and global",
409 name);
410 return 0;
411 }
412 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
413 if (PySet_Add(global, name) < 0)
414 return 0;
415 if (bound && (PySet_Discard(bound, name) < 0))
416 return 0;
417 return 1;
418 }
419 if (flags & DEF_NONLOCAL) {
420 if (flags & DEF_PARAM) {
421 PyErr_Format(PyExc_SyntaxError,
422 "name '%U' is parameter and nonlocal",
423 name);
424 return 0;
425 }
426 if (!bound) {
427 PyErr_Format(PyExc_SyntaxError,
428 "nonlocal declaration not allowed at module level");
429 return 0;
430 }
431 if (!PySet_Contains(bound, name)) {
432 PyErr_Format(PyExc_SyntaxError,
433 "no binding for nonlocal '%U' found",
434 name);
435
436 return 0;
437 }
438 SET_SCOPE(scopes, name, FREE);
439 ste->ste_free = 1;
440 return PySet_Add(free, name) >= 0;
441 }
442 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000443 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 if (PySet_Add(local, name) < 0)
445 return 0;
446 if (PySet_Discard(global, name) < 0)
447 return 0;
448 return 1;
449 }
450 /* If an enclosing block has a binding for this name, it
451 is a free variable rather than a global variable.
452 Note that having a non-NULL bound implies that the block
453 is nested.
454 */
455 if (bound && PySet_Contains(bound, name)) {
456 SET_SCOPE(scopes, name, FREE);
457 ste->ste_free = 1;
458 return PySet_Add(free, name) >= 0;
459 }
460 /* If a parent has a global statement, then call it global
461 explicit? It could also be global implicit.
462 */
463 if (global && PySet_Contains(global, name)) {
464 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
465 return 1;
466 }
467 if (ste->ste_nested)
468 ste->ste_free = 1;
469 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
470 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471}
472
473#undef SET_SCOPE
474
475/* If a name is defined in free and also in locals, then this block
476 provides the binding for the free variable. The name should be
477 marked CELL in this block and removed from the free list.
478
479 Note that the current block's free variables are included in free.
480 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000481
Martin v. Löwis2673a572007-10-29 19:54:24 +0000482 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000483 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484*/
485
486static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000487analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PyObject *name, *v, *v_cell;
490 int success = 0;
491 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 v_cell = PyLong_FromLong(CELL);
494 if (!v_cell)
495 return 0;
496 while (PyDict_Next(scopes, &pos, &name, &v)) {
497 long scope;
498 assert(PyLong_Check(v));
499 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000500 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 continue;
502 if (!PySet_Contains(free, name))
503 continue;
504 if (restricted != NULL &&
505 PyUnicode_CompareWithASCIIString(name, restricted))
506 continue;
507 /* Replace LOCAL with CELL for this name, and remove
508 from free. It is safe to replace the value of name
509 in the dict, because it will not cause a resize.
510 */
511 if (PyDict_SetItem(scopes, name, v_cell) < 0)
512 goto error;
513 if (PySet_Discard(free, name) < 0)
514 goto error;
515 }
516 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 Py_DECREF(v_cell);
519 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520}
521
522/* Check for illegal statements in unoptimized namespaces */
523static int
524check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
528 || !(ste->ste_free || ste->ste_child_free))
529 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 trailer = (ste->ste_child_free ?
532 "contains a nested function with free variables" :
533 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 switch (ste->ste_unoptimized) {
536 case OPT_TOPLEVEL: /* import * at top-level is fine */
537 return 1;
538 case OPT_IMPORT_STAR:
539 PyErr_Format(PyExc_SyntaxError,
540 "import * is not allowed in function '%U' because it %s",
541 ste->ste_name, trailer);
542 break;
543 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000545 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
546 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548}
549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550/* Enter the final scope information into the ste_symbols dict.
551 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552 * All arguments are dicts. Modifies symbols, others are read-only.
553*/
554static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 PyObject *name = NULL, *itr = NULL;
559 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
560 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* Update scope information for all symbols in this scope */
563 while (PyDict_Next(symbols, &pos, &name, &v)) {
564 long scope, flags;
565 assert(PyLong_Check(v));
566 flags = PyLong_AS_LONG(v);
567 v_scope = PyDict_GetItem(scopes, name);
568 assert(v_scope && PyLong_Check(v_scope));
569 scope = PyLong_AS_LONG(v_scope);
570 flags |= (scope << SCOPE_OFFSET);
571 v_new = PyLong_FromLong(flags);
572 if (!v_new)
573 return 0;
574 if (PyDict_SetItem(symbols, name, v_new) < 0) {
575 Py_DECREF(v_new);
576 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 Py_DECREF(v_new);
579 }
580
581 /* Record not yet resolved free variables from children (if any) */
582 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
583 if (!v_free)
584 return 0;
585
586 itr = PyObject_GetIter(free);
587 if (!itr)
588 goto error;
589
590 while ((name = PyIter_Next(itr))) {
591 v = PyDict_GetItem(symbols, name);
592
593 /* Handle symbol that already exists in this scope */
594 if (v) {
595 /* Handle a free variable in a method of
596 the class that has the same name as a local
597 or global in the class scope.
598 */
599 if (classflag &&
600 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
601 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
602 v_new = PyLong_FromLong(flags);
603 if (!v_new) {
604 goto error;
605 }
606 if (PyDict_SetItem(symbols, name, v_new) < 0) {
607 Py_DECREF(v_new);
608 goto error;
609 }
610 Py_DECREF(v_new);
611 }
612 /* It's a cell, or already free in this scope */
613 Py_DECREF(name);
614 continue;
615 }
616 /* Handle global symbol */
617 if (!PySet_Contains(bound, name)) {
618 Py_DECREF(name);
619 continue; /* it's a global */
620 }
621 /* Propagate new free symbol up the lexical stack */
622 if (PyDict_SetItem(symbols, name, v_free) < 0) {
623 goto error;
624 }
625 Py_DECREF(name);
626 }
627 Py_DECREF(itr);
628 Py_DECREF(v_free);
629 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000630error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 Py_XDECREF(v_free);
632 Py_XDECREF(itr);
633 Py_XDECREF(name);
634 return 0;
635}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636
637/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000638
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 Arguments:
640 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000641 bound -- set of variables bound in enclosing scopes (input). bound
642 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 free -- set of free variables in enclosed scopes (output)
644 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000645
646 The implementation uses two mutually recursive functions,
647 analyze_block() and analyze_child_block(). analyze_block() is
648 responsible for analyzing the individual names defined in a block.
649 analyze_child_block() prepares temporary namespace dictionaries
650 used to evaluated nested blocks.
651
652 The two functions exist because a child block should see the name
653 bindings of its enclosing blocks, but those bindings should not
654 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655*/
656
657static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
659 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000660
661static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
663 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
666 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
667 PyObject *temp;
668 int i, success = 0;
669 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 local = PySet_New(NULL); /* collect new names bound in block */
672 if (!local)
673 goto error;
674 scopes = PyDict_New(); /* collect scopes defined for each name */
675 if (!scopes)
676 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 /* Allocate new global and bound variable dictionaries. These
679 dictionaries hold the names visible in nested blocks. For
680 ClassBlocks, the bound and global names are initialized
681 before analyzing names, because class bindings aren't
682 visible in methods. For other blocks, they are initialized
683 after names are analyzed.
684 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 /* TODO(jhylton): Package these dicts in a struct so that we
687 can write reasonable helper functions?
688 */
689 newglobal = PySet_New(NULL);
690 if (!newglobal)
691 goto error;
692 newfree = PySet_New(NULL);
693 if (!newfree)
694 goto error;
695 newbound = PySet_New(NULL);
696 if (!newbound)
697 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 /* Class namespace has no effect on names visible in
700 nested functions, so populate the global and bound
701 sets to be passed to child blocks before analyzing
702 this one.
703 */
704 if (ste->ste_type == ClassBlock) {
705 /* Pass down known globals */
706 temp = PyNumber_InPlaceOr(newglobal, global);
707 if (!temp)
708 goto error;
709 Py_DECREF(temp);
710 /* Pass down previously bound symbols */
711 if (bound) {
712 temp = PyNumber_InPlaceOr(newbound, bound);
713 if (!temp)
714 goto error;
715 Py_DECREF(temp);
716 }
717 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
720 long flags = PyLong_AS_LONG(v);
721 if (!analyze_name(ste, scopes, name, flags,
722 bound, local, free, global))
723 goto error;
724 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 /* Populate global and bound sets to be passed to children. */
727 if (ste->ste_type != ClassBlock) {
728 /* Add function locals to bound set */
729 if (ste->ste_type == FunctionBlock) {
730 temp = PyNumber_InPlaceOr(newbound, local);
731 if (!temp)
732 goto error;
733 Py_DECREF(temp);
734 }
735 /* Pass down previously bound symbols */
736 if (bound) {
737 temp = PyNumber_InPlaceOr(newbound, bound);
738 if (!temp)
739 goto error;
740 Py_DECREF(temp);
741 }
742 /* Pass down known globals */
743 temp = PyNumber_InPlaceOr(newglobal, global);
744 if (!temp)
745 goto error;
746 Py_DECREF(temp);
747 }
748 else {
749 /* Special-case __class__ */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 assert(PySet_Contains(local, __class__) == 1);
751 if (PySet_Add(newbound, __class__) < 0)
752 goto error;
753 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300755 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000757 newbound, newglobal now contain the names visible in
758 nested blocks. The free variables in the children will
759 be collected in allfree.
760 */
761 allfree = PySet_New(NULL);
762 if (!allfree)
763 goto error;
764 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
765 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
766 PySTEntryObject* entry;
767 assert(c && PySTEntry_Check(c));
768 entry = (PySTEntryObject*)c;
769 if (!analyze_child_block(entry, newbound, newfree, newglobal,
770 allfree))
771 goto error;
772 /* Check if any children have free variables */
773 if (entry->ste_free || entry->ste_child_free)
774 ste->ste_child_free = 1;
775 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 temp = PyNumber_InPlaceOr(newfree, allfree);
778 if (!temp)
779 goto error;
780 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 /* Check if any local variables must be converted to cell variables */
783 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
784 NULL))
785 goto error;
786 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
Benjamin Petersonf5ff2232011-06-19 19:42:22 -0500787 "@__class__"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 goto error;
789 /* Records the results of the analysis in the symbol table entry */
790 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
791 ste->ste_type == ClassBlock))
792 goto error;
793 if (!check_unoptimized(ste))
794 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 temp = PyNumber_InPlaceOr(free, newfree);
797 if (!temp)
798 goto error;
799 Py_DECREF(temp);
800 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000801 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 Py_XDECREF(scopes);
803 Py_XDECREF(local);
804 Py_XDECREF(newbound);
805 Py_XDECREF(newglobal);
806 Py_XDECREF(newfree);
807 Py_XDECREF(allfree);
808 if (!success)
809 assert(PyErr_Occurred());
810 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811}
812
813static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
815 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
818 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 These dictionary are used by all blocks enclosed by the
823 current block. The analyze_block() call modifies these
824 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 */
827 temp_bound = PySet_New(bound);
828 if (!temp_bound)
829 goto error;
830 temp_free = PySet_New(free);
831 if (!temp_free)
832 goto error;
833 temp_global = PySet_New(global);
834 if (!temp_global)
835 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
838 goto error;
839 temp = PyNumber_InPlaceOr(child_free, temp_free);
840 if (!temp)
841 goto error;
842 Py_DECREF(temp);
843 Py_DECREF(temp_bound);
844 Py_DECREF(temp_free);
845 Py_DECREF(temp_global);
846 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000847 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 Py_XDECREF(temp_bound);
849 Py_XDECREF(temp_free);
850 Py_XDECREF(temp_global);
851 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000852}
853
854static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855symtable_analyze(struct symtable *st)
856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyObject *free, *global;
858 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 free = PySet_New(NULL);
861 if (!free)
862 return 0;
863 global = PySet_New(NULL);
864 if (!global) {
865 Py_DECREF(free);
866 return 0;
867 }
868 r = analyze_block(st->st_top, NULL, free, global);
869 Py_DECREF(free);
870 Py_DECREF(global);
871 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872}
873
874
875static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000876symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
879 lineno, NULL, NULL) < 0) {
880 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
881 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000882 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
883 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 }
885 return 0;
886 }
887 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888}
889
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000890/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891 This reference is released when the block is exited, via the DECREF
892 in symtable_exit_block().
893*/
894
895static int
896symtable_exit_block(struct symtable *st, void *ast)
897{
Benjamin Peterson609da582011-06-29 22:52:39 -0500898 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899
Benjamin Peterson609da582011-06-29 22:52:39 -0500900 st->st_cur = NULL;
901 size = PyList_GET_SIZE(st->st_stack);
902 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500903 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500905 if (--size)
906 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 }
908 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909}
910
911static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000913 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914{
Benjamin Peterson609da582011-06-29 22:52:39 -0500915 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916
Benjamin Peterson609da582011-06-29 22:52:39 -0500917 ste = ste_new(st, name, block, ast, lineno, col_offset);
918 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500920 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
921 Py_DECREF(ste);
922 return 0;
923 }
924 prev = st->st_cur;
925 /* The entry is owned by the stack. Borrow it for st_cur. */
926 Py_DECREF(ste);
927 st->st_cur = ste;
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) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500931 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 return 0;
933 }
934 }
935 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936}
937
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000938static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939symtable_lookup(struct symtable *st, PyObject *name)
940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 PyObject *o;
942 PyObject *mangled = _Py_Mangle(st->st_private, name);
943 if (!mangled)
944 return 0;
945 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
946 Py_DECREF(mangled);
947 if (!o)
948 return 0;
949 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950}
951
952static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 PyObject *o;
956 PyObject *dict;
957 long val;
958 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959
Jeremy Hylton81e95022007-02-27 06:50:52 +0000960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 if (!mangled)
962 return 0;
963 dict = st->st_cur->ste_symbols;
964 if ((o = PyDict_GetItem(dict, mangled))) {
965 val = PyLong_AS_LONG(o);
966 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
967 /* Is it better to use 'mangled' or 'name' here? */
968 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000969 PyErr_SyntaxLocationEx(st->st_filename,
970 st->st_cur->ste_lineno,
971 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 goto error;
973 }
974 val |= flag;
975 } else
976 val = flag;
977 o = PyLong_FromLong(val);
978 if (o == NULL)
979 goto error;
980 if (PyDict_SetItem(dict, mangled, o) < 0) {
981 Py_DECREF(o);
982 goto error;
983 }
984 Py_DECREF(o);
985
986 if (flag & DEF_PARAM) {
987 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
988 goto error;
989 } else if (flag & DEF_GLOBAL) {
990 /* XXX need to update DEF_GLOBAL for other flags too;
991 perhaps only DEF_FREE_GLOBAL */
992 val = flag;
993 if ((o = PyDict_GetItem(st->st_global, mangled))) {
994 val |= PyLong_AS_LONG(o);
995 }
996 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 goto error;
999 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1000 Py_DECREF(o);
1001 goto error;
1002 }
1003 Py_DECREF(o);
1004 }
1005 Py_DECREF(mangled);
1006 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001007
1008error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Py_DECREF(mangled);
1010 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011}
1012
1013/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1014 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 function.
1016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1018 useful if the first node in the sequence requires special treatment.
1019*/
1020
1021#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 if (!symtable_visit_ ## TYPE((ST), (V))) \
1023 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001024
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 int i; \
1027 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1028 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1029 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1030 if (!symtable_visit_ ## TYPE((ST), elt)) \
1031 return 0; \
1032 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001034
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 int i; \
1037 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1038 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1039 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1040 if (!symtable_visit_ ## TYPE((ST), elt)) \
1041 return 0; \
1042 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001044
Guido van Rossum4f72a782006-10-27 23:31:49 +00001045#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 int i = 0; \
1047 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1048 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1049 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1050 if (!elt) continue; /* can be NULL */ \
1051 if (!symtable_visit_expr((ST), elt)) \
1052 return 0; \
1053 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001054}
1055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001057symtable_new_tmpname(struct symtable *st)
1058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 char tmpname[256];
1060 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1063 ++st->st_cur->ste_tmpname);
1064 tmp = PyUnicode_InternFromString(tmpname);
1065 if (!tmp)
1066 return 0;
1067 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1068 return 0;
1069 Py_DECREF(tmp);
1070 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001071}
1072
Guido van Rossum4f72a782006-10-27 23:31:49 +00001073
Guido van Rossumc2e20742006-02-27 22:32:47 +00001074static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075symtable_visit_stmt(struct symtable *st, stmt_ty s)
1076{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 switch (s->kind) {
1078 case FunctionDef_kind:
1079 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1080 return 0;
1081 if (s->v.FunctionDef.args->defaults)
1082 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1083 if (s->v.FunctionDef.args->kw_defaults)
1084 VISIT_KWONLYDEFAULTS(st,
1085 s->v.FunctionDef.args->kw_defaults);
1086 if (!symtable_visit_annotations(st, s))
1087 return 0;
1088 if (s->v.FunctionDef.decorator_list)
1089 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1090 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001091 FunctionBlock, (void *)s, s->lineno,
1092 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001094 VISIT(st, arguments, s->v.FunctionDef.args);
1095 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 if (!symtable_exit_block(st, s))
1097 return 0;
1098 break;
1099 case ClassDef_kind: {
1100 PyObject *tmp;
1101 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1102 return 0;
1103 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1104 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1105 if (s->v.ClassDef.starargs)
1106 VISIT(st, expr, s->v.ClassDef.starargs);
1107 if (s->v.ClassDef.kwargs)
1108 VISIT(st, expr, s->v.ClassDef.kwargs);
1109 if (s->v.ClassDef.decorator_list)
1110 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1111 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001112 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 return 0;
Benjamin Petersonf5ff2232011-06-19 19:42:22 -05001114 if (!symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 !GET_IDENTIFIER(__locals__) ||
1116 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1117 symtable_exit_block(st, s);
1118 return 0;
1119 }
1120 tmp = st->st_private;
1121 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001122 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 st->st_private = tmp;
1124 if (!symtable_exit_block(st, s))
1125 return 0;
1126 break;
1127 }
1128 case Return_kind:
1129 if (s->v.Return.value) {
1130 VISIT(st, expr, s->v.Return.value);
1131 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 }
1133 break;
1134 case Delete_kind:
1135 VISIT_SEQ(st, expr, s->v.Delete.targets);
1136 break;
1137 case Assign_kind:
1138 VISIT_SEQ(st, expr, s->v.Assign.targets);
1139 VISIT(st, expr, s->v.Assign.value);
1140 break;
1141 case AugAssign_kind:
1142 VISIT(st, expr, s->v.AugAssign.target);
1143 VISIT(st, expr, s->v.AugAssign.value);
1144 break;
1145 case For_kind:
1146 VISIT(st, expr, s->v.For.target);
1147 VISIT(st, expr, s->v.For.iter);
1148 VISIT_SEQ(st, stmt, s->v.For.body);
1149 if (s->v.For.orelse)
1150 VISIT_SEQ(st, stmt, s->v.For.orelse);
1151 break;
1152 case While_kind:
1153 VISIT(st, expr, s->v.While.test);
1154 VISIT_SEQ(st, stmt, s->v.While.body);
1155 if (s->v.While.orelse)
1156 VISIT_SEQ(st, stmt, s->v.While.orelse);
1157 break;
1158 case If_kind:
1159 /* XXX if 0: and lookup_yield() hacks */
1160 VISIT(st, expr, s->v.If.test);
1161 VISIT_SEQ(st, stmt, s->v.If.body);
1162 if (s->v.If.orelse)
1163 VISIT_SEQ(st, stmt, s->v.If.orelse);
1164 break;
1165 case Raise_kind:
1166 if (s->v.Raise.exc) {
1167 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001168 if (s->v.Raise.cause) {
1169 VISIT(st, expr, s->v.Raise.cause);
1170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 }
1172 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001173 case Try_kind:
1174 VISIT_SEQ(st, stmt, s->v.Try.body);
1175 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1176 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1177 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 break;
1179 case Assert_kind:
1180 VISIT(st, expr, s->v.Assert.test);
1181 if (s->v.Assert.msg)
1182 VISIT(st, expr, s->v.Assert.msg);
1183 break;
1184 case Import_kind:
1185 VISIT_SEQ(st, alias, s->v.Import.names);
1186 /* XXX Don't have the lineno available inside
1187 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001188 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001190 st->st_cur->ste_opt_col_offset = s->col_offset;
1191 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 break;
1193 case ImportFrom_kind:
1194 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1195 /* XXX Don't have the lineno available inside
1196 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001197 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001199 st->st_cur->ste_opt_col_offset = s->col_offset;
1200 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 break;
1202 case Global_kind: {
1203 int i;
1204 asdl_seq *seq = s->v.Global.names;
1205 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1206 identifier name = (identifier)asdl_seq_GET(seq, i);
1207 char *c_name = _PyUnicode_AsString(name);
1208 long cur = symtable_lookup(st, name);
1209 if (cur < 0)
1210 return 0;
1211 if (cur & (DEF_LOCAL | USE)) {
1212 char buf[256];
1213 if (cur & DEF_LOCAL)
1214 PyOS_snprintf(buf, sizeof(buf),
1215 GLOBAL_AFTER_ASSIGN,
1216 c_name);
1217 else
1218 PyOS_snprintf(buf, sizeof(buf),
1219 GLOBAL_AFTER_USE,
1220 c_name);
1221 if (!symtable_warn(st, buf, s->lineno))
1222 return 0;
1223 }
1224 if (!symtable_add_def(st, name, DEF_GLOBAL))
1225 return 0;
1226 }
1227 break;
1228 }
1229 case Nonlocal_kind: {
1230 int i;
1231 asdl_seq *seq = s->v.Nonlocal.names;
1232 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1233 identifier name = (identifier)asdl_seq_GET(seq, i);
1234 char *c_name = _PyUnicode_AsString(name);
1235 long cur = symtable_lookup(st, name);
1236 if (cur < 0)
1237 return 0;
1238 if (cur & (DEF_LOCAL | USE)) {
1239 char buf[256];
1240 if (cur & DEF_LOCAL)
1241 PyOS_snprintf(buf, sizeof(buf),
1242 NONLOCAL_AFTER_ASSIGN,
1243 c_name);
1244 else
1245 PyOS_snprintf(buf, sizeof(buf),
1246 NONLOCAL_AFTER_USE,
1247 c_name);
1248 if (!symtable_warn(st, buf, s->lineno))
1249 return 0;
1250 }
1251 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1252 return 0;
1253 }
1254 break;
1255 }
1256 case Expr_kind:
1257 VISIT(st, expr, s->v.Expr.value);
1258 break;
1259 case Pass_kind:
1260 case Break_kind:
1261 case Continue_kind:
1262 /* nothing to do here */
1263 break;
1264 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001265 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 VISIT_SEQ(st, stmt, s->v.With.body);
1267 break;
1268 }
1269 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270}
1271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273symtable_visit_expr(struct symtable *st, expr_ty e)
1274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 switch (e->kind) {
1276 case BoolOp_kind:
1277 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1278 break;
1279 case BinOp_kind:
1280 VISIT(st, expr, e->v.BinOp.left);
1281 VISIT(st, expr, e->v.BinOp.right);
1282 break;
1283 case UnaryOp_kind:
1284 VISIT(st, expr, e->v.UnaryOp.operand);
1285 break;
1286 case Lambda_kind: {
1287 if (!GET_IDENTIFIER(lambda))
1288 return 0;
1289 if (e->v.Lambda.args->defaults)
1290 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001291 if (e->v.Lambda.args->kw_defaults)
1292 VISIT_KWONLYDEFAULTS(st,
1293 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001295 FunctionBlock, (void *)e, e->lineno,
1296 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001298 VISIT(st, arguments, e->v.Lambda.args);
1299 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 if (!symtable_exit_block(st, (void *)e))
1301 return 0;
1302 break;
1303 }
1304 case IfExp_kind:
1305 VISIT(st, expr, e->v.IfExp.test);
1306 VISIT(st, expr, e->v.IfExp.body);
1307 VISIT(st, expr, e->v.IfExp.orelse);
1308 break;
1309 case Dict_kind:
1310 VISIT_SEQ(st, expr, e->v.Dict.keys);
1311 VISIT_SEQ(st, expr, e->v.Dict.values);
1312 break;
1313 case Set_kind:
1314 VISIT_SEQ(st, expr, e->v.Set.elts);
1315 break;
1316 case GeneratorExp_kind:
1317 if (!symtable_visit_genexp(st, e))
1318 return 0;
1319 break;
1320 case ListComp_kind:
1321 if (!symtable_visit_listcomp(st, e))
1322 return 0;
1323 break;
1324 case SetComp_kind:
1325 if (!symtable_visit_setcomp(st, e))
1326 return 0;
1327 break;
1328 case DictComp_kind:
1329 if (!symtable_visit_dictcomp(st, e))
1330 return 0;
1331 break;
1332 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001333 case YieldFrom_kind: {
1334 expr_ty value;
1335 value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value;
1336 if (value)
1337 VISIT(st, expr, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 break;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 case Compare_kind:
1342 VISIT(st, expr, e->v.Compare.left);
1343 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1344 break;
1345 case Call_kind:
1346 VISIT(st, expr, e->v.Call.func);
1347 VISIT_SEQ(st, expr, e->v.Call.args);
1348 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1349 if (e->v.Call.starargs)
1350 VISIT(st, expr, e->v.Call.starargs);
1351 if (e->v.Call.kwargs)
1352 VISIT(st, expr, e->v.Call.kwargs);
1353 break;
1354 case Num_kind:
1355 case Str_kind:
1356 case Bytes_kind:
1357 case Ellipsis_kind:
1358 /* Nothing to do here. */
1359 break;
1360 /* The following exprs can be assignment targets. */
1361 case Attribute_kind:
1362 VISIT(st, expr, e->v.Attribute.value);
1363 break;
1364 case Subscript_kind:
1365 VISIT(st, expr, e->v.Subscript.value);
1366 VISIT(st, slice, e->v.Subscript.slice);
1367 break;
1368 case Starred_kind:
1369 VISIT(st, expr, e->v.Starred.value);
1370 break;
1371 case Name_kind:
1372 if (!symtable_add_def(st, e->v.Name.id,
1373 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1374 return 0;
1375 /* Special-case super: it counts as a use of __class__ */
1376 if (e->v.Name.ctx == Load &&
1377 st->st_cur->ste_type == FunctionBlock &&
1378 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Benjamin Petersonf5ff2232011-06-19 19:42:22 -05001379 if (!symtable_add_def(st, __class__, USE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 return 0;
1381 }
1382 break;
1383 /* child nodes of List and Tuple will have expr_context set */
1384 case List_kind:
1385 VISIT_SEQ(st, expr, e->v.List.elts);
1386 break;
1387 case Tuple_kind:
1388 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1389 break;
1390 }
1391 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392}
1393
1394static int
1395symtable_implicit_arg(struct symtable *st, int pos)
1396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1398 if (id == NULL)
1399 return 0;
1400 if (!symtable_add_def(st, id, DEF_PARAM)) {
1401 Py_DECREF(id);
1402 return 0;
1403 }
1404 Py_DECREF(id);
1405 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406}
1407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001409symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 if (!args)
1414 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 for (i = 0; i < asdl_seq_LEN(args); i++) {
1417 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1418 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1419 return 0;
1420 }
1421
1422 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423}
1424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001426symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 if (!args)
1431 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 for (i = 0; i < asdl_seq_LEN(args); i++) {
1434 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1435 if (arg->annotation)
1436 VISIT(st, expr, arg->annotation);
1437 }
1438
1439 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001440}
1441
Neal Norwitzc1505362006-12-28 06:47:50 +00001442static int
1443symtable_visit_annotations(struct symtable *st, stmt_ty s)
1444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 arguments_ty a = s->v.FunctionDef.args;
1446
1447 if (a->args && !symtable_visit_argannotations(st, a->args))
1448 return 0;
1449 if (a->varargannotation)
1450 VISIT(st, expr, a->varargannotation);
1451 if (a->kwargannotation)
1452 VISIT(st, expr, a->kwargannotation);
1453 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1454 return 0;
1455 if (s->v.FunctionDef.returns)
1456 VISIT(st, expr, s->v.FunctionDef.returns);
1457 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458}
1459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461symtable_visit_arguments(struct symtable *st, arguments_ty a)
1462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 /* skip default arguments inside function block
1464 XXX should ast be different?
1465 */
1466 if (a->args && !symtable_visit_params(st, a->args))
1467 return 0;
1468 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1469 return 0;
1470 if (a->vararg) {
1471 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1472 return 0;
1473 st->st_cur->ste_varargs = 1;
1474 }
1475 if (a->kwarg) {
1476 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1477 return 0;
1478 st->st_cur->ste_varkeywords = 1;
1479 }
1480 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481}
1482
1483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 if (eh->v.ExceptHandler.type)
1488 VISIT(st, expr, eh->v.ExceptHandler.type);
1489 if (eh->v.ExceptHandler.name)
1490 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1491 return 0;
1492 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1493 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494}
1495
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001496static int
1497symtable_visit_withitem(struct symtable *st, withitem_ty item)
1498{
1499 VISIT(st, expr, item->context_expr);
1500 if (item->optional_vars) {
1501 VISIT(st, expr, item->optional_vars);
1502 }
1503 return 1;
1504}
1505
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508symtable_visit_alias(struct symtable *st, alias_ty a)
1509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001511 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 dotted package name (e.g. spam.eggs)
1513 */
1514 PyObject *store_name;
1515 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001516 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1517 PyUnicode_GET_LENGTH(name), 1);
1518 if (dot != -1) {
1519 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 if (!store_name)
1521 return 0;
1522 }
1523 else {
1524 store_name = name;
1525 Py_INCREF(store_name);
1526 }
1527 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1528 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1529 Py_DECREF(store_name);
1530 return r;
1531 }
1532 else {
1533 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001534 int lineno = st->st_cur->ste_lineno;
1535 int col_offset = st->st_cur->ste_col_offset;
1536 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1537 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1538 Py_DECREF(store_name);
1539 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 }
1541 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1542 Py_DECREF(store_name);
1543 return 1;
1544 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545}
1546
1547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 VISIT(st, expr, lc->target);
1552 VISIT(st, expr, lc->iter);
1553 VISIT_SEQ(st, expr, lc->ifs);
1554 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001555}
1556
1557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559symtable_visit_keyword(struct symtable *st, keyword_ty k)
1560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 VISIT(st, expr, k->value);
1562 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563}
1564
1565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567symtable_visit_slice(struct symtable *st, slice_ty s)
1568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 switch (s->kind) {
1570 case Slice_kind:
1571 if (s->v.Slice.lower)
1572 VISIT(st, expr, s->v.Slice.lower)
1573 if (s->v.Slice.upper)
1574 VISIT(st, expr, s->v.Slice.upper)
1575 if (s->v.Slice.step)
1576 VISIT(st, expr, s->v.Slice.step)
1577 break;
1578 case ExtSlice_kind:
1579 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1580 break;
1581 case Index_kind:
1582 VISIT(st, expr, s->v.Index.value)
1583 break;
1584 }
1585 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586}
1587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001589symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001590 identifier scope_name, asdl_seq *generators,
1591 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 int is_generator = (e->kind == GeneratorExp_kind);
1594 int needs_tmp = !is_generator;
1595 comprehension_ty outermost = ((comprehension_ty)
1596 asdl_seq_GET(generators, 0));
1597 /* Outermost iterator is evaluated in current scope */
1598 VISIT(st, expr, outermost->iter);
1599 /* Create comprehension scope for the rest */
1600 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001601 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1602 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 return 0;
1604 }
1605 st->st_cur->ste_generator = is_generator;
1606 /* Outermost iter is received as an argument */
1607 if (!symtable_implicit_arg(st, 0)) {
1608 symtable_exit_block(st, (void *)e);
1609 return 0;
1610 }
1611 /* Allocate temporary name if needed */
1612 if (needs_tmp && !symtable_new_tmpname(st)) {
1613 symtable_exit_block(st, (void *)e);
1614 return 0;
1615 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001616 VISIT(st, expr, outermost->target);
1617 VISIT_SEQ(st, expr, outermost->ifs);
1618 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001620 VISIT(st, expr, value);
1621 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001626symtable_visit_genexp(struct symtable *st, expr_ty e)
1627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1629 e->v.GeneratorExp.generators,
1630 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001631}
1632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001634symtable_visit_listcomp(struct symtable *st, expr_ty e)
1635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1637 e->v.ListComp.generators,
1638 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001639}
1640
1641static int
1642symtable_visit_setcomp(struct symtable *st, expr_ty e)
1643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1645 e->v.SetComp.generators,
1646 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001647}
1648
1649static int
1650symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1653 e->v.DictComp.generators,
1654 e->v.DictComp.key,
1655 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001656}