blob: d2bb889f4a24bb9f551f58f65beef1e245968d7c [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;
Christian Heimes837e53a2012-09-10 03:08:46 +020027 PyObject *k = NULL;
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:
Christian Heimes837e53a2012-09-10 03:08:46 +020082 Py_XDECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 Py_XDECREF(ste);
84 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085}
86
87static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
91 ste->ste_name,
92 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093}
94
95static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000096ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 ste->ste_table = NULL;
99 Py_XDECREF(ste->ste_id);
100 Py_XDECREF(ste->ste_name);
101 Py_XDECREF(ste->ste_symbols);
102 Py_XDECREF(ste->ste_varnames);
103 Py_XDECREF(ste->ste_children);
104 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000105}
106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108
Guido van Rossum6f799372001-09-20 20:46:19 +0000109static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 {"id", T_OBJECT, OFF(ste_id), READONLY},
111 {"name", T_OBJECT, OFF(ste_name), READONLY},
112 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
113 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
114 {"children", T_OBJECT, OFF(ste_children), READONLY},
115 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
116 {"nested", T_INT, OFF(ste_nested), READONLY},
117 {"type", T_INT, OFF(ste_type), READONLY},
118 {"lineno", T_INT, OFF(ste_lineno), READONLY},
119 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120};
121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 PyVarObject_HEAD_INIT(&PyType_Type, 0)
124 "symtable entry",
125 sizeof(PySTEntryObject),
126 0,
127 (destructor)ste_dealloc, /* tp_dealloc */
128 0, /* tp_print */
129 0, /* tp_getattr */
130 0, /* tp_setattr */
131 0, /* tp_reserved */
132 (reprfunc)ste_repr, /* tp_repr */
133 0, /* tp_as_number */
134 0, /* tp_as_sequence */
135 0, /* tp_as_mapping */
136 0, /* tp_hash */
137 0, /* tp_call */
138 0, /* tp_str */
139 PyObject_GenericGetAttr, /* tp_getattro */
140 0, /* tp_setattro */
141 0, /* tp_as_buffer */
142 Py_TPFLAGS_DEFAULT, /* tp_flags */
143 0, /* tp_doc */
144 0, /* tp_traverse */
145 0, /* tp_clear */
146 0, /* tp_richcompare */
147 0, /* tp_weaklistoffset */
148 0, /* tp_iter */
149 0, /* tp_iternext */
150 0, /* tp_methods */
151 ste_memberlist, /* tp_members */
152 0, /* tp_getset */
153 0, /* tp_base */
154 0, /* tp_dict */
155 0, /* tp_descr_get */
156 0, /* tp_descr_set */
157 0, /* tp_dictoffset */
158 0, /* tp_init */
159 0, /* tp_alloc */
160 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000161};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000162
163static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000164static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000166 _Py_block_ty block, void *ast, int lineno,
167 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168static int symtable_exit_block(struct symtable *st, void *ast);
169static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
170static int symtable_visit_expr(struct symtable *st, expr_ty s);
171static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000172static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
173static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000174static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000175static int symtable_visit_arguments(struct symtable *st, arguments_ty);
176static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
177static int symtable_visit_alias(struct symtable *st, alias_ty);
178static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
179static int symtable_visit_keyword(struct symtable *st, keyword_ty);
180static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000181static int symtable_visit_params(struct symtable *st, asdl_seq *args);
182static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000184static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500185static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186
187
Nick Coghlan650f0d02007-04-15 12:05:43 +0000188static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
190 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
192#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194
195#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000196"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197
198static struct symtable *
199symtable_new(void)
200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
204 if (st == NULL)
205 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 st->st_filename = NULL;
208 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if ((st->st_stack = PyList_New(0)) == NULL)
211 goto fail;
212 if ((st->st_blocks = PyDict_New()) == NULL)
213 goto fail;
214 st->st_cur = NULL;
215 st->st_private = NULL;
216 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 PySymtable_Free(st);
219 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220}
221
222struct symtable *
223PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
224{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000225 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 asdl_seq *seq;
227 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (st == NULL)
230 return st;
231 st->st_filename = filename;
232 st->st_future = future;
233 /* Make the initial symbol information gathering pass */
234 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000235 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 PySymtable_Free(st);
237 return NULL;
238 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 st->st_top = st->st_cur;
241 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
242 switch (mod->kind) {
243 case Module_kind:
244 seq = mod->v.Module.body;
245 for (i = 0; i < asdl_seq_LEN(seq); i++)
246 if (!symtable_visit_stmt(st,
247 (stmt_ty)asdl_seq_GET(seq, i)))
248 goto error;
249 break;
250 case Expression_kind:
251 if (!symtable_visit_expr(st, mod->v.Expression.body))
252 goto error;
253 break;
254 case Interactive_kind:
255 seq = mod->v.Interactive.body;
256 for (i = 0; i < asdl_seq_LEN(seq); i++)
257 if (!symtable_visit_stmt(st,
258 (stmt_ty)asdl_seq_GET(seq, i)))
259 goto error;
260 break;
261 case Suite_kind:
262 PyErr_SetString(PyExc_RuntimeError,
263 "this compiler does not handle Suites");
264 goto error;
265 }
266 if (!symtable_exit_block(st, (void *)mod)) {
267 PySymtable_Free(st);
268 return NULL;
269 }
270 /* Make the second symbol analysis pass */
271 if (symtable_analyze(st))
272 return st;
273 PySymtable_Free(st);
274 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 (void) symtable_exit_block(st, (void *)mod);
277 PySymtable_Free(st);
278 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279}
280
281void
282PySymtable_Free(struct symtable *st)
283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 Py_XDECREF(st->st_blocks);
285 Py_XDECREF(st->st_stack);
286 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287}
288
289PySTEntryObject *
290PySymtable_Lookup(struct symtable *st, void *key)
291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 k = PyLong_FromVoidPtr(key);
295 if (k == NULL)
296 return NULL;
297 v = PyDict_GetItem(st->st_blocks, k);
298 if (v) {
299 assert(PySTEntry_Check(v));
300 Py_INCREF(v);
301 }
302 else {
303 PyErr_SetString(PyExc_KeyError,
304 "unknown symbol table entry");
305 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 Py_DECREF(k);
308 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309}
310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312PyST_GetScope(PySTEntryObject *ste, PyObject *name)
313{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
315 if (!v)
316 return 0;
317 assert(PyLong_Check(v));
318 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319}
320
321
322/* Analyze raw symbol information to determine scope of each name.
323
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000324 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000326 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330 explicit global is declared with the global statement. An implicit
331 global is a free variable for which the compiler has found no binding
332 in an enclosing function scope. The implicit global is either a global
333 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
334 to handle these names to implement slightly odd semantics. In such a
335 block, the name is treated as global until it is assigned to; then it
336 is treated as a local.
337
338 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000339 The first pass collects raw facts from the AST via the symtable_visit_*
340 functions: the name is a parameter here, the name is used but not defined
341 here, etc. The second pass analyzes these facts during a pass over the
342 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343
344 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000346 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000347 Names which are explicitly declared nonlocal must exist in this set of
348 visible names - if they do not, a syntax error is raised. After doing
349 the local analysis, it analyzes each of its child blocks using an
350 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351
Nick Coghlan650f0d02007-04-15 12:05:43 +0000352 The children update the free variable set. If a local variable is added to
353 the free variable set by the child, the variable is marked as a cell. The
354 function object being defined must provide runtime storage for the variable
355 that may outlive the function's frame. Cell variables are removed from the
356 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000357
Nick Coghlan650f0d02007-04-15 12:05:43 +0000358 During analysis, the names are:
359 symbols: dict mapping from symbol names to flag values (including offset scope values)
360 scopes: dict mapping from symbol names to scope values (no offset)
361 local: set of all symbol names local to the current scope
362 bound: set of all symbol names local to a containing function scope
363 free: set of all symbol names referenced but not bound in child scopes
364 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365*/
366
367#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 PyObject *o = PyLong_FromLong(I); \
369 if (!o) \
370 return 0; \
371 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
372 Py_DECREF(o); \
373 return 0; \
374 } \
375 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376}
377
378/* Decide on scope of name, given flags.
379
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000380 The namespace dictionaries may be modified to record information
381 about the new name. For example, a new global will add an entry to
382 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383*/
384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000386analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 PyObject *bound, PyObject *local, PyObject *free,
388 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 if (flags & DEF_GLOBAL) {
391 if (flags & DEF_PARAM) {
392 PyErr_Format(PyExc_SyntaxError,
393 "name '%U' is parameter and global",
394 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000395 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
396 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397
398 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 if (flags & DEF_NONLOCAL) {
401 PyErr_Format(PyExc_SyntaxError,
402 "name '%U' is nonlocal and global",
403 name);
404 return 0;
405 }
406 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
407 if (PySet_Add(global, name) < 0)
408 return 0;
409 if (bound && (PySet_Discard(bound, name) < 0))
410 return 0;
411 return 1;
412 }
413 if (flags & DEF_NONLOCAL) {
414 if (flags & DEF_PARAM) {
415 PyErr_Format(PyExc_SyntaxError,
416 "name '%U' is parameter and nonlocal",
417 name);
418 return 0;
419 }
420 if (!bound) {
421 PyErr_Format(PyExc_SyntaxError,
422 "nonlocal declaration not allowed at module level");
423 return 0;
424 }
425 if (!PySet_Contains(bound, name)) {
426 PyErr_Format(PyExc_SyntaxError,
427 "no binding for nonlocal '%U' found",
428 name);
429
430 return 0;
431 }
432 SET_SCOPE(scopes, name, FREE);
433 ste->ste_free = 1;
434 return PySet_Add(free, name) >= 0;
435 }
436 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000437 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (PySet_Add(local, name) < 0)
439 return 0;
440 if (PySet_Discard(global, name) < 0)
441 return 0;
442 return 1;
443 }
444 /* If an enclosing block has a binding for this name, it
445 is a free variable rather than a global variable.
446 Note that having a non-NULL bound implies that the block
447 is nested.
448 */
449 if (bound && PySet_Contains(bound, name)) {
450 SET_SCOPE(scopes, name, FREE);
451 ste->ste_free = 1;
452 return PySet_Add(free, name) >= 0;
453 }
454 /* If a parent has a global statement, then call it global
455 explicit? It could also be global implicit.
456 */
457 if (global && PySet_Contains(global, name)) {
458 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
459 return 1;
460 }
461 if (ste->ste_nested)
462 ste->ste_free = 1;
463 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
464 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465}
466
467#undef SET_SCOPE
468
469/* If a name is defined in free and also in locals, then this block
470 provides the binding for the free variable. The name should be
471 marked CELL in this block and removed from the free list.
472
473 Note that the current block's free variables are included in free.
474 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000475
Martin v. Löwis2673a572007-10-29 19:54:24 +0000476 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000477 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478*/
479
480static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000481analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 PyObject *name, *v, *v_cell;
484 int success = 0;
485 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 v_cell = PyLong_FromLong(CELL);
488 if (!v_cell)
489 return 0;
490 while (PyDict_Next(scopes, &pos, &name, &v)) {
491 long scope;
492 assert(PyLong_Check(v));
493 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000494 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 continue;
496 if (!PySet_Contains(free, name))
497 continue;
498 if (restricted != NULL &&
499 PyUnicode_CompareWithASCIIString(name, restricted))
500 continue;
501 /* Replace LOCAL with CELL for this name, and remove
502 from free. It is safe to replace the value of name
503 in the dict, because it will not cause a resize.
504 */
505 if (PyDict_SetItem(scopes, name, v_cell) < 0)
506 goto error;
507 if (PySet_Discard(free, name) < 0)
508 goto error;
509 }
510 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 Py_DECREF(v_cell);
513 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514}
515
516/* Check for illegal statements in unoptimized namespaces */
517static int
518check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
522 || !(ste->ste_free || ste->ste_child_free))
523 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 trailer = (ste->ste_child_free ?
526 "contains a nested function with free variables" :
527 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 switch (ste->ste_unoptimized) {
530 case OPT_TOPLEVEL: /* import * at top-level is fine */
531 return 1;
532 case OPT_IMPORT_STAR:
533 PyErr_Format(PyExc_SyntaxError,
534 "import * is not allowed in function '%U' because it %s",
535 ste->ste_name, trailer);
536 break;
537 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000539 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
540 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542}
543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544/* Enter the final scope information into the ste_symbols dict.
545 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546 * All arguments are dicts. Modifies symbols, others are read-only.
547*/
548static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000550 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 PyObject *name = NULL, *itr = NULL;
553 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
554 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* Update scope information for all symbols in this scope */
557 while (PyDict_Next(symbols, &pos, &name, &v)) {
558 long scope, flags;
559 assert(PyLong_Check(v));
560 flags = PyLong_AS_LONG(v);
561 v_scope = PyDict_GetItem(scopes, name);
562 assert(v_scope && PyLong_Check(v_scope));
563 scope = PyLong_AS_LONG(v_scope);
564 flags |= (scope << SCOPE_OFFSET);
565 v_new = PyLong_FromLong(flags);
566 if (!v_new)
567 return 0;
568 if (PyDict_SetItem(symbols, name, v_new) < 0) {
569 Py_DECREF(v_new);
570 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 Py_DECREF(v_new);
573 }
574
575 /* Record not yet resolved free variables from children (if any) */
576 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
577 if (!v_free)
578 return 0;
579
580 itr = PyObject_GetIter(free);
581 if (!itr)
582 goto error;
583
584 while ((name = PyIter_Next(itr))) {
585 v = PyDict_GetItem(symbols, name);
586
587 /* Handle symbol that already exists in this scope */
588 if (v) {
589 /* Handle a free variable in a method of
590 the class that has the same name as a local
591 or global in the class scope.
592 */
593 if (classflag &&
594 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
595 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
596 v_new = PyLong_FromLong(flags);
597 if (!v_new) {
598 goto error;
599 }
600 if (PyDict_SetItem(symbols, name, v_new) < 0) {
601 Py_DECREF(v_new);
602 goto error;
603 }
604 Py_DECREF(v_new);
605 }
606 /* It's a cell, or already free in this scope */
607 Py_DECREF(name);
608 continue;
609 }
610 /* Handle global symbol */
611 if (!PySet_Contains(bound, name)) {
612 Py_DECREF(name);
613 continue; /* it's a global */
614 }
615 /* Propagate new free symbol up the lexical stack */
616 if (PyDict_SetItem(symbols, name, v_free) < 0) {
617 goto error;
618 }
619 Py_DECREF(name);
620 }
621 Py_DECREF(itr);
622 Py_DECREF(v_free);
623 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000624error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 Py_XDECREF(v_free);
626 Py_XDECREF(itr);
627 Py_XDECREF(name);
628 return 0;
629}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630
631/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000632
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 Arguments:
634 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000635 bound -- set of variables bound in enclosing scopes (input). bound
636 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 free -- set of free variables in enclosed scopes (output)
638 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000639
640 The implementation uses two mutually recursive functions,
641 analyze_block() and analyze_child_block(). analyze_block() is
642 responsible for analyzing the individual names defined in a block.
643 analyze_child_block() prepares temporary namespace dictionaries
644 used to evaluated nested blocks.
645
646 The two functions exist because a child block should see the name
647 bindings of its enclosing blocks, but those bindings should not
648 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649*/
650
651static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
653 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000654
655static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
657 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
660 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
661 PyObject *temp;
662 int i, success = 0;
663 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 local = PySet_New(NULL); /* collect new names bound in block */
666 if (!local)
667 goto error;
668 scopes = PyDict_New(); /* collect scopes defined for each name */
669 if (!scopes)
670 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 /* Allocate new global and bound variable dictionaries. These
673 dictionaries hold the names visible in nested blocks. For
674 ClassBlocks, the bound and global names are initialized
675 before analyzing names, because class bindings aren't
676 visible in methods. For other blocks, they are initialized
677 after names are analyzed.
678 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 /* TODO(jhylton): Package these dicts in a struct so that we
681 can write reasonable helper functions?
682 */
683 newglobal = PySet_New(NULL);
684 if (!newglobal)
685 goto error;
686 newfree = PySet_New(NULL);
687 if (!newfree)
688 goto error;
689 newbound = PySet_New(NULL);
690 if (!newbound)
691 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 /* Class namespace has no effect on names visible in
694 nested functions, so populate the global and bound
695 sets to be passed to child blocks before analyzing
696 this one.
697 */
698 if (ste->ste_type == ClassBlock) {
699 /* Pass down known globals */
700 temp = PyNumber_InPlaceOr(newglobal, global);
701 if (!temp)
702 goto error;
703 Py_DECREF(temp);
704 /* Pass down previously bound symbols */
705 if (bound) {
706 temp = PyNumber_InPlaceOr(newbound, bound);
707 if (!temp)
708 goto error;
709 Py_DECREF(temp);
710 }
711 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
714 long flags = PyLong_AS_LONG(v);
715 if (!analyze_name(ste, scopes, name, flags,
716 bound, local, free, global))
717 goto error;
718 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 /* Populate global and bound sets to be passed to children. */
721 if (ste->ste_type != ClassBlock) {
722 /* Add function locals to bound set */
723 if (ste->ste_type == FunctionBlock) {
724 temp = PyNumber_InPlaceOr(newbound, local);
725 if (!temp)
726 goto error;
727 Py_DECREF(temp);
728 }
729 /* Pass down previously bound symbols */
730 if (bound) {
731 temp = PyNumber_InPlaceOr(newbound, bound);
732 if (!temp)
733 goto error;
734 Py_DECREF(temp);
735 }
736 /* Pass down known globals */
737 temp = PyNumber_InPlaceOr(newglobal, global);
738 if (!temp)
739 goto error;
740 Py_DECREF(temp);
741 }
742 else {
743 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000744 if (!GET_IDENTIFIER(__class__))
745 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 assert(PySet_Contains(local, __class__) == 1);
747 if (PySet_Add(newbound, __class__) < 0)
748 goto error;
749 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000750
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300751 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 newbound, newglobal now contain the names visible in
754 nested blocks. The free variables in the children will
755 be collected in allfree.
756 */
757 allfree = PySet_New(NULL);
758 if (!allfree)
759 goto error;
760 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
761 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
762 PySTEntryObject* entry;
763 assert(c && PySTEntry_Check(c));
764 entry = (PySTEntryObject*)c;
765 if (!analyze_child_block(entry, newbound, newfree, newglobal,
766 allfree))
767 goto error;
768 /* Check if any children have free variables */
769 if (entry->ste_free || entry->ste_child_free)
770 ste->ste_child_free = 1;
771 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 temp = PyNumber_InPlaceOr(newfree, allfree);
774 if (!temp)
775 goto error;
776 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 /* Check if any local variables must be converted to cell variables */
779 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
780 NULL))
781 goto error;
782 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000783 "__class__"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 goto error;
785 /* Records the results of the analysis in the symbol table entry */
786 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
787 ste->ste_type == ClassBlock))
788 goto error;
789 if (!check_unoptimized(ste))
790 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 temp = PyNumber_InPlaceOr(free, newfree);
793 if (!temp)
794 goto error;
795 Py_DECREF(temp);
796 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 Py_XDECREF(scopes);
799 Py_XDECREF(local);
800 Py_XDECREF(newbound);
801 Py_XDECREF(newglobal);
802 Py_XDECREF(newfree);
803 Py_XDECREF(allfree);
804 if (!success)
805 assert(PyErr_Occurred());
806 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000807}
808
809static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
811 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
814 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 These dictionary are used by all blocks enclosed by the
819 current block. The analyze_block() call modifies these
820 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 */
823 temp_bound = PySet_New(bound);
824 if (!temp_bound)
825 goto error;
826 temp_free = PySet_New(free);
827 if (!temp_free)
828 goto error;
829 temp_global = PySet_New(global);
830 if (!temp_global)
831 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
834 goto error;
835 temp = PyNumber_InPlaceOr(child_free, temp_free);
836 if (!temp)
837 goto error;
838 Py_DECREF(temp);
839 Py_DECREF(temp_bound);
840 Py_DECREF(temp_free);
841 Py_DECREF(temp_global);
842 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000843 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 Py_XDECREF(temp_bound);
845 Py_XDECREF(temp_free);
846 Py_XDECREF(temp_global);
847 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000848}
849
850static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851symtable_analyze(struct symtable *st)
852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 PyObject *free, *global;
854 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 free = PySet_New(NULL);
857 if (!free)
858 return 0;
859 global = PySet_New(NULL);
860 if (!global) {
861 Py_DECREF(free);
862 return 0;
863 }
864 r = analyze_block(st->st_top, NULL, free, global);
865 Py_DECREF(free);
866 Py_DECREF(global);
867 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868}
869
870
871static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000872symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
875 lineno, NULL, NULL) < 0) {
876 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
877 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000878 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
879 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 }
881 return 0;
882 }
883 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000884}
885
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000886/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887 This reference is released when the block is exited, via the DECREF
888 in symtable_exit_block().
889*/
890
891static int
892symtable_exit_block(struct symtable *st, void *ast)
893{
Benjamin Peterson609da582011-06-29 22:52:39 -0500894 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895
Benjamin Peterson609da582011-06-29 22:52:39 -0500896 st->st_cur = NULL;
897 size = PyList_GET_SIZE(st->st_stack);
898 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500899 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500901 if (--size)
902 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 }
904 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905}
906
907static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000909 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910{
Benjamin Peterson609da582011-06-29 22:52:39 -0500911 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912
Benjamin Peterson609da582011-06-29 22:52:39 -0500913 ste = ste_new(st, name, block, ast, lineno, col_offset);
914 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500916 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
917 Py_DECREF(ste);
918 return 0;
919 }
920 prev = st->st_cur;
921 /* The entry is owned by the stack. Borrow it for st_cur. */
922 Py_DECREF(ste);
923 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000924 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 st->st_global = st->st_cur->ste_symbols;
926 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500927 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 return 0;
929 }
930 }
931 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932}
933
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000934static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935symtable_lookup(struct symtable *st, PyObject *name)
936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 PyObject *o;
938 PyObject *mangled = _Py_Mangle(st->st_private, name);
939 if (!mangled)
940 return 0;
941 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
942 Py_DECREF(mangled);
943 if (!o)
944 return 0;
945 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946}
947
948static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 PyObject *o;
952 PyObject *dict;
953 long val;
954 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955
Jeremy Hylton81e95022007-02-27 06:50:52 +0000956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 if (!mangled)
958 return 0;
959 dict = st->st_cur->ste_symbols;
960 if ((o = PyDict_GetItem(dict, mangled))) {
961 val = PyLong_AS_LONG(o);
962 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
963 /* Is it better to use 'mangled' or 'name' here? */
964 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000965 PyErr_SyntaxLocationEx(st->st_filename,
966 st->st_cur->ste_lineno,
967 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 goto error;
969 }
970 val |= flag;
971 } else
972 val = flag;
973 o = PyLong_FromLong(val);
974 if (o == NULL)
975 goto error;
976 if (PyDict_SetItem(dict, mangled, o) < 0) {
977 Py_DECREF(o);
978 goto error;
979 }
980 Py_DECREF(o);
981
982 if (flag & DEF_PARAM) {
983 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
984 goto error;
985 } else if (flag & DEF_GLOBAL) {
986 /* XXX need to update DEF_GLOBAL for other flags too;
987 perhaps only DEF_FREE_GLOBAL */
988 val = flag;
989 if ((o = PyDict_GetItem(st->st_global, mangled))) {
990 val |= PyLong_AS_LONG(o);
991 }
992 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 goto error;
995 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
996 Py_DECREF(o);
997 goto error;
998 }
999 Py_DECREF(o);
1000 }
1001 Py_DECREF(mangled);
1002 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001003
1004error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 Py_DECREF(mangled);
1006 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007}
1008
1009/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1010 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 function.
1012
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1014 useful if the first node in the sequence requires special treatment.
1015*/
1016
1017#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 if (!symtable_visit_ ## TYPE((ST), (V))) \
1019 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001020
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 int i; \
1023 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1024 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1025 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1026 if (!symtable_visit_ ## TYPE((ST), elt)) \
1027 return 0; \
1028 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001030
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 int i; \
1033 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1034 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1035 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1036 if (!symtable_visit_ ## TYPE((ST), elt)) \
1037 return 0; \
1038 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001040
Guido van Rossum4f72a782006-10-27 23:31:49 +00001041#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 int i = 0; \
1043 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1044 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1045 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1046 if (!elt) continue; /* can be NULL */ \
1047 if (!symtable_visit_expr((ST), elt)) \
1048 return 0; \
1049 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001050}
1051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001053symtable_new_tmpname(struct symtable *st)
1054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 char tmpname[256];
1056 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1059 ++st->st_cur->ste_tmpname);
1060 tmp = PyUnicode_InternFromString(tmpname);
1061 if (!tmp)
1062 return 0;
1063 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1064 return 0;
1065 Py_DECREF(tmp);
1066 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001067}
1068
Guido van Rossum4f72a782006-10-27 23:31:49 +00001069
Guido van Rossumc2e20742006-02-27 22:32:47 +00001070static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071symtable_visit_stmt(struct symtable *st, stmt_ty s)
1072{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 switch (s->kind) {
1074 case FunctionDef_kind:
1075 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1076 return 0;
1077 if (s->v.FunctionDef.args->defaults)
1078 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1079 if (s->v.FunctionDef.args->kw_defaults)
1080 VISIT_KWONLYDEFAULTS(st,
1081 s->v.FunctionDef.args->kw_defaults);
1082 if (!symtable_visit_annotations(st, s))
1083 return 0;
1084 if (s->v.FunctionDef.decorator_list)
1085 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1086 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001087 FunctionBlock, (void *)s, s->lineno,
1088 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001090 VISIT(st, arguments, s->v.FunctionDef.args);
1091 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 if (!symtable_exit_block(st, s))
1093 return 0;
1094 break;
1095 case ClassDef_kind: {
1096 PyObject *tmp;
1097 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1098 return 0;
1099 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1100 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1101 if (s->v.ClassDef.starargs)
1102 VISIT(st, expr, s->v.ClassDef.starargs);
1103 if (s->v.ClassDef.kwargs)
1104 VISIT(st, expr, s->v.ClassDef.kwargs);
1105 if (s->v.ClassDef.decorator_list)
1106 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1107 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001108 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 return 0;
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001110 if (!GET_IDENTIFIER(__class__) ||
1111 !symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 !GET_IDENTIFIER(__locals__) ||
1113 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1114 symtable_exit_block(st, s);
1115 return 0;
1116 }
1117 tmp = st->st_private;
1118 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001119 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 st->st_private = tmp;
1121 if (!symtable_exit_block(st, s))
1122 return 0;
1123 break;
1124 }
1125 case Return_kind:
1126 if (s->v.Return.value) {
1127 VISIT(st, expr, s->v.Return.value);
1128 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 }
1130 break;
1131 case Delete_kind:
1132 VISIT_SEQ(st, expr, s->v.Delete.targets);
1133 break;
1134 case Assign_kind:
1135 VISIT_SEQ(st, expr, s->v.Assign.targets);
1136 VISIT(st, expr, s->v.Assign.value);
1137 break;
1138 case AugAssign_kind:
1139 VISIT(st, expr, s->v.AugAssign.target);
1140 VISIT(st, expr, s->v.AugAssign.value);
1141 break;
1142 case For_kind:
1143 VISIT(st, expr, s->v.For.target);
1144 VISIT(st, expr, s->v.For.iter);
1145 VISIT_SEQ(st, stmt, s->v.For.body);
1146 if (s->v.For.orelse)
1147 VISIT_SEQ(st, stmt, s->v.For.orelse);
1148 break;
1149 case While_kind:
1150 VISIT(st, expr, s->v.While.test);
1151 VISIT_SEQ(st, stmt, s->v.While.body);
1152 if (s->v.While.orelse)
1153 VISIT_SEQ(st, stmt, s->v.While.orelse);
1154 break;
1155 case If_kind:
1156 /* XXX if 0: and lookup_yield() hacks */
1157 VISIT(st, expr, s->v.If.test);
1158 VISIT_SEQ(st, stmt, s->v.If.body);
1159 if (s->v.If.orelse)
1160 VISIT_SEQ(st, stmt, s->v.If.orelse);
1161 break;
1162 case Raise_kind:
1163 if (s->v.Raise.exc) {
1164 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001165 if (s->v.Raise.cause) {
1166 VISIT(st, expr, s->v.Raise.cause);
1167 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 }
1169 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001170 case Try_kind:
1171 VISIT_SEQ(st, stmt, s->v.Try.body);
1172 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1173 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1174 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 break;
1176 case Assert_kind:
1177 VISIT(st, expr, s->v.Assert.test);
1178 if (s->v.Assert.msg)
1179 VISIT(st, expr, s->v.Assert.msg);
1180 break;
1181 case Import_kind:
1182 VISIT_SEQ(st, alias, s->v.Import.names);
1183 /* XXX Don't have the lineno available inside
1184 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001185 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001187 st->st_cur->ste_opt_col_offset = s->col_offset;
1188 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 break;
1190 case ImportFrom_kind:
1191 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1192 /* XXX Don't have the lineno available inside
1193 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001194 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001196 st->st_cur->ste_opt_col_offset = s->col_offset;
1197 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 break;
1199 case Global_kind: {
1200 int i;
1201 asdl_seq *seq = s->v.Global.names;
1202 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1203 identifier name = (identifier)asdl_seq_GET(seq, i);
1204 char *c_name = _PyUnicode_AsString(name);
1205 long cur = symtable_lookup(st, name);
1206 if (cur < 0)
1207 return 0;
1208 if (cur & (DEF_LOCAL | USE)) {
1209 char buf[256];
1210 if (cur & DEF_LOCAL)
1211 PyOS_snprintf(buf, sizeof(buf),
1212 GLOBAL_AFTER_ASSIGN,
1213 c_name);
1214 else
1215 PyOS_snprintf(buf, sizeof(buf),
1216 GLOBAL_AFTER_USE,
1217 c_name);
1218 if (!symtable_warn(st, buf, s->lineno))
1219 return 0;
1220 }
1221 if (!symtable_add_def(st, name, DEF_GLOBAL))
1222 return 0;
1223 }
1224 break;
1225 }
1226 case Nonlocal_kind: {
1227 int i;
1228 asdl_seq *seq = s->v.Nonlocal.names;
1229 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1230 identifier name = (identifier)asdl_seq_GET(seq, i);
1231 char *c_name = _PyUnicode_AsString(name);
1232 long cur = symtable_lookup(st, name);
1233 if (cur < 0)
1234 return 0;
1235 if (cur & (DEF_LOCAL | USE)) {
1236 char buf[256];
1237 if (cur & DEF_LOCAL)
1238 PyOS_snprintf(buf, sizeof(buf),
1239 NONLOCAL_AFTER_ASSIGN,
1240 c_name);
1241 else
1242 PyOS_snprintf(buf, sizeof(buf),
1243 NONLOCAL_AFTER_USE,
1244 c_name);
1245 if (!symtable_warn(st, buf, s->lineno))
1246 return 0;
1247 }
1248 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1249 return 0;
1250 }
1251 break;
1252 }
1253 case Expr_kind:
1254 VISIT(st, expr, s->v.Expr.value);
1255 break;
1256 case Pass_kind:
1257 case Break_kind:
1258 case Continue_kind:
1259 /* nothing to do here */
1260 break;
1261 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001262 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 VISIT_SEQ(st, stmt, s->v.With.body);
1264 break;
1265 }
1266 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267}
1268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270symtable_visit_expr(struct symtable *st, expr_ty e)
1271{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 switch (e->kind) {
1273 case BoolOp_kind:
1274 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1275 break;
1276 case BinOp_kind:
1277 VISIT(st, expr, e->v.BinOp.left);
1278 VISIT(st, expr, e->v.BinOp.right);
1279 break;
1280 case UnaryOp_kind:
1281 VISIT(st, expr, e->v.UnaryOp.operand);
1282 break;
1283 case Lambda_kind: {
1284 if (!GET_IDENTIFIER(lambda))
1285 return 0;
1286 if (e->v.Lambda.args->defaults)
1287 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001288 if (e->v.Lambda.args->kw_defaults)
1289 VISIT_KWONLYDEFAULTS(st,
1290 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001292 FunctionBlock, (void *)e, e->lineno,
1293 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001295 VISIT(st, arguments, e->v.Lambda.args);
1296 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 if (!symtable_exit_block(st, (void *)e))
1298 return 0;
1299 break;
1300 }
1301 case IfExp_kind:
1302 VISIT(st, expr, e->v.IfExp.test);
1303 VISIT(st, expr, e->v.IfExp.body);
1304 VISIT(st, expr, e->v.IfExp.orelse);
1305 break;
1306 case Dict_kind:
1307 VISIT_SEQ(st, expr, e->v.Dict.keys);
1308 VISIT_SEQ(st, expr, e->v.Dict.values);
1309 break;
1310 case Set_kind:
1311 VISIT_SEQ(st, expr, e->v.Set.elts);
1312 break;
1313 case GeneratorExp_kind:
1314 if (!symtable_visit_genexp(st, e))
1315 return 0;
1316 break;
1317 case ListComp_kind:
1318 if (!symtable_visit_listcomp(st, e))
1319 return 0;
1320 break;
1321 case SetComp_kind:
1322 if (!symtable_visit_setcomp(st, e))
1323 return 0;
1324 break;
1325 case DictComp_kind:
1326 if (!symtable_visit_dictcomp(st, e))
1327 return 0;
1328 break;
1329 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001330 case YieldFrom_kind: {
1331 expr_ty value;
1332 value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value;
1333 if (value)
1334 VISIT(st, expr, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 break;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001337 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 case Compare_kind:
1339 VISIT(st, expr, e->v.Compare.left);
1340 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1341 break;
1342 case Call_kind:
1343 VISIT(st, expr, e->v.Call.func);
1344 VISIT_SEQ(st, expr, e->v.Call.args);
1345 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1346 if (e->v.Call.starargs)
1347 VISIT(st, expr, e->v.Call.starargs);
1348 if (e->v.Call.kwargs)
1349 VISIT(st, expr, e->v.Call.kwargs);
1350 break;
1351 case Num_kind:
1352 case Str_kind:
1353 case Bytes_kind:
1354 case Ellipsis_kind:
1355 /* Nothing to do here. */
1356 break;
1357 /* The following exprs can be assignment targets. */
1358 case Attribute_kind:
1359 VISIT(st, expr, e->v.Attribute.value);
1360 break;
1361 case Subscript_kind:
1362 VISIT(st, expr, e->v.Subscript.value);
1363 VISIT(st, slice, e->v.Subscript.slice);
1364 break;
1365 case Starred_kind:
1366 VISIT(st, expr, e->v.Starred.value);
1367 break;
1368 case Name_kind:
1369 if (!symtable_add_def(st, e->v.Name.id,
1370 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1371 return 0;
1372 /* Special-case super: it counts as a use of __class__ */
1373 if (e->v.Name.ctx == Load &&
1374 st->st_cur->ste_type == FunctionBlock &&
1375 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001376 if (!GET_IDENTIFIER(__class__) ||
1377 !symtable_add_def(st, __class__, USE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 return 0;
1379 }
1380 break;
1381 /* child nodes of List and Tuple will have expr_context set */
1382 case List_kind:
1383 VISIT_SEQ(st, expr, e->v.List.elts);
1384 break;
1385 case Tuple_kind:
1386 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1387 break;
1388 }
1389 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390}
1391
1392static int
1393symtable_implicit_arg(struct symtable *st, int pos)
1394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1396 if (id == NULL)
1397 return 0;
1398 if (!symtable_add_def(st, id, DEF_PARAM)) {
1399 Py_DECREF(id);
1400 return 0;
1401 }
1402 Py_DECREF(id);
1403 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404}
1405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001407symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001409 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 if (!args)
1412 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 for (i = 0; i < asdl_seq_LEN(args); i++) {
1415 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1416 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1417 return 0;
1418 }
1419
1420 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421}
1422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001424symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 if (!args)
1429 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 for (i = 0; i < asdl_seq_LEN(args); i++) {
1432 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1433 if (arg->annotation)
1434 VISIT(st, expr, arg->annotation);
1435 }
1436
1437 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001438}
1439
Neal Norwitzc1505362006-12-28 06:47:50 +00001440static int
1441symtable_visit_annotations(struct symtable *st, stmt_ty s)
1442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 arguments_ty a = s->v.FunctionDef.args;
1444
1445 if (a->args && !symtable_visit_argannotations(st, a->args))
1446 return 0;
1447 if (a->varargannotation)
1448 VISIT(st, expr, a->varargannotation);
1449 if (a->kwargannotation)
1450 VISIT(st, expr, a->kwargannotation);
1451 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1452 return 0;
1453 if (s->v.FunctionDef.returns)
1454 VISIT(st, expr, s->v.FunctionDef.returns);
1455 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456}
1457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459symtable_visit_arguments(struct symtable *st, arguments_ty a)
1460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 /* skip default arguments inside function block
1462 XXX should ast be different?
1463 */
1464 if (a->args && !symtable_visit_params(st, a->args))
1465 return 0;
1466 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1467 return 0;
1468 if (a->vararg) {
1469 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1470 return 0;
1471 st->st_cur->ste_varargs = 1;
1472 }
1473 if (a->kwarg) {
1474 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1475 return 0;
1476 st->st_cur->ste_varkeywords = 1;
1477 }
1478 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479}
1480
1481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 if (eh->v.ExceptHandler.type)
1486 VISIT(st, expr, eh->v.ExceptHandler.type);
1487 if (eh->v.ExceptHandler.name)
1488 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1489 return 0;
1490 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1491 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492}
1493
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001494static int
1495symtable_visit_withitem(struct symtable *st, withitem_ty item)
1496{
1497 VISIT(st, expr, item->context_expr);
1498 if (item->optional_vars) {
1499 VISIT(st, expr, item->optional_vars);
1500 }
1501 return 1;
1502}
1503
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506symtable_visit_alias(struct symtable *st, alias_ty a)
1507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001509 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 dotted package name (e.g. spam.eggs)
1511 */
1512 PyObject *store_name;
1513 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001514 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1515 PyUnicode_GET_LENGTH(name), 1);
1516 if (dot != -1) {
1517 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 if (!store_name)
1519 return 0;
1520 }
1521 else {
1522 store_name = name;
1523 Py_INCREF(store_name);
1524 }
1525 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1526 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1527 Py_DECREF(store_name);
1528 return r;
1529 }
1530 else {
1531 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001532 int lineno = st->st_cur->ste_lineno;
1533 int col_offset = st->st_cur->ste_col_offset;
1534 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1535 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1536 Py_DECREF(store_name);
1537 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 }
1539 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1540 Py_DECREF(store_name);
1541 return 1;
1542 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543}
1544
1545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 VISIT(st, expr, lc->target);
1550 VISIT(st, expr, lc->iter);
1551 VISIT_SEQ(st, expr, lc->ifs);
1552 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553}
1554
1555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557symtable_visit_keyword(struct symtable *st, keyword_ty k)
1558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 VISIT(st, expr, k->value);
1560 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561}
1562
1563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565symtable_visit_slice(struct symtable *st, slice_ty s)
1566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 switch (s->kind) {
1568 case Slice_kind:
1569 if (s->v.Slice.lower)
1570 VISIT(st, expr, s->v.Slice.lower)
1571 if (s->v.Slice.upper)
1572 VISIT(st, expr, s->v.Slice.upper)
1573 if (s->v.Slice.step)
1574 VISIT(st, expr, s->v.Slice.step)
1575 break;
1576 case ExtSlice_kind:
1577 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1578 break;
1579 case Index_kind:
1580 VISIT(st, expr, s->v.Index.value)
1581 break;
1582 }
1583 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584}
1585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001587symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001588 identifier scope_name, asdl_seq *generators,
1589 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 int is_generator = (e->kind == GeneratorExp_kind);
1592 int needs_tmp = !is_generator;
1593 comprehension_ty outermost = ((comprehension_ty)
1594 asdl_seq_GET(generators, 0));
1595 /* Outermost iterator is evaluated in current scope */
1596 VISIT(st, expr, outermost->iter);
1597 /* Create comprehension scope for the rest */
1598 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001599 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1600 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 return 0;
1602 }
1603 st->st_cur->ste_generator = is_generator;
1604 /* Outermost iter is received as an argument */
1605 if (!symtable_implicit_arg(st, 0)) {
1606 symtable_exit_block(st, (void *)e);
1607 return 0;
1608 }
1609 /* Allocate temporary name if needed */
1610 if (needs_tmp && !symtable_new_tmpname(st)) {
1611 symtable_exit_block(st, (void *)e);
1612 return 0;
1613 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001614 VISIT(st, expr, outermost->target);
1615 VISIT_SEQ(st, expr, outermost->ifs);
1616 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001618 VISIT(st, expr, value);
1619 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001624symtable_visit_genexp(struct symtable *st, expr_ty e)
1625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1627 e->v.GeneratorExp.generators,
1628 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001629}
1630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001632symtable_visit_listcomp(struct symtable *st, expr_ty e)
1633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1635 e->v.ListComp.generators,
1636 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001637}
1638
1639static int
1640symtable_visit_setcomp(struct symtable *st, expr_ty e)
1641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1643 e->v.SetComp.generators,
1644 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001645}
1646
1647static int
1648symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1651 e->v.DictComp.generators,
1652 e->v.DictComp.key,
1653 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001654}