blob: ff440242cd88aa65eb4b3c90ec7ad3b77d9432a9 [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);
Christian Heimes55ad6512012-09-12 17:58:10 +020033 if (ste == NULL) {
34 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020036 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020038 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 ste->ste_name = name;
41 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 ste->ste_symbols = NULL;
44 ste->ste_varnames = NULL;
45 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 ste->ste_symbols = PyDict_New();
48 if (ste->ste_symbols == NULL)
49 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 ste->ste_varnames = PyList_New(0);
52 if (ste->ste_varnames == NULL)
53 goto fail;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 ste->ste_children = PyList_New(0);
56 if (ste->ste_children == NULL)
57 goto fail;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000058
Benjamin Petersond9c87022012-10-31 20:26:20 -040059 ste->ste_directives = NULL;
60
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 ste->ste_type = block;
62 ste->ste_unoptimized = 0;
63 ste->ste_nested = 0;
64 ste->ste_free = 0;
65 ste->ste_varargs = 0;
66 ste->ste_varkeywords = 0;
67 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000068 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000069 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000071 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 if (st->st_cur != NULL &&
74 (st->st_cur->ste_nested ||
75 st->st_cur->ste_type == FunctionBlock))
76 ste->ste_nested = 1;
77 ste->ste_child_free = 0;
78 ste->ste_generator = 0;
79 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
82 goto fail;
83
84 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000085 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 Py_XDECREF(ste);
87 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000088}
89
90static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000092{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
94 ste->ste_name,
95 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096}
97
98static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 ste->ste_table = NULL;
102 Py_XDECREF(ste->ste_id);
103 Py_XDECREF(ste->ste_name);
104 Py_XDECREF(ste->ste_symbols);
105 Py_XDECREF(ste->ste_varnames);
106 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400107 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109}
110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000112
Guido van Rossum6f799372001-09-20 20:46:19 +0000113static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 {"id", T_OBJECT, OFF(ste_id), READONLY},
115 {"name", T_OBJECT, OFF(ste_name), READONLY},
116 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
117 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
118 {"children", T_OBJECT, OFF(ste_children), READONLY},
119 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
120 {"nested", T_INT, OFF(ste_nested), READONLY},
121 {"type", T_INT, OFF(ste_type), READONLY},
122 {"lineno", T_INT, OFF(ste_lineno), READONLY},
123 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124};
125
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000126PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 PyVarObject_HEAD_INIT(&PyType_Type, 0)
128 "symtable entry",
129 sizeof(PySTEntryObject),
130 0,
131 (destructor)ste_dealloc, /* tp_dealloc */
132 0, /* tp_print */
133 0, /* tp_getattr */
134 0, /* tp_setattr */
135 0, /* tp_reserved */
136 (reprfunc)ste_repr, /* tp_repr */
137 0, /* tp_as_number */
138 0, /* tp_as_sequence */
139 0, /* tp_as_mapping */
140 0, /* tp_hash */
141 0, /* tp_call */
142 0, /* tp_str */
143 PyObject_GenericGetAttr, /* tp_getattro */
144 0, /* tp_setattro */
145 0, /* tp_as_buffer */
146 Py_TPFLAGS_DEFAULT, /* tp_flags */
147 0, /* tp_doc */
148 0, /* tp_traverse */
149 0, /* tp_clear */
150 0, /* tp_richcompare */
151 0, /* tp_weaklistoffset */
152 0, /* tp_iter */
153 0, /* tp_iternext */
154 0, /* tp_methods */
155 ste_memberlist, /* tp_members */
156 0, /* tp_getset */
157 0, /* tp_base */
158 0, /* tp_dict */
159 0, /* tp_descr_get */
160 0, /* tp_descr_set */
161 0, /* tp_dictoffset */
162 0, /* tp_init */
163 0, /* tp_alloc */
164 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000165};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000166
167static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000168static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000170 _Py_block_ty block, void *ast, int lineno,
171 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000172static int symtable_exit_block(struct symtable *st, void *ast);
173static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
174static int symtable_visit_expr(struct symtable *st, expr_ty s);
175static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000176static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
177static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000178static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179static int symtable_visit_arguments(struct symtable *st, arguments_ty);
180static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
181static int symtable_visit_alias(struct symtable *st, alias_ty);
182static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
183static int symtable_visit_keyword(struct symtable *st, keyword_ty);
184static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000185static int symtable_visit_params(struct symtable *st, asdl_seq *args);
186static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000188static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500189static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191
Nick Coghlan650f0d02007-04-15 12:05:43 +0000192static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
194 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
199#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000200"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
202static struct symtable *
203symtable_new(void)
204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
208 if (st == NULL)
209 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 st->st_filename = NULL;
212 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if ((st->st_stack = PyList_New(0)) == NULL)
215 goto fail;
216 if ((st->st_blocks = PyDict_New()) == NULL)
217 goto fail;
218 st->st_cur = NULL;
219 st->st_private = NULL;
220 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 PySymtable_Free(st);
223 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224}
225
226struct symtable *
227PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
228{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000229 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 asdl_seq *seq;
231 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 if (st == NULL)
234 return st;
235 st->st_filename = filename;
236 st->st_future = future;
237 /* Make the initial symbol information gathering pass */
238 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000239 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 PySymtable_Free(st);
241 return NULL;
242 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 st->st_top = st->st_cur;
245 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
246 switch (mod->kind) {
247 case Module_kind:
248 seq = mod->v.Module.body;
249 for (i = 0; i < asdl_seq_LEN(seq); i++)
250 if (!symtable_visit_stmt(st,
251 (stmt_ty)asdl_seq_GET(seq, i)))
252 goto error;
253 break;
254 case Expression_kind:
255 if (!symtable_visit_expr(st, mod->v.Expression.body))
256 goto error;
257 break;
258 case Interactive_kind:
259 seq = mod->v.Interactive.body;
260 for (i = 0; i < asdl_seq_LEN(seq); i++)
261 if (!symtable_visit_stmt(st,
262 (stmt_ty)asdl_seq_GET(seq, i)))
263 goto error;
264 break;
265 case Suite_kind:
266 PyErr_SetString(PyExc_RuntimeError,
267 "this compiler does not handle Suites");
268 goto error;
269 }
270 if (!symtable_exit_block(st, (void *)mod)) {
271 PySymtable_Free(st);
272 return NULL;
273 }
274 /* Make the second symbol analysis pass */
275 if (symtable_analyze(st))
276 return st;
277 PySymtable_Free(st);
278 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 (void) symtable_exit_block(st, (void *)mod);
281 PySymtable_Free(st);
282 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283}
284
285void
286PySymtable_Free(struct symtable *st)
287{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 Py_XDECREF(st->st_blocks);
289 Py_XDECREF(st->st_stack);
290 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291}
292
293PySTEntryObject *
294PySymtable_Lookup(struct symtable *st, void *key)
295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 k = PyLong_FromVoidPtr(key);
299 if (k == NULL)
300 return NULL;
301 v = PyDict_GetItem(st->st_blocks, k);
302 if (v) {
303 assert(PySTEntry_Check(v));
304 Py_INCREF(v);
305 }
306 else {
307 PyErr_SetString(PyExc_KeyError,
308 "unknown symbol table entry");
309 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 Py_DECREF(k);
312 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313}
314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000316PyST_GetScope(PySTEntryObject *ste, PyObject *name)
317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
319 if (!v)
320 return 0;
321 assert(PyLong_Check(v));
322 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323}
324
Benjamin Petersond9c87022012-10-31 20:26:20 -0400325static int
326error_at_directive(PySTEntryObject *ste, PyObject *name)
327{
328 Py_ssize_t i;
329 PyObject *data;
330 assert(ste->ste_directives);
331 for (i = 0; ; i++) {
332 data = PyList_GET_ITEM(ste->ste_directives, i);
333 assert(PyTuple_CheckExact(data));
334 if (PyTuple_GET_ITEM(data, 0) == name)
335 break;
336 }
337 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
338 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
339 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
340 return 0;
341}
342
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343
344/* Analyze raw symbol information to determine scope of each name.
345
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000346 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 explicit global is declared with the global statement. An implicit
353 global is a free variable for which the compiler has found no binding
354 in an enclosing function scope. The implicit global is either a global
355 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
356 to handle these names to implement slightly odd semantics. In such a
357 block, the name is treated as global until it is assigned to; then it
358 is treated as a local.
359
360 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000361 The first pass collects raw facts from the AST via the symtable_visit_*
362 functions: the name is a parameter here, the name is used but not defined
363 here, etc. The second pass analyzes these facts during a pass over the
364 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365
366 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000368 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000369 Names which are explicitly declared nonlocal must exist in this set of
370 visible names - if they do not, a syntax error is raised. After doing
371 the local analysis, it analyzes each of its child blocks using an
372 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373
Nick Coghlan650f0d02007-04-15 12:05:43 +0000374 The children update the free variable set. If a local variable is added to
375 the free variable set by the child, the variable is marked as a cell. The
376 function object being defined must provide runtime storage for the variable
377 that may outlive the function's frame. Cell variables are removed from the
378 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000379
Nick Coghlan650f0d02007-04-15 12:05:43 +0000380 During analysis, the names are:
381 symbols: dict mapping from symbol names to flag values (including offset scope values)
382 scopes: dict mapping from symbol names to scope values (no offset)
383 local: set of all symbol names local to the current scope
384 bound: set of all symbol names local to a containing function scope
385 free: set of all symbol names referenced but not bound in child scopes
386 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387*/
388
389#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 PyObject *o = PyLong_FromLong(I); \
391 if (!o) \
392 return 0; \
393 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
394 Py_DECREF(o); \
395 return 0; \
396 } \
397 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398}
399
400/* Decide on scope of name, given flags.
401
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000402 The namespace dictionaries may be modified to record information
403 about the new name. For example, a new global will add an entry to
404 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405*/
406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000408analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 PyObject *bound, PyObject *local, PyObject *free,
410 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 if (flags & DEF_GLOBAL) {
413 if (flags & DEF_PARAM) {
414 PyErr_Format(PyExc_SyntaxError,
415 "name '%U' is parameter and global",
416 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400417 return error_at_directive(ste, name);
Jeremy Hylton81e95022007-02-27 06:50:52 +0000418 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 if (flags & DEF_NONLOCAL) {
420 PyErr_Format(PyExc_SyntaxError,
421 "name '%U' is nonlocal and global",
422 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400423 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 }
425 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
426 if (PySet_Add(global, name) < 0)
427 return 0;
428 if (bound && (PySet_Discard(bound, name) < 0))
429 return 0;
430 return 1;
431 }
432 if (flags & DEF_NONLOCAL) {
433 if (flags & DEF_PARAM) {
434 PyErr_Format(PyExc_SyntaxError,
435 "name '%U' is parameter and nonlocal",
436 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400437 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 }
439 if (!bound) {
440 PyErr_Format(PyExc_SyntaxError,
441 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400442 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 }
444 if (!PySet_Contains(bound, name)) {
445 PyErr_Format(PyExc_SyntaxError,
446 "no binding for nonlocal '%U' found",
447 name);
448
Benjamin Petersond9c87022012-10-31 20:26:20 -0400449 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 }
451 SET_SCOPE(scopes, name, FREE);
452 ste->ste_free = 1;
453 return PySet_Add(free, name) >= 0;
454 }
455 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000456 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (PySet_Add(local, name) < 0)
458 return 0;
459 if (PySet_Discard(global, name) < 0)
460 return 0;
461 return 1;
462 }
463 /* If an enclosing block has a binding for this name, it
464 is a free variable rather than a global variable.
465 Note that having a non-NULL bound implies that the block
466 is nested.
467 */
468 if (bound && PySet_Contains(bound, name)) {
469 SET_SCOPE(scopes, name, FREE);
470 ste->ste_free = 1;
471 return PySet_Add(free, name) >= 0;
472 }
473 /* If a parent has a global statement, then call it global
474 explicit? It could also be global implicit.
475 */
476 if (global && PySet_Contains(global, name)) {
477 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
478 return 1;
479 }
480 if (ste->ste_nested)
481 ste->ste_free = 1;
482 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
483 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484}
485
486#undef SET_SCOPE
487
488/* If a name is defined in free and also in locals, then this block
489 provides the binding for the free variable. The name should be
490 marked CELL in this block and removed from the free list.
491
492 Note that the current block's free variables are included in free.
493 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000494
Martin v. Löwis2673a572007-10-29 19:54:24 +0000495 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000496 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497*/
498
499static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000500analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 PyObject *name, *v, *v_cell;
503 int success = 0;
504 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 v_cell = PyLong_FromLong(CELL);
507 if (!v_cell)
508 return 0;
509 while (PyDict_Next(scopes, &pos, &name, &v)) {
510 long scope;
511 assert(PyLong_Check(v));
512 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000513 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 continue;
515 if (!PySet_Contains(free, name))
516 continue;
517 if (restricted != NULL &&
518 PyUnicode_CompareWithASCIIString(name, restricted))
519 continue;
520 /* Replace LOCAL with CELL for this name, and remove
521 from free. It is safe to replace the value of name
522 in the dict, because it will not cause a resize.
523 */
524 if (PyDict_SetItem(scopes, name, v_cell) < 0)
525 goto error;
526 if (PySet_Discard(free, name) < 0)
527 goto error;
528 }
529 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 Py_DECREF(v_cell);
532 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533}
534
535/* Check for illegal statements in unoptimized namespaces */
536static int
537check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
541 || !(ste->ste_free || ste->ste_child_free))
542 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 trailer = (ste->ste_child_free ?
545 "contains a nested function with free variables" :
546 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 switch (ste->ste_unoptimized) {
549 case OPT_TOPLEVEL: /* import * at top-level is fine */
550 return 1;
551 case OPT_IMPORT_STAR:
552 PyErr_Format(PyExc_SyntaxError,
553 "import * is not allowed in function '%U' because it %s",
554 ste->ste_name, trailer);
555 break;
556 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000558 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
559 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561}
562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563/* Enter the final scope information into the ste_symbols dict.
564 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565 * All arguments are dicts. Modifies symbols, others are read-only.
566*/
567static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000569 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 PyObject *name = NULL, *itr = NULL;
572 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
573 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 /* Update scope information for all symbols in this scope */
576 while (PyDict_Next(symbols, &pos, &name, &v)) {
577 long scope, flags;
578 assert(PyLong_Check(v));
579 flags = PyLong_AS_LONG(v);
580 v_scope = PyDict_GetItem(scopes, name);
581 assert(v_scope && PyLong_Check(v_scope));
582 scope = PyLong_AS_LONG(v_scope);
583 flags |= (scope << SCOPE_OFFSET);
584 v_new = PyLong_FromLong(flags);
585 if (!v_new)
586 return 0;
587 if (PyDict_SetItem(symbols, name, v_new) < 0) {
588 Py_DECREF(v_new);
589 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000590 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 Py_DECREF(v_new);
592 }
593
594 /* Record not yet resolved free variables from children (if any) */
595 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
596 if (!v_free)
597 return 0;
598
599 itr = PyObject_GetIter(free);
600 if (!itr)
601 goto error;
602
603 while ((name = PyIter_Next(itr))) {
604 v = PyDict_GetItem(symbols, name);
605
606 /* Handle symbol that already exists in this scope */
607 if (v) {
608 /* Handle a free variable in a method of
609 the class that has the same name as a local
610 or global in the class scope.
611 */
612 if (classflag &&
613 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
614 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
615 v_new = PyLong_FromLong(flags);
616 if (!v_new) {
617 goto error;
618 }
619 if (PyDict_SetItem(symbols, name, v_new) < 0) {
620 Py_DECREF(v_new);
621 goto error;
622 }
623 Py_DECREF(v_new);
624 }
625 /* It's a cell, or already free in this scope */
626 Py_DECREF(name);
627 continue;
628 }
629 /* Handle global symbol */
630 if (!PySet_Contains(bound, name)) {
631 Py_DECREF(name);
632 continue; /* it's a global */
633 }
634 /* Propagate new free symbol up the lexical stack */
635 if (PyDict_SetItem(symbols, name, v_free) < 0) {
636 goto error;
637 }
638 Py_DECREF(name);
639 }
640 Py_DECREF(itr);
641 Py_DECREF(v_free);
642 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000643error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 Py_XDECREF(v_free);
645 Py_XDECREF(itr);
646 Py_XDECREF(name);
647 return 0;
648}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649
650/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000651
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652 Arguments:
653 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000654 bound -- set of variables bound in enclosing scopes (input). bound
655 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656 free -- set of free variables in enclosed scopes (output)
657 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000658
659 The implementation uses two mutually recursive functions,
660 analyze_block() and analyze_child_block(). analyze_block() is
661 responsible for analyzing the individual names defined in a block.
662 analyze_child_block() prepares temporary namespace dictionaries
663 used to evaluated nested blocks.
664
665 The two functions exist because a child block should see the name
666 bindings of its enclosing blocks, but those bindings should not
667 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668*/
669
670static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
672 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000673
674static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
676 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
679 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
680 PyObject *temp;
681 int i, success = 0;
682 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 local = PySet_New(NULL); /* collect new names bound in block */
685 if (!local)
686 goto error;
687 scopes = PyDict_New(); /* collect scopes defined for each name */
688 if (!scopes)
689 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 /* Allocate new global and bound variable dictionaries. These
692 dictionaries hold the names visible in nested blocks. For
693 ClassBlocks, the bound and global names are initialized
694 before analyzing names, because class bindings aren't
695 visible in methods. For other blocks, they are initialized
696 after names are analyzed.
697 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 /* TODO(jhylton): Package these dicts in a struct so that we
700 can write reasonable helper functions?
701 */
702 newglobal = PySet_New(NULL);
703 if (!newglobal)
704 goto error;
705 newfree = PySet_New(NULL);
706 if (!newfree)
707 goto error;
708 newbound = PySet_New(NULL);
709 if (!newbound)
710 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 /* Class namespace has no effect on names visible in
713 nested functions, so populate the global and bound
714 sets to be passed to child blocks before analyzing
715 this one.
716 */
717 if (ste->ste_type == ClassBlock) {
718 /* Pass down known globals */
719 temp = PyNumber_InPlaceOr(newglobal, global);
720 if (!temp)
721 goto error;
722 Py_DECREF(temp);
723 /* Pass down previously bound symbols */
724 if (bound) {
725 temp = PyNumber_InPlaceOr(newbound, bound);
726 if (!temp)
727 goto error;
728 Py_DECREF(temp);
729 }
730 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
733 long flags = PyLong_AS_LONG(v);
734 if (!analyze_name(ste, scopes, name, flags,
735 bound, local, free, global))
736 goto error;
737 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 /* Populate global and bound sets to be passed to children. */
740 if (ste->ste_type != ClassBlock) {
741 /* Add function locals to bound set */
742 if (ste->ste_type == FunctionBlock) {
743 temp = PyNumber_InPlaceOr(newbound, local);
744 if (!temp)
745 goto error;
746 Py_DECREF(temp);
747 }
748 /* Pass down previously bound symbols */
749 if (bound) {
750 temp = PyNumber_InPlaceOr(newbound, bound);
751 if (!temp)
752 goto error;
753 Py_DECREF(temp);
754 }
755 /* Pass down known globals */
756 temp = PyNumber_InPlaceOr(newglobal, global);
757 if (!temp)
758 goto error;
759 Py_DECREF(temp);
760 }
761 else {
762 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000763 if (!GET_IDENTIFIER(__class__))
764 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 assert(PySet_Contains(local, __class__) == 1);
766 if (PySet_Add(newbound, __class__) < 0)
767 goto error;
768 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300770 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 newbound, newglobal now contain the names visible in
773 nested blocks. The free variables in the children will
774 be collected in allfree.
775 */
776 allfree = PySet_New(NULL);
777 if (!allfree)
778 goto error;
779 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
780 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
781 PySTEntryObject* entry;
782 assert(c && PySTEntry_Check(c));
783 entry = (PySTEntryObject*)c;
784 if (!analyze_child_block(entry, newbound, newfree, newglobal,
785 allfree))
786 goto error;
787 /* Check if any children have free variables */
788 if (entry->ste_free || entry->ste_child_free)
789 ste->ste_child_free = 1;
790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 temp = PyNumber_InPlaceOr(newfree, allfree);
793 if (!temp)
794 goto error;
795 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 /* Check if any local variables must be converted to cell variables */
798 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
799 NULL))
800 goto error;
801 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000802 "__class__"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 goto error;
804 /* Records the results of the analysis in the symbol table entry */
805 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
806 ste->ste_type == ClassBlock))
807 goto error;
808 if (!check_unoptimized(ste))
809 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 temp = PyNumber_InPlaceOr(free, newfree);
812 if (!temp)
813 goto error;
814 Py_DECREF(temp);
815 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 Py_XDECREF(scopes);
818 Py_XDECREF(local);
819 Py_XDECREF(newbound);
820 Py_XDECREF(newglobal);
821 Py_XDECREF(newfree);
822 Py_XDECREF(allfree);
823 if (!success)
824 assert(PyErr_Occurred());
825 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826}
827
828static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
830 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
833 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 These dictionary are used by all blocks enclosed by the
838 current block. The analyze_block() call modifies these
839 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 */
842 temp_bound = PySet_New(bound);
843 if (!temp_bound)
844 goto error;
845 temp_free = PySet_New(free);
846 if (!temp_free)
847 goto error;
848 temp_global = PySet_New(global);
849 if (!temp_global)
850 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
853 goto error;
854 temp = PyNumber_InPlaceOr(child_free, temp_free);
855 if (!temp)
856 goto error;
857 Py_DECREF(temp);
858 Py_DECREF(temp_bound);
859 Py_DECREF(temp_free);
860 Py_DECREF(temp_global);
861 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000862 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 Py_XDECREF(temp_bound);
864 Py_XDECREF(temp_free);
865 Py_XDECREF(temp_global);
866 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000867}
868
869static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870symtable_analyze(struct symtable *st)
871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 PyObject *free, *global;
873 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 free = PySet_New(NULL);
876 if (!free)
877 return 0;
878 global = PySet_New(NULL);
879 if (!global) {
880 Py_DECREF(free);
881 return 0;
882 }
883 r = analyze_block(st->st_top, NULL, free, global);
884 Py_DECREF(free);
885 Py_DECREF(global);
886 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000887}
888
889
890static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000891symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
894 lineno, NULL, NULL) < 0) {
895 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
896 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000897 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
898 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 }
900 return 0;
901 }
902 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903}
904
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000905/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 This reference is released when the block is exited, via the DECREF
907 in symtable_exit_block().
908*/
909
910static int
911symtable_exit_block(struct symtable *st, void *ast)
912{
Benjamin Peterson609da582011-06-29 22:52:39 -0500913 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914
Benjamin Peterson609da582011-06-29 22:52:39 -0500915 st->st_cur = NULL;
916 size = PyList_GET_SIZE(st->st_stack);
917 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500918 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500920 if (--size)
921 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 }
923 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924}
925
926static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000928 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929{
Benjamin Peterson609da582011-06-29 22:52:39 -0500930 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931
Benjamin Peterson609da582011-06-29 22:52:39 -0500932 ste = ste_new(st, name, block, ast, lineno, col_offset);
933 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500935 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
936 Py_DECREF(ste);
937 return 0;
938 }
939 prev = st->st_cur;
940 /* The entry is owned by the stack. Borrow it for st_cur. */
941 Py_DECREF(ste);
942 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000943 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000944 st->st_global = st->st_cur->ste_symbols;
945 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500946 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 return 0;
948 }
949 }
950 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951}
952
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000953static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954symtable_lookup(struct symtable *st, PyObject *name)
955{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 PyObject *o;
957 PyObject *mangled = _Py_Mangle(st->st_private, name);
958 if (!mangled)
959 return 0;
960 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
961 Py_DECREF(mangled);
962 if (!o)
963 return 0;
964 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965}
966
967static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 PyObject *o;
971 PyObject *dict;
972 long val;
973 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974
Jeremy Hylton81e95022007-02-27 06:50:52 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 if (!mangled)
977 return 0;
978 dict = st->st_cur->ste_symbols;
979 if ((o = PyDict_GetItem(dict, mangled))) {
980 val = PyLong_AS_LONG(o);
981 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
982 /* Is it better to use 'mangled' or 'name' here? */
983 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000984 PyErr_SyntaxLocationEx(st->st_filename,
985 st->st_cur->ste_lineno,
986 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 goto error;
988 }
989 val |= flag;
990 } else
991 val = flag;
992 o = PyLong_FromLong(val);
993 if (o == NULL)
994 goto error;
995 if (PyDict_SetItem(dict, mangled, o) < 0) {
996 Py_DECREF(o);
997 goto error;
998 }
999 Py_DECREF(o);
1000
1001 if (flag & DEF_PARAM) {
1002 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1003 goto error;
1004 } else if (flag & DEF_GLOBAL) {
1005 /* XXX need to update DEF_GLOBAL for other flags too;
1006 perhaps only DEF_FREE_GLOBAL */
1007 val = flag;
1008 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1009 val |= PyLong_AS_LONG(o);
1010 }
1011 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 goto error;
1014 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1015 Py_DECREF(o);
1016 goto error;
1017 }
1018 Py_DECREF(o);
1019 }
1020 Py_DECREF(mangled);
1021 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001022
1023error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 Py_DECREF(mangled);
1025 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026}
1027
1028/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1029 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 function.
1031
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1033 useful if the first node in the sequence requires special treatment.
1034*/
1035
1036#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 if (!symtable_visit_ ## TYPE((ST), (V))) \
1038 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001039
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 int i; \
1042 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1043 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1044 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1045 if (!symtable_visit_ ## TYPE((ST), elt)) \
1046 return 0; \
1047 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001049
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 int i; \
1052 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1053 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1054 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1055 if (!symtable_visit_ ## TYPE((ST), elt)) \
1056 return 0; \
1057 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001059
Guido van Rossum4f72a782006-10-27 23:31:49 +00001060#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 int i = 0; \
1062 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1063 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1064 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1065 if (!elt) continue; /* can be NULL */ \
1066 if (!symtable_visit_expr((ST), elt)) \
1067 return 0; \
1068 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001069}
1070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001072symtable_new_tmpname(struct symtable *st)
1073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 char tmpname[256];
1075 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1078 ++st->st_cur->ste_tmpname);
1079 tmp = PyUnicode_InternFromString(tmpname);
1080 if (!tmp)
1081 return 0;
1082 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1083 return 0;
1084 Py_DECREF(tmp);
1085 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001086}
1087
Guido van Rossum4f72a782006-10-27 23:31:49 +00001088
Guido van Rossumc2e20742006-02-27 22:32:47 +00001089static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001090symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1091{
1092 PyObject *data;
1093 int res;
1094 if (!st->st_cur->ste_directives) {
1095 st->st_cur->ste_directives = PyList_New(0);
1096 if (!st->st_cur->ste_directives)
1097 return 0;
1098 }
1099 data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset);
1100 if (!data)
1101 return 0;
1102 res = PyList_Append(st->st_cur->ste_directives, data);
1103 Py_DECREF(data);
1104 return res == 0;
1105}
1106
1107
1108static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109symtable_visit_stmt(struct symtable *st, stmt_ty s)
1110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 switch (s->kind) {
1112 case FunctionDef_kind:
1113 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1114 return 0;
1115 if (s->v.FunctionDef.args->defaults)
1116 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1117 if (s->v.FunctionDef.args->kw_defaults)
1118 VISIT_KWONLYDEFAULTS(st,
1119 s->v.FunctionDef.args->kw_defaults);
1120 if (!symtable_visit_annotations(st, s))
1121 return 0;
1122 if (s->v.FunctionDef.decorator_list)
1123 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1124 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001125 FunctionBlock, (void *)s, s->lineno,
1126 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001128 VISIT(st, arguments, s->v.FunctionDef.args);
1129 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (!symtable_exit_block(st, s))
1131 return 0;
1132 break;
1133 case ClassDef_kind: {
1134 PyObject *tmp;
1135 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1136 return 0;
1137 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1138 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1139 if (s->v.ClassDef.starargs)
1140 VISIT(st, expr, s->v.ClassDef.starargs);
1141 if (s->v.ClassDef.kwargs)
1142 VISIT(st, expr, s->v.ClassDef.kwargs);
1143 if (s->v.ClassDef.decorator_list)
1144 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1145 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001146 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 return 0;
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001148 if (!GET_IDENTIFIER(__class__) ||
1149 !symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 !GET_IDENTIFIER(__locals__) ||
1151 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1152 symtable_exit_block(st, s);
1153 return 0;
1154 }
1155 tmp = st->st_private;
1156 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001157 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 st->st_private = tmp;
1159 if (!symtable_exit_block(st, s))
1160 return 0;
1161 break;
1162 }
1163 case Return_kind:
1164 if (s->v.Return.value) {
1165 VISIT(st, expr, s->v.Return.value);
1166 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 }
1168 break;
1169 case Delete_kind:
1170 VISIT_SEQ(st, expr, s->v.Delete.targets);
1171 break;
1172 case Assign_kind:
1173 VISIT_SEQ(st, expr, s->v.Assign.targets);
1174 VISIT(st, expr, s->v.Assign.value);
1175 break;
1176 case AugAssign_kind:
1177 VISIT(st, expr, s->v.AugAssign.target);
1178 VISIT(st, expr, s->v.AugAssign.value);
1179 break;
1180 case For_kind:
1181 VISIT(st, expr, s->v.For.target);
1182 VISIT(st, expr, s->v.For.iter);
1183 VISIT_SEQ(st, stmt, s->v.For.body);
1184 if (s->v.For.orelse)
1185 VISIT_SEQ(st, stmt, s->v.For.orelse);
1186 break;
1187 case While_kind:
1188 VISIT(st, expr, s->v.While.test);
1189 VISIT_SEQ(st, stmt, s->v.While.body);
1190 if (s->v.While.orelse)
1191 VISIT_SEQ(st, stmt, s->v.While.orelse);
1192 break;
1193 case If_kind:
1194 /* XXX if 0: and lookup_yield() hacks */
1195 VISIT(st, expr, s->v.If.test);
1196 VISIT_SEQ(st, stmt, s->v.If.body);
1197 if (s->v.If.orelse)
1198 VISIT_SEQ(st, stmt, s->v.If.orelse);
1199 break;
1200 case Raise_kind:
1201 if (s->v.Raise.exc) {
1202 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001203 if (s->v.Raise.cause) {
1204 VISIT(st, expr, s->v.Raise.cause);
1205 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 }
1207 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001208 case Try_kind:
1209 VISIT_SEQ(st, stmt, s->v.Try.body);
1210 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1211 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1212 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 break;
1214 case Assert_kind:
1215 VISIT(st, expr, s->v.Assert.test);
1216 if (s->v.Assert.msg)
1217 VISIT(st, expr, s->v.Assert.msg);
1218 break;
1219 case Import_kind:
1220 VISIT_SEQ(st, alias, s->v.Import.names);
1221 /* XXX Don't have the lineno available inside
1222 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001223 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001225 st->st_cur->ste_opt_col_offset = s->col_offset;
1226 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 break;
1228 case ImportFrom_kind:
1229 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1230 /* XXX Don't have the lineno available inside
1231 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001232 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001234 st->st_cur->ste_opt_col_offset = s->col_offset;
1235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 break;
1237 case Global_kind: {
1238 int i;
1239 asdl_seq *seq = s->v.Global.names;
1240 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1241 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 long cur = symtable_lookup(st, name);
1243 if (cur < 0)
1244 return 0;
1245 if (cur & (DEF_LOCAL | USE)) {
1246 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001247 char *c_name = _PyUnicode_AsString(name);
1248 if (!c_name)
1249 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (cur & DEF_LOCAL)
1251 PyOS_snprintf(buf, sizeof(buf),
1252 GLOBAL_AFTER_ASSIGN,
1253 c_name);
1254 else
1255 PyOS_snprintf(buf, sizeof(buf),
1256 GLOBAL_AFTER_USE,
1257 c_name);
1258 if (!symtable_warn(st, buf, s->lineno))
1259 return 0;
1260 }
1261 if (!symtable_add_def(st, name, DEF_GLOBAL))
1262 return 0;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001263 if (!symtable_record_directive(st, name, s))
1264 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 }
1266 break;
1267 }
1268 case Nonlocal_kind: {
1269 int i;
1270 asdl_seq *seq = s->v.Nonlocal.names;
1271 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1272 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 long cur = symtable_lookup(st, name);
1274 if (cur < 0)
1275 return 0;
1276 if (cur & (DEF_LOCAL | USE)) {
1277 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001278 char *c_name = _PyUnicode_AsString(name);
1279 if (!c_name)
1280 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (cur & DEF_LOCAL)
1282 PyOS_snprintf(buf, sizeof(buf),
1283 NONLOCAL_AFTER_ASSIGN,
1284 c_name);
1285 else
1286 PyOS_snprintf(buf, sizeof(buf),
1287 NONLOCAL_AFTER_USE,
1288 c_name);
1289 if (!symtable_warn(st, buf, s->lineno))
1290 return 0;
1291 }
1292 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1293 return 0;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001294 if (!symtable_record_directive(st, name, s))
1295 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 }
1297 break;
1298 }
1299 case Expr_kind:
1300 VISIT(st, expr, s->v.Expr.value);
1301 break;
1302 case Pass_kind:
1303 case Break_kind:
1304 case Continue_kind:
1305 /* nothing to do here */
1306 break;
1307 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001308 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 VISIT_SEQ(st, stmt, s->v.With.body);
1310 break;
1311 }
1312 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313}
1314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316symtable_visit_expr(struct symtable *st, expr_ty e)
1317{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 switch (e->kind) {
1319 case BoolOp_kind:
1320 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1321 break;
1322 case BinOp_kind:
1323 VISIT(st, expr, e->v.BinOp.left);
1324 VISIT(st, expr, e->v.BinOp.right);
1325 break;
1326 case UnaryOp_kind:
1327 VISIT(st, expr, e->v.UnaryOp.operand);
1328 break;
1329 case Lambda_kind: {
1330 if (!GET_IDENTIFIER(lambda))
1331 return 0;
1332 if (e->v.Lambda.args->defaults)
1333 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001334 if (e->v.Lambda.args->kw_defaults)
1335 VISIT_KWONLYDEFAULTS(st,
1336 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001338 FunctionBlock, (void *)e, e->lineno,
1339 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001341 VISIT(st, arguments, e->v.Lambda.args);
1342 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (!symtable_exit_block(st, (void *)e))
1344 return 0;
1345 break;
1346 }
1347 case IfExp_kind:
1348 VISIT(st, expr, e->v.IfExp.test);
1349 VISIT(st, expr, e->v.IfExp.body);
1350 VISIT(st, expr, e->v.IfExp.orelse);
1351 break;
1352 case Dict_kind:
1353 VISIT_SEQ(st, expr, e->v.Dict.keys);
1354 VISIT_SEQ(st, expr, e->v.Dict.values);
1355 break;
1356 case Set_kind:
1357 VISIT_SEQ(st, expr, e->v.Set.elts);
1358 break;
1359 case GeneratorExp_kind:
1360 if (!symtable_visit_genexp(st, e))
1361 return 0;
1362 break;
1363 case ListComp_kind:
1364 if (!symtable_visit_listcomp(st, e))
1365 return 0;
1366 break;
1367 case SetComp_kind:
1368 if (!symtable_visit_setcomp(st, e))
1369 return 0;
1370 break;
1371 case DictComp_kind:
1372 if (!symtable_visit_dictcomp(st, e))
1373 return 0;
1374 break;
1375 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001376 case YieldFrom_kind: {
1377 expr_ty value;
1378 value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value;
1379 if (value)
1380 VISIT(st, expr, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 break;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001383 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 case Compare_kind:
1385 VISIT(st, expr, e->v.Compare.left);
1386 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1387 break;
1388 case Call_kind:
1389 VISIT(st, expr, e->v.Call.func);
1390 VISIT_SEQ(st, expr, e->v.Call.args);
1391 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1392 if (e->v.Call.starargs)
1393 VISIT(st, expr, e->v.Call.starargs);
1394 if (e->v.Call.kwargs)
1395 VISIT(st, expr, e->v.Call.kwargs);
1396 break;
1397 case Num_kind:
1398 case Str_kind:
1399 case Bytes_kind:
1400 case Ellipsis_kind:
1401 /* Nothing to do here. */
1402 break;
1403 /* The following exprs can be assignment targets. */
1404 case Attribute_kind:
1405 VISIT(st, expr, e->v.Attribute.value);
1406 break;
1407 case Subscript_kind:
1408 VISIT(st, expr, e->v.Subscript.value);
1409 VISIT(st, slice, e->v.Subscript.slice);
1410 break;
1411 case Starred_kind:
1412 VISIT(st, expr, e->v.Starred.value);
1413 break;
1414 case Name_kind:
1415 if (!symtable_add_def(st, e->v.Name.id,
1416 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1417 return 0;
1418 /* Special-case super: it counts as a use of __class__ */
1419 if (e->v.Name.ctx == Load &&
1420 st->st_cur->ste_type == FunctionBlock &&
1421 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001422 if (!GET_IDENTIFIER(__class__) ||
1423 !symtable_add_def(st, __class__, USE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 return 0;
1425 }
1426 break;
1427 /* child nodes of List and Tuple will have expr_context set */
1428 case List_kind:
1429 VISIT_SEQ(st, expr, e->v.List.elts);
1430 break;
1431 case Tuple_kind:
1432 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1433 break;
1434 }
1435 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436}
1437
1438static int
1439symtable_implicit_arg(struct symtable *st, int pos)
1440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1442 if (id == NULL)
1443 return 0;
1444 if (!symtable_add_def(st, id, DEF_PARAM)) {
1445 Py_DECREF(id);
1446 return 0;
1447 }
1448 Py_DECREF(id);
1449 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450}
1451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001453symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 if (!args)
1458 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 for (i = 0; i < asdl_seq_LEN(args); i++) {
1461 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1462 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1463 return 0;
1464 }
1465
1466 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467}
1468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001470symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 if (!args)
1475 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 for (i = 0; i < asdl_seq_LEN(args); i++) {
1478 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1479 if (arg->annotation)
1480 VISIT(st, expr, arg->annotation);
1481 }
1482
1483 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001484}
1485
Neal Norwitzc1505362006-12-28 06:47:50 +00001486static int
1487symtable_visit_annotations(struct symtable *st, stmt_ty s)
1488{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 arguments_ty a = s->v.FunctionDef.args;
1490
1491 if (a->args && !symtable_visit_argannotations(st, a->args))
1492 return 0;
1493 if (a->varargannotation)
1494 VISIT(st, expr, a->varargannotation);
1495 if (a->kwargannotation)
1496 VISIT(st, expr, a->kwargannotation);
1497 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1498 return 0;
1499 if (s->v.FunctionDef.returns)
1500 VISIT(st, expr, s->v.FunctionDef.returns);
1501 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502}
1503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505symtable_visit_arguments(struct symtable *st, arguments_ty a)
1506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 /* skip default arguments inside function block
1508 XXX should ast be different?
1509 */
1510 if (a->args && !symtable_visit_params(st, a->args))
1511 return 0;
1512 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1513 return 0;
1514 if (a->vararg) {
1515 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1516 return 0;
1517 st->st_cur->ste_varargs = 1;
1518 }
1519 if (a->kwarg) {
1520 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1521 return 0;
1522 st->st_cur->ste_varkeywords = 1;
1523 }
1524 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
1526
1527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 if (eh->v.ExceptHandler.type)
1532 VISIT(st, expr, eh->v.ExceptHandler.type);
1533 if (eh->v.ExceptHandler.name)
1534 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1535 return 0;
1536 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1537 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538}
1539
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001540static int
1541symtable_visit_withitem(struct symtable *st, withitem_ty item)
1542{
1543 VISIT(st, expr, item->context_expr);
1544 if (item->optional_vars) {
1545 VISIT(st, expr, item->optional_vars);
1546 }
1547 return 1;
1548}
1549
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552symtable_visit_alias(struct symtable *st, alias_ty a)
1553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001555 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 dotted package name (e.g. spam.eggs)
1557 */
1558 PyObject *store_name;
1559 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001560 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1561 PyUnicode_GET_LENGTH(name), 1);
1562 if (dot != -1) {
1563 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (!store_name)
1565 return 0;
1566 }
1567 else {
1568 store_name = name;
1569 Py_INCREF(store_name);
1570 }
1571 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1572 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1573 Py_DECREF(store_name);
1574 return r;
1575 }
1576 else {
1577 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001578 int lineno = st->st_cur->ste_lineno;
1579 int col_offset = st->st_cur->ste_col_offset;
1580 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1581 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1582 Py_DECREF(store_name);
1583 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 }
1585 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1586 Py_DECREF(store_name);
1587 return 1;
1588 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589}
1590
1591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 VISIT(st, expr, lc->target);
1596 VISIT(st, expr, lc->iter);
1597 VISIT_SEQ(st, expr, lc->ifs);
1598 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599}
1600
1601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603symtable_visit_keyword(struct symtable *st, keyword_ty k)
1604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 VISIT(st, expr, k->value);
1606 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607}
1608
1609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611symtable_visit_slice(struct symtable *st, slice_ty s)
1612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 switch (s->kind) {
1614 case Slice_kind:
1615 if (s->v.Slice.lower)
1616 VISIT(st, expr, s->v.Slice.lower)
1617 if (s->v.Slice.upper)
1618 VISIT(st, expr, s->v.Slice.upper)
1619 if (s->v.Slice.step)
1620 VISIT(st, expr, s->v.Slice.step)
1621 break;
1622 case ExtSlice_kind:
1623 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1624 break;
1625 case Index_kind:
1626 VISIT(st, expr, s->v.Index.value)
1627 break;
1628 }
1629 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630}
1631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001633symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001634 identifier scope_name, asdl_seq *generators,
1635 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 int is_generator = (e->kind == GeneratorExp_kind);
1638 int needs_tmp = !is_generator;
1639 comprehension_ty outermost = ((comprehension_ty)
1640 asdl_seq_GET(generators, 0));
1641 /* Outermost iterator is evaluated in current scope */
1642 VISIT(st, expr, outermost->iter);
1643 /* Create comprehension scope for the rest */
1644 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001645 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1646 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 return 0;
1648 }
1649 st->st_cur->ste_generator = is_generator;
1650 /* Outermost iter is received as an argument */
1651 if (!symtable_implicit_arg(st, 0)) {
1652 symtable_exit_block(st, (void *)e);
1653 return 0;
1654 }
1655 /* Allocate temporary name if needed */
1656 if (needs_tmp && !symtable_new_tmpname(st)) {
1657 symtable_exit_block(st, (void *)e);
1658 return 0;
1659 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001660 VISIT(st, expr, outermost->target);
1661 VISIT_SEQ(st, expr, outermost->ifs);
1662 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001664 VISIT(st, expr, value);
1665 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001670symtable_visit_genexp(struct symtable *st, expr_ty e)
1671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1673 e->v.GeneratorExp.generators,
1674 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001675}
1676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001678symtable_visit_listcomp(struct symtable *st, expr_ty e)
1679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1681 e->v.ListComp.generators,
1682 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001683}
1684
1685static int
1686symtable_visit_setcomp(struct symtable *st, expr_ty e)
1687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1689 e->v.SetComp.generators,
1690 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001691}
1692
1693static int
1694symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1697 e->v.DictComp.generators,
1698 e->v.DictComp.key,
1699 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001700}