blob: a0b786b3d7a5fd627256b8af584a4e64d2a647e7 [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{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000224 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 asdl_seq *seq;
226 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (st == NULL)
229 return st;
230 st->st_filename = filename;
231 st->st_future = future;
232 /* Make the initial symbol information gathering pass */
233 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000234 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 PySymtable_Free(st);
236 return NULL;
237 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 st->st_top = st->st_cur;
240 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
241 switch (mod->kind) {
242 case Module_kind:
243 seq = mod->v.Module.body;
244 for (i = 0; i < asdl_seq_LEN(seq); i++)
245 if (!symtable_visit_stmt(st,
246 (stmt_ty)asdl_seq_GET(seq, i)))
247 goto error;
248 break;
249 case Expression_kind:
250 if (!symtable_visit_expr(st, mod->v.Expression.body))
251 goto error;
252 break;
253 case Interactive_kind:
254 seq = mod->v.Interactive.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 Suite_kind:
261 PyErr_SetString(PyExc_RuntimeError,
262 "this compiler does not handle Suites");
263 goto error;
264 }
265 if (!symtable_exit_block(st, (void *)mod)) {
266 PySymtable_Free(st);
267 return NULL;
268 }
269 /* Make the second symbol analysis pass */
270 if (symtable_analyze(st))
271 return st;
272 PySymtable_Free(st);
273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 (void) symtable_exit_block(st, (void *)mod);
276 PySymtable_Free(st);
277 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000278}
279
280void
281PySymtable_Free(struct symtable *st)
282{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 Py_XDECREF(st->st_blocks);
284 Py_XDECREF(st->st_stack);
285 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286}
287
288PySTEntryObject *
289PySymtable_Lookup(struct symtable *st, void *key)
290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 k = PyLong_FromVoidPtr(key);
294 if (k == NULL)
295 return NULL;
296 v = PyDict_GetItem(st->st_blocks, k);
297 if (v) {
298 assert(PySTEntry_Check(v));
299 Py_INCREF(v);
300 }
301 else {
302 PyErr_SetString(PyExc_KeyError,
303 "unknown symbol table entry");
304 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 Py_DECREF(k);
307 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308}
309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000311PyST_GetScope(PySTEntryObject *ste, PyObject *name)
312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
314 if (!v)
315 return 0;
316 assert(PyLong_Check(v));
317 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318}
319
320
321/* Analyze raw symbol information to determine scope of each name.
322
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000323 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329 explicit global is declared with the global statement. An implicit
330 global is a free variable for which the compiler has found no binding
331 in an enclosing function scope. The implicit global is either a global
332 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
333 to handle these names to implement slightly odd semantics. In such a
334 block, the name is treated as global until it is assigned to; then it
335 is treated as a local.
336
337 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000338 The first pass collects raw facts from the AST via the symtable_visit_*
339 functions: the name is a parameter here, the name is used but not defined
340 here, etc. The second pass analyzes these facts during a pass over the
341 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342
343 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000345 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000346 Names which are explicitly declared nonlocal must exist in this set of
347 visible names - if they do not, a syntax error is raised. After doing
348 the local analysis, it analyzes each of its child blocks using an
349 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350
Nick Coghlan650f0d02007-04-15 12:05:43 +0000351 The children update the free variable set. If a local variable is added to
352 the free variable set by the child, the variable is marked as a cell. The
353 function object being defined must provide runtime storage for the variable
354 that may outlive the function's frame. Cell variables are removed from the
355 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000356
Nick Coghlan650f0d02007-04-15 12:05:43 +0000357 During analysis, the names are:
358 symbols: dict mapping from symbol names to flag values (including offset scope values)
359 scopes: dict mapping from symbol names to scope values (no offset)
360 local: set of all symbol names local to the current scope
361 bound: set of all symbol names local to a containing function scope
362 free: set of all symbol names referenced but not bound in child scopes
363 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364*/
365
366#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 PyObject *o = PyLong_FromLong(I); \
368 if (!o) \
369 return 0; \
370 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
371 Py_DECREF(o); \
372 return 0; \
373 } \
374 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375}
376
377/* Decide on scope of name, given flags.
378
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000379 The namespace dictionaries may be modified to record information
380 about the new name. For example, a new global will add an entry to
381 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382*/
383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000385analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 PyObject *bound, PyObject *local, PyObject *free,
387 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 if (flags & DEF_GLOBAL) {
390 if (flags & DEF_PARAM) {
391 PyErr_Format(PyExc_SyntaxError,
392 "name '%U' is parameter and global",
393 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000394 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
395 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396
397 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (flags & DEF_NONLOCAL) {
400 PyErr_Format(PyExc_SyntaxError,
401 "name '%U' is nonlocal and global",
402 name);
403 return 0;
404 }
405 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
406 if (PySet_Add(global, name) < 0)
407 return 0;
408 if (bound && (PySet_Discard(bound, name) < 0))
409 return 0;
410 return 1;
411 }
412 if (flags & DEF_NONLOCAL) {
413 if (flags & DEF_PARAM) {
414 PyErr_Format(PyExc_SyntaxError,
415 "name '%U' is parameter and nonlocal",
416 name);
417 return 0;
418 }
419 if (!bound) {
420 PyErr_Format(PyExc_SyntaxError,
421 "nonlocal declaration not allowed at module level");
422 return 0;
423 }
424 if (!PySet_Contains(bound, name)) {
425 PyErr_Format(PyExc_SyntaxError,
426 "no binding for nonlocal '%U' found",
427 name);
428
429 return 0;
430 }
431 SET_SCOPE(scopes, name, FREE);
432 ste->ste_free = 1;
433 return PySet_Add(free, name) >= 0;
434 }
435 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000436 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (PySet_Add(local, name) < 0)
438 return 0;
439 if (PySet_Discard(global, name) < 0)
440 return 0;
441 return 1;
442 }
443 /* If an enclosing block has a binding for this name, it
444 is a free variable rather than a global variable.
445 Note that having a non-NULL bound implies that the block
446 is nested.
447 */
448 if (bound && PySet_Contains(bound, name)) {
449 SET_SCOPE(scopes, name, FREE);
450 ste->ste_free = 1;
451 return PySet_Add(free, name) >= 0;
452 }
453 /* If a parent has a global statement, then call it global
454 explicit? It could also be global implicit.
455 */
456 if (global && PySet_Contains(global, name)) {
457 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
458 return 1;
459 }
460 if (ste->ste_nested)
461 ste->ste_free = 1;
462 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
463 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464}
465
466#undef SET_SCOPE
467
468/* If a name is defined in free and also in locals, then this block
469 provides the binding for the free variable. The name should be
470 marked CELL in this block and removed from the free list.
471
472 Note that the current block's free variables are included in free.
473 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000474
Martin v. Löwis2673a572007-10-29 19:54:24 +0000475 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000476 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477*/
478
479static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000480analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 PyObject *name, *v, *v_cell;
483 int success = 0;
484 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 v_cell = PyLong_FromLong(CELL);
487 if (!v_cell)
488 return 0;
489 while (PyDict_Next(scopes, &pos, &name, &v)) {
490 long scope;
491 assert(PyLong_Check(v));
492 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000493 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 continue;
495 if (!PySet_Contains(free, name))
496 continue;
497 if (restricted != NULL &&
498 PyUnicode_CompareWithASCIIString(name, restricted))
499 continue;
500 /* Replace LOCAL with CELL for this name, and remove
501 from free. It is safe to replace the value of name
502 in the dict, because it will not cause a resize.
503 */
504 if (PyDict_SetItem(scopes, name, v_cell) < 0)
505 goto error;
506 if (PySet_Discard(free, name) < 0)
507 goto error;
508 }
509 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 Py_DECREF(v_cell);
512 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513}
514
515/* Check for illegal statements in unoptimized namespaces */
516static int
517check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
521 || !(ste->ste_free || ste->ste_child_free))
522 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 trailer = (ste->ste_child_free ?
525 "contains a nested function with free variables" :
526 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 switch (ste->ste_unoptimized) {
529 case OPT_TOPLEVEL: /* import * at top-level is fine */
530 return 1;
531 case OPT_IMPORT_STAR:
532 PyErr_Format(PyExc_SyntaxError,
533 "import * is not allowed in function '%U' because it %s",
534 ste->ste_name, trailer);
535 break;
536 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000538 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
539 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541}
542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543/* Enter the final scope information into the ste_symbols dict.
544 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 * All arguments are dicts. Modifies symbols, others are read-only.
546*/
547static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 PyObject *name = NULL, *itr = NULL;
552 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
553 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* Update scope information for all symbols in this scope */
556 while (PyDict_Next(symbols, &pos, &name, &v)) {
557 long scope, flags;
558 assert(PyLong_Check(v));
559 flags = PyLong_AS_LONG(v);
560 v_scope = PyDict_GetItem(scopes, name);
561 assert(v_scope && PyLong_Check(v_scope));
562 scope = PyLong_AS_LONG(v_scope);
563 flags |= (scope << SCOPE_OFFSET);
564 v_new = PyLong_FromLong(flags);
565 if (!v_new)
566 return 0;
567 if (PyDict_SetItem(symbols, name, v_new) < 0) {
568 Py_DECREF(v_new);
569 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 Py_DECREF(v_new);
572 }
573
574 /* Record not yet resolved free variables from children (if any) */
575 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
576 if (!v_free)
577 return 0;
578
579 itr = PyObject_GetIter(free);
580 if (!itr)
581 goto error;
582
583 while ((name = PyIter_Next(itr))) {
584 v = PyDict_GetItem(symbols, name);
585
586 /* Handle symbol that already exists in this scope */
587 if (v) {
588 /* Handle a free variable in a method of
589 the class that has the same name as a local
590 or global in the class scope.
591 */
592 if (classflag &&
593 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
594 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
595 v_new = PyLong_FromLong(flags);
596 if (!v_new) {
597 goto error;
598 }
599 if (PyDict_SetItem(symbols, name, v_new) < 0) {
600 Py_DECREF(v_new);
601 goto error;
602 }
603 Py_DECREF(v_new);
604 }
605 /* It's a cell, or already free in this scope */
606 Py_DECREF(name);
607 continue;
608 }
609 /* Handle global symbol */
610 if (!PySet_Contains(bound, name)) {
611 Py_DECREF(name);
612 continue; /* it's a global */
613 }
614 /* Propagate new free symbol up the lexical stack */
615 if (PyDict_SetItem(symbols, name, v_free) < 0) {
616 goto error;
617 }
618 Py_DECREF(name);
619 }
620 Py_DECREF(itr);
621 Py_DECREF(v_free);
622 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000623error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 Py_XDECREF(v_free);
625 Py_XDECREF(itr);
626 Py_XDECREF(name);
627 return 0;
628}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629
630/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000631
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632 Arguments:
633 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000634 bound -- set of variables bound in enclosing scopes (input). bound
635 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 free -- set of free variables in enclosed scopes (output)
637 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000638
639 The implementation uses two mutually recursive functions,
640 analyze_block() and analyze_child_block(). analyze_block() is
641 responsible for analyzing the individual names defined in a block.
642 analyze_child_block() prepares temporary namespace dictionaries
643 used to evaluated nested blocks.
644
645 The two functions exist because a child block should see the name
646 bindings of its enclosing blocks, but those bindings should not
647 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000648*/
649
650static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
652 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000653
654static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
656 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
659 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
660 PyObject *temp;
661 int i, success = 0;
662 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 local = PySet_New(NULL); /* collect new names bound in block */
665 if (!local)
666 goto error;
667 scopes = PyDict_New(); /* collect scopes defined for each name */
668 if (!scopes)
669 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 /* Allocate new global and bound variable dictionaries. These
672 dictionaries hold the names visible in nested blocks. For
673 ClassBlocks, the bound and global names are initialized
674 before analyzing names, because class bindings aren't
675 visible in methods. For other blocks, they are initialized
676 after names are analyzed.
677 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 /* TODO(jhylton): Package these dicts in a struct so that we
680 can write reasonable helper functions?
681 */
682 newglobal = PySet_New(NULL);
683 if (!newglobal)
684 goto error;
685 newfree = PySet_New(NULL);
686 if (!newfree)
687 goto error;
688 newbound = PySet_New(NULL);
689 if (!newbound)
690 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 /* Class namespace has no effect on names visible in
693 nested functions, so populate the global and bound
694 sets to be passed to child blocks before analyzing
695 this one.
696 */
697 if (ste->ste_type == ClassBlock) {
698 /* Pass down known globals */
699 temp = PyNumber_InPlaceOr(newglobal, global);
700 if (!temp)
701 goto error;
702 Py_DECREF(temp);
703 /* Pass down previously bound symbols */
704 if (bound) {
705 temp = PyNumber_InPlaceOr(newbound, bound);
706 if (!temp)
707 goto error;
708 Py_DECREF(temp);
709 }
710 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
713 long flags = PyLong_AS_LONG(v);
714 if (!analyze_name(ste, scopes, name, flags,
715 bound, local, free, global))
716 goto error;
717 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 /* Populate global and bound sets to be passed to children. */
720 if (ste->ste_type != ClassBlock) {
721 /* Add function locals to bound set */
722 if (ste->ste_type == FunctionBlock) {
723 temp = PyNumber_InPlaceOr(newbound, local);
724 if (!temp)
725 goto error;
726 Py_DECREF(temp);
727 }
728 /* Pass down previously bound symbols */
729 if (bound) {
730 temp = PyNumber_InPlaceOr(newbound, bound);
731 if (!temp)
732 goto error;
733 Py_DECREF(temp);
734 }
735 /* Pass down known globals */
736 temp = PyNumber_InPlaceOr(newglobal, global);
737 if (!temp)
738 goto error;
739 Py_DECREF(temp);
740 }
741 else {
742 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000743 if (!GET_IDENTIFIER(__class__))
744 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 assert(PySet_Contains(local, __class__) == 1);
746 if (PySet_Add(newbound, __class__) < 0)
747 goto error;
748 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300750 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 newbound, newglobal now contain the names visible in
753 nested blocks. The free variables in the children will
754 be collected in allfree.
755 */
756 allfree = PySet_New(NULL);
757 if (!allfree)
758 goto error;
759 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
760 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
761 PySTEntryObject* entry;
762 assert(c && PySTEntry_Check(c));
763 entry = (PySTEntryObject*)c;
764 if (!analyze_child_block(entry, newbound, newfree, newglobal,
765 allfree))
766 goto error;
767 /* Check if any children have free variables */
768 if (entry->ste_free || entry->ste_child_free)
769 ste->ste_child_free = 1;
770 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 temp = PyNumber_InPlaceOr(newfree, allfree);
773 if (!temp)
774 goto error;
775 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 /* Check if any local variables must be converted to cell variables */
778 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
779 NULL))
780 goto error;
781 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000782 "__class__"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 goto error;
784 /* Records the results of the analysis in the symbol table entry */
785 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
786 ste->ste_type == ClassBlock))
787 goto error;
788 if (!check_unoptimized(ste))
789 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 temp = PyNumber_InPlaceOr(free, newfree);
792 if (!temp)
793 goto error;
794 Py_DECREF(temp);
795 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 Py_XDECREF(scopes);
798 Py_XDECREF(local);
799 Py_XDECREF(newbound);
800 Py_XDECREF(newglobal);
801 Py_XDECREF(newfree);
802 Py_XDECREF(allfree);
803 if (!success)
804 assert(PyErr_Occurred());
805 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000806}
807
808static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
810 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
813 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 These dictionary are used by all blocks enclosed by the
818 current block. The analyze_block() call modifies these
819 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 */
822 temp_bound = PySet_New(bound);
823 if (!temp_bound)
824 goto error;
825 temp_free = PySet_New(free);
826 if (!temp_free)
827 goto error;
828 temp_global = PySet_New(global);
829 if (!temp_global)
830 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
833 goto error;
834 temp = PyNumber_InPlaceOr(child_free, temp_free);
835 if (!temp)
836 goto error;
837 Py_DECREF(temp);
838 Py_DECREF(temp_bound);
839 Py_DECREF(temp_free);
840 Py_DECREF(temp_global);
841 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000842 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 Py_XDECREF(temp_bound);
844 Py_XDECREF(temp_free);
845 Py_XDECREF(temp_global);
846 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000847}
848
849static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850symtable_analyze(struct symtable *st)
851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 PyObject *free, *global;
853 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 free = PySet_New(NULL);
856 if (!free)
857 return 0;
858 global = PySet_New(NULL);
859 if (!global) {
860 Py_DECREF(free);
861 return 0;
862 }
863 r = analyze_block(st->st_top, NULL, free, global);
864 Py_DECREF(free);
865 Py_DECREF(global);
866 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867}
868
869
870static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000871symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
874 lineno, NULL, NULL) < 0) {
875 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
876 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000877 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
878 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 }
880 return 0;
881 }
882 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883}
884
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000885/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886 This reference is released when the block is exited, via the DECREF
887 in symtable_exit_block().
888*/
889
890static int
891symtable_exit_block(struct symtable *st, void *ast)
892{
Benjamin Peterson609da582011-06-29 22:52:39 -0500893 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894
Benjamin Peterson609da582011-06-29 22:52:39 -0500895 st->st_cur = NULL;
896 size = PyList_GET_SIZE(st->st_stack);
897 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500898 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500900 if (--size)
901 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 }
903 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904}
905
906static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000908 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909{
Benjamin Peterson609da582011-06-29 22:52:39 -0500910 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911
Benjamin Peterson609da582011-06-29 22:52:39 -0500912 ste = ste_new(st, name, block, ast, lineno, col_offset);
913 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500915 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
916 Py_DECREF(ste);
917 return 0;
918 }
919 prev = st->st_cur;
920 /* The entry is owned by the stack. Borrow it for st_cur. */
921 Py_DECREF(ste);
922 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000923 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 st->st_global = st->st_cur->ste_symbols;
925 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500926 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 return 0;
928 }
929 }
930 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931}
932
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000933static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934symtable_lookup(struct symtable *st, PyObject *name)
935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 PyObject *o;
937 PyObject *mangled = _Py_Mangle(st->st_private, name);
938 if (!mangled)
939 return 0;
940 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
941 Py_DECREF(mangled);
942 if (!o)
943 return 0;
944 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945}
946
947static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 PyObject *o;
951 PyObject *dict;
952 long val;
953 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954
Jeremy Hylton81e95022007-02-27 06:50:52 +0000955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 if (!mangled)
957 return 0;
958 dict = st->st_cur->ste_symbols;
959 if ((o = PyDict_GetItem(dict, mangled))) {
960 val = PyLong_AS_LONG(o);
961 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
962 /* Is it better to use 'mangled' or 'name' here? */
963 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000964 PyErr_SyntaxLocationEx(st->st_filename,
965 st->st_cur->ste_lineno,
966 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 goto error;
968 }
969 val |= flag;
970 } else
971 val = flag;
972 o = PyLong_FromLong(val);
973 if (o == NULL)
974 goto error;
975 if (PyDict_SetItem(dict, mangled, o) < 0) {
976 Py_DECREF(o);
977 goto error;
978 }
979 Py_DECREF(o);
980
981 if (flag & DEF_PARAM) {
982 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
983 goto error;
984 } else if (flag & DEF_GLOBAL) {
985 /* XXX need to update DEF_GLOBAL for other flags too;
986 perhaps only DEF_FREE_GLOBAL */
987 val = flag;
988 if ((o = PyDict_GetItem(st->st_global, mangled))) {
989 val |= PyLong_AS_LONG(o);
990 }
991 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 goto error;
994 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
995 Py_DECREF(o);
996 goto error;
997 }
998 Py_DECREF(o);
999 }
1000 Py_DECREF(mangled);
1001 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001002
1003error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 Py_DECREF(mangled);
1005 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006}
1007
1008/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1009 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 function.
1011
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1013 useful if the first node in the sequence requires special treatment.
1014*/
1015
1016#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 if (!symtable_visit_ ## TYPE((ST), (V))) \
1018 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 int i; \
1022 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1023 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1024 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1025 if (!symtable_visit_ ## TYPE((ST), elt)) \
1026 return 0; \
1027 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001029
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 int i; \
1032 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1033 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1034 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1035 if (!symtable_visit_ ## TYPE((ST), elt)) \
1036 return 0; \
1037 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001039
Guido van Rossum4f72a782006-10-27 23:31:49 +00001040#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 int i = 0; \
1042 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1043 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1044 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1045 if (!elt) continue; /* can be NULL */ \
1046 if (!symtable_visit_expr((ST), elt)) \
1047 return 0; \
1048 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001049}
1050
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001052symtable_new_tmpname(struct symtable *st)
1053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 char tmpname[256];
1055 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1058 ++st->st_cur->ste_tmpname);
1059 tmp = PyUnicode_InternFromString(tmpname);
1060 if (!tmp)
1061 return 0;
1062 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1063 return 0;
1064 Py_DECREF(tmp);
1065 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001066}
1067
Guido van Rossum4f72a782006-10-27 23:31:49 +00001068
Guido van Rossumc2e20742006-02-27 22:32:47 +00001069static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070symtable_visit_stmt(struct symtable *st, stmt_ty s)
1071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 switch (s->kind) {
1073 case FunctionDef_kind:
1074 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1075 return 0;
1076 if (s->v.FunctionDef.args->defaults)
1077 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1078 if (s->v.FunctionDef.args->kw_defaults)
1079 VISIT_KWONLYDEFAULTS(st,
1080 s->v.FunctionDef.args->kw_defaults);
1081 if (!symtable_visit_annotations(st, s))
1082 return 0;
1083 if (s->v.FunctionDef.decorator_list)
1084 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1085 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001086 FunctionBlock, (void *)s, s->lineno,
1087 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001089 VISIT(st, arguments, s->v.FunctionDef.args);
1090 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (!symtable_exit_block(st, s))
1092 return 0;
1093 break;
1094 case ClassDef_kind: {
1095 PyObject *tmp;
1096 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1097 return 0;
1098 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1099 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1100 if (s->v.ClassDef.starargs)
1101 VISIT(st, expr, s->v.ClassDef.starargs);
1102 if (s->v.ClassDef.kwargs)
1103 VISIT(st, expr, s->v.ClassDef.kwargs);
1104 if (s->v.ClassDef.decorator_list)
1105 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1106 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001107 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 return 0;
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001109 if (!GET_IDENTIFIER(__class__) ||
1110 !symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 !GET_IDENTIFIER(__locals__) ||
1112 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1113 symtable_exit_block(st, s);
1114 return 0;
1115 }
1116 tmp = st->st_private;
1117 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001118 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 st->st_private = tmp;
1120 if (!symtable_exit_block(st, s))
1121 return 0;
1122 break;
1123 }
1124 case Return_kind:
1125 if (s->v.Return.value) {
1126 VISIT(st, expr, s->v.Return.value);
1127 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 }
1129 break;
1130 case Delete_kind:
1131 VISIT_SEQ(st, expr, s->v.Delete.targets);
1132 break;
1133 case Assign_kind:
1134 VISIT_SEQ(st, expr, s->v.Assign.targets);
1135 VISIT(st, expr, s->v.Assign.value);
1136 break;
1137 case AugAssign_kind:
1138 VISIT(st, expr, s->v.AugAssign.target);
1139 VISIT(st, expr, s->v.AugAssign.value);
1140 break;
1141 case For_kind:
1142 VISIT(st, expr, s->v.For.target);
1143 VISIT(st, expr, s->v.For.iter);
1144 VISIT_SEQ(st, stmt, s->v.For.body);
1145 if (s->v.For.orelse)
1146 VISIT_SEQ(st, stmt, s->v.For.orelse);
1147 break;
1148 case While_kind:
1149 VISIT(st, expr, s->v.While.test);
1150 VISIT_SEQ(st, stmt, s->v.While.body);
1151 if (s->v.While.orelse)
1152 VISIT_SEQ(st, stmt, s->v.While.orelse);
1153 break;
1154 case If_kind:
1155 /* XXX if 0: and lookup_yield() hacks */
1156 VISIT(st, expr, s->v.If.test);
1157 VISIT_SEQ(st, stmt, s->v.If.body);
1158 if (s->v.If.orelse)
1159 VISIT_SEQ(st, stmt, s->v.If.orelse);
1160 break;
1161 case Raise_kind:
1162 if (s->v.Raise.exc) {
1163 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001164 if (s->v.Raise.cause) {
1165 VISIT(st, expr, s->v.Raise.cause);
1166 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 }
1168 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001169 case Try_kind:
1170 VISIT_SEQ(st, stmt, s->v.Try.body);
1171 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1172 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1173 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 break;
1175 case Assert_kind:
1176 VISIT(st, expr, s->v.Assert.test);
1177 if (s->v.Assert.msg)
1178 VISIT(st, expr, s->v.Assert.msg);
1179 break;
1180 case Import_kind:
1181 VISIT_SEQ(st, alias, s->v.Import.names);
1182 /* XXX Don't have the lineno available inside
1183 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001184 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001186 st->st_cur->ste_opt_col_offset = s->col_offset;
1187 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 break;
1189 case ImportFrom_kind:
1190 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1191 /* XXX Don't have the lineno available inside
1192 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001193 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001195 st->st_cur->ste_opt_col_offset = s->col_offset;
1196 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 break;
1198 case Global_kind: {
1199 int i;
1200 asdl_seq *seq = s->v.Global.names;
1201 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1202 identifier name = (identifier)asdl_seq_GET(seq, i);
1203 char *c_name = _PyUnicode_AsString(name);
1204 long cur = symtable_lookup(st, name);
1205 if (cur < 0)
1206 return 0;
1207 if (cur & (DEF_LOCAL | USE)) {
1208 char buf[256];
1209 if (cur & DEF_LOCAL)
1210 PyOS_snprintf(buf, sizeof(buf),
1211 GLOBAL_AFTER_ASSIGN,
1212 c_name);
1213 else
1214 PyOS_snprintf(buf, sizeof(buf),
1215 GLOBAL_AFTER_USE,
1216 c_name);
1217 if (!symtable_warn(st, buf, s->lineno))
1218 return 0;
1219 }
1220 if (!symtable_add_def(st, name, DEF_GLOBAL))
1221 return 0;
1222 }
1223 break;
1224 }
1225 case Nonlocal_kind: {
1226 int i;
1227 asdl_seq *seq = s->v.Nonlocal.names;
1228 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1229 identifier name = (identifier)asdl_seq_GET(seq, i);
1230 char *c_name = _PyUnicode_AsString(name);
1231 long cur = symtable_lookup(st, name);
1232 if (cur < 0)
1233 return 0;
1234 if (cur & (DEF_LOCAL | USE)) {
1235 char buf[256];
1236 if (cur & DEF_LOCAL)
1237 PyOS_snprintf(buf, sizeof(buf),
1238 NONLOCAL_AFTER_ASSIGN,
1239 c_name);
1240 else
1241 PyOS_snprintf(buf, sizeof(buf),
1242 NONLOCAL_AFTER_USE,
1243 c_name);
1244 if (!symtable_warn(st, buf, s->lineno))
1245 return 0;
1246 }
1247 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1248 return 0;
1249 }
1250 break;
1251 }
1252 case Expr_kind:
1253 VISIT(st, expr, s->v.Expr.value);
1254 break;
1255 case Pass_kind:
1256 case Break_kind:
1257 case Continue_kind:
1258 /* nothing to do here */
1259 break;
1260 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001261 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 VISIT_SEQ(st, stmt, s->v.With.body);
1263 break;
1264 }
1265 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266}
1267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269symtable_visit_expr(struct symtable *st, expr_ty e)
1270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 switch (e->kind) {
1272 case BoolOp_kind:
1273 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1274 break;
1275 case BinOp_kind:
1276 VISIT(st, expr, e->v.BinOp.left);
1277 VISIT(st, expr, e->v.BinOp.right);
1278 break;
1279 case UnaryOp_kind:
1280 VISIT(st, expr, e->v.UnaryOp.operand);
1281 break;
1282 case Lambda_kind: {
1283 if (!GET_IDENTIFIER(lambda))
1284 return 0;
1285 if (e->v.Lambda.args->defaults)
1286 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001287 if (e->v.Lambda.args->kw_defaults)
1288 VISIT_KWONLYDEFAULTS(st,
1289 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001291 FunctionBlock, (void *)e, e->lineno,
1292 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001294 VISIT(st, arguments, e->v.Lambda.args);
1295 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 if (!symtable_exit_block(st, (void *)e))
1297 return 0;
1298 break;
1299 }
1300 case IfExp_kind:
1301 VISIT(st, expr, e->v.IfExp.test);
1302 VISIT(st, expr, e->v.IfExp.body);
1303 VISIT(st, expr, e->v.IfExp.orelse);
1304 break;
1305 case Dict_kind:
1306 VISIT_SEQ(st, expr, e->v.Dict.keys);
1307 VISIT_SEQ(st, expr, e->v.Dict.values);
1308 break;
1309 case Set_kind:
1310 VISIT_SEQ(st, expr, e->v.Set.elts);
1311 break;
1312 case GeneratorExp_kind:
1313 if (!symtable_visit_genexp(st, e))
1314 return 0;
1315 break;
1316 case ListComp_kind:
1317 if (!symtable_visit_listcomp(st, e))
1318 return 0;
1319 break;
1320 case SetComp_kind:
1321 if (!symtable_visit_setcomp(st, e))
1322 return 0;
1323 break;
1324 case DictComp_kind:
1325 if (!symtable_visit_dictcomp(st, e))
1326 return 0;
1327 break;
1328 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001329 case YieldFrom_kind: {
1330 expr_ty value;
1331 value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value;
1332 if (value)
1333 VISIT(st, expr, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 break;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001336 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 case Compare_kind:
1338 VISIT(st, expr, e->v.Compare.left);
1339 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1340 break;
1341 case Call_kind:
1342 VISIT(st, expr, e->v.Call.func);
1343 VISIT_SEQ(st, expr, e->v.Call.args);
1344 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1345 if (e->v.Call.starargs)
1346 VISIT(st, expr, e->v.Call.starargs);
1347 if (e->v.Call.kwargs)
1348 VISIT(st, expr, e->v.Call.kwargs);
1349 break;
1350 case Num_kind:
1351 case Str_kind:
1352 case Bytes_kind:
1353 case Ellipsis_kind:
1354 /* Nothing to do here. */
1355 break;
1356 /* The following exprs can be assignment targets. */
1357 case Attribute_kind:
1358 VISIT(st, expr, e->v.Attribute.value);
1359 break;
1360 case Subscript_kind:
1361 VISIT(st, expr, e->v.Subscript.value);
1362 VISIT(st, slice, e->v.Subscript.slice);
1363 break;
1364 case Starred_kind:
1365 VISIT(st, expr, e->v.Starred.value);
1366 break;
1367 case Name_kind:
1368 if (!symtable_add_def(st, e->v.Name.id,
1369 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1370 return 0;
1371 /* Special-case super: it counts as a use of __class__ */
1372 if (e->v.Name.ctx == Load &&
1373 st->st_cur->ste_type == FunctionBlock &&
1374 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001375 if (!GET_IDENTIFIER(__class__) ||
1376 !symtable_add_def(st, __class__, USE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 return 0;
1378 }
1379 break;
1380 /* child nodes of List and Tuple will have expr_context set */
1381 case List_kind:
1382 VISIT_SEQ(st, expr, e->v.List.elts);
1383 break;
1384 case Tuple_kind:
1385 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1386 break;
1387 }
1388 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389}
1390
1391static int
1392symtable_implicit_arg(struct symtable *st, int pos)
1393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1395 if (id == NULL)
1396 return 0;
1397 if (!symtable_add_def(st, id, DEF_PARAM)) {
1398 Py_DECREF(id);
1399 return 0;
1400 }
1401 Py_DECREF(id);
1402 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403}
1404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001406symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 if (!args)
1411 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 for (i = 0; i < asdl_seq_LEN(args); i++) {
1414 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1415 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1416 return 0;
1417 }
1418
1419 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420}
1421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001423symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 if (!args)
1428 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 for (i = 0; i < asdl_seq_LEN(args); i++) {
1431 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1432 if (arg->annotation)
1433 VISIT(st, expr, arg->annotation);
1434 }
1435
1436 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001437}
1438
Neal Norwitzc1505362006-12-28 06:47:50 +00001439static int
1440symtable_visit_annotations(struct symtable *st, stmt_ty s)
1441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 arguments_ty a = s->v.FunctionDef.args;
1443
1444 if (a->args && !symtable_visit_argannotations(st, a->args))
1445 return 0;
1446 if (a->varargannotation)
1447 VISIT(st, expr, a->varargannotation);
1448 if (a->kwargannotation)
1449 VISIT(st, expr, a->kwargannotation);
1450 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1451 return 0;
1452 if (s->v.FunctionDef.returns)
1453 VISIT(st, expr, s->v.FunctionDef.returns);
1454 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455}
1456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458symtable_visit_arguments(struct symtable *st, arguments_ty a)
1459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 /* skip default arguments inside function block
1461 XXX should ast be different?
1462 */
1463 if (a->args && !symtable_visit_params(st, a->args))
1464 return 0;
1465 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1466 return 0;
1467 if (a->vararg) {
1468 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1469 return 0;
1470 st->st_cur->ste_varargs = 1;
1471 }
1472 if (a->kwarg) {
1473 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1474 return 0;
1475 st->st_cur->ste_varkeywords = 1;
1476 }
1477 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478}
1479
1480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 if (eh->v.ExceptHandler.type)
1485 VISIT(st, expr, eh->v.ExceptHandler.type);
1486 if (eh->v.ExceptHandler.name)
1487 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1488 return 0;
1489 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1490 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491}
1492
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001493static int
1494symtable_visit_withitem(struct symtable *st, withitem_ty item)
1495{
1496 VISIT(st, expr, item->context_expr);
1497 if (item->optional_vars) {
1498 VISIT(st, expr, item->optional_vars);
1499 }
1500 return 1;
1501}
1502
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505symtable_visit_alias(struct symtable *st, alias_ty a)
1506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001508 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 dotted package name (e.g. spam.eggs)
1510 */
1511 PyObject *store_name;
1512 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001513 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1514 PyUnicode_GET_LENGTH(name), 1);
1515 if (dot != -1) {
1516 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 if (!store_name)
1518 return 0;
1519 }
1520 else {
1521 store_name = name;
1522 Py_INCREF(store_name);
1523 }
1524 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1525 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1526 Py_DECREF(store_name);
1527 return r;
1528 }
1529 else {
1530 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001531 int lineno = st->st_cur->ste_lineno;
1532 int col_offset = st->st_cur->ste_col_offset;
1533 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1534 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1535 Py_DECREF(store_name);
1536 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 }
1538 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1539 Py_DECREF(store_name);
1540 return 1;
1541 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542}
1543
1544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 VISIT(st, expr, lc->target);
1549 VISIT(st, expr, lc->iter);
1550 VISIT_SEQ(st, expr, lc->ifs);
1551 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552}
1553
1554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556symtable_visit_keyword(struct symtable *st, keyword_ty k)
1557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 VISIT(st, expr, k->value);
1559 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560}
1561
1562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564symtable_visit_slice(struct symtable *st, slice_ty s)
1565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 switch (s->kind) {
1567 case Slice_kind:
1568 if (s->v.Slice.lower)
1569 VISIT(st, expr, s->v.Slice.lower)
1570 if (s->v.Slice.upper)
1571 VISIT(st, expr, s->v.Slice.upper)
1572 if (s->v.Slice.step)
1573 VISIT(st, expr, s->v.Slice.step)
1574 break;
1575 case ExtSlice_kind:
1576 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1577 break;
1578 case Index_kind:
1579 VISIT(st, expr, s->v.Index.value)
1580 break;
1581 }
1582 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583}
1584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001586symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001587 identifier scope_name, asdl_seq *generators,
1588 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 int is_generator = (e->kind == GeneratorExp_kind);
1591 int needs_tmp = !is_generator;
1592 comprehension_ty outermost = ((comprehension_ty)
1593 asdl_seq_GET(generators, 0));
1594 /* Outermost iterator is evaluated in current scope */
1595 VISIT(st, expr, outermost->iter);
1596 /* Create comprehension scope for the rest */
1597 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001598 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1599 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 return 0;
1601 }
1602 st->st_cur->ste_generator = is_generator;
1603 /* Outermost iter is received as an argument */
1604 if (!symtable_implicit_arg(st, 0)) {
1605 symtable_exit_block(st, (void *)e);
1606 return 0;
1607 }
1608 /* Allocate temporary name if needed */
1609 if (needs_tmp && !symtable_new_tmpname(st)) {
1610 symtable_exit_block(st, (void *)e);
1611 return 0;
1612 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001613 VISIT(st, expr, outermost->target);
1614 VISIT_SEQ(st, expr, outermost->ifs);
1615 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001617 VISIT(st, expr, value);
1618 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001623symtable_visit_genexp(struct symtable *st, expr_ty e)
1624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1626 e->v.GeneratorExp.generators,
1627 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001628}
1629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001631symtable_visit_listcomp(struct symtable *st, expr_ty e)
1632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1634 e->v.ListComp.generators,
1635 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001636}
1637
1638static int
1639symtable_visit_setcomp(struct symtable *st, expr_ty e)
1640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1642 e->v.SetComp.generators,
1643 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001644}
1645
1646static int
1647symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1650 e->v.DictComp.generators,
1651 e->v.DictComp.key,
1652 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001653}