blob: ff6e8b79b99017824e43a747b7075d1b1d9d2762 [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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 ste->ste_type = block;
60 ste->ste_unoptimized = 0;
61 ste->ste_nested = 0;
62 ste->ste_free = 0;
63 ste->ste_varargs = 0;
64 ste->ste_varkeywords = 0;
65 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000066 ste->ste_opt_col_offset = 0;
Benjamin Petersonb8ffb602010-10-20 21:25:23 +000067 ste->ste_tmpname = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000069 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (st->st_cur != NULL &&
72 (st->st_cur->ste_nested ||
73 st->st_cur->ste_type == FunctionBlock))
74 ste->ste_nested = 1;
75 ste->ste_child_free = 0;
76 ste->ste_generator = 0;
77 ste->ste_returns_value = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
80 goto fail;
81
82 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 Py_XDECREF(ste);
85 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000086}
87
88static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
92 ste->ste_name,
93 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000094}
95
96static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 ste->ste_table = NULL;
100 Py_XDECREF(ste->ste_id);
101 Py_XDECREF(ste->ste_name);
102 Py_XDECREF(ste->ste_symbols);
103 Py_XDECREF(ste->ste_varnames);
104 Py_XDECREF(ste->ste_children);
105 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000106}
107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109
Guido van Rossum6f799372001-09-20 20:46:19 +0000110static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 {"id", T_OBJECT, OFF(ste_id), READONLY},
112 {"name", T_OBJECT, OFF(ste_name), READONLY},
113 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
114 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
115 {"children", T_OBJECT, OFF(ste_children), READONLY},
116 {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
117 {"nested", T_INT, OFF(ste_nested), READONLY},
118 {"type", T_INT, OFF(ste_type), READONLY},
119 {"lineno", T_INT, OFF(ste_lineno), READONLY},
120 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121};
122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 PyVarObject_HEAD_INIT(&PyType_Type, 0)
125 "symtable entry",
126 sizeof(PySTEntryObject),
127 0,
128 (destructor)ste_dealloc, /* tp_dealloc */
129 0, /* tp_print */
130 0, /* tp_getattr */
131 0, /* tp_setattr */
132 0, /* tp_reserved */
133 (reprfunc)ste_repr, /* tp_repr */
134 0, /* tp_as_number */
135 0, /* tp_as_sequence */
136 0, /* tp_as_mapping */
137 0, /* tp_hash */
138 0, /* tp_call */
139 0, /* tp_str */
140 PyObject_GenericGetAttr, /* tp_getattro */
141 0, /* tp_setattro */
142 0, /* tp_as_buffer */
143 Py_TPFLAGS_DEFAULT, /* tp_flags */
144 0, /* tp_doc */
145 0, /* tp_traverse */
146 0, /* tp_clear */
147 0, /* tp_richcompare */
148 0, /* tp_weaklistoffset */
149 0, /* tp_iter */
150 0, /* tp_iternext */
151 0, /* tp_methods */
152 ste_memberlist, /* tp_members */
153 0, /* tp_getset */
154 0, /* tp_base */
155 0, /* tp_dict */
156 0, /* tp_descr_get */
157 0, /* tp_descr_set */
158 0, /* tp_dictoffset */
159 0, /* tp_init */
160 0, /* tp_alloc */
161 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000162};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163
164static int symtable_analyze(struct symtable *st);
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000165static int symtable_warn(struct symtable *st, char *msg, int lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000167 _Py_block_ty block, void *ast, int lineno,
168 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000169static int symtable_exit_block(struct symtable *st, void *ast);
170static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
171static int symtable_visit_expr(struct symtable *st, expr_ty s);
172static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000173static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
174static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000175static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000176static int symtable_visit_arguments(struct symtable *st, arguments_ty);
177static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
178static int symtable_visit_alias(struct symtable *st, alias_ty);
179static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
180static int symtable_visit_keyword(struct symtable *st, keyword_ty);
181static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000182static int symtable_visit_params(struct symtable *st, asdl_seq *args);
183static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184static int symtable_implicit_arg(struct symtable *st, int pos);
Neal Norwitzc1505362006-12-28 06:47:50 +0000185static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500186static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
188
Nick Coghlan650f0d02007-04-15 12:05:43 +0000189static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
191 __class__ = NULL, __locals__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
193#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
196#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000197"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198
199static struct symtable *
200symtable_new(void)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
205 if (st == NULL)
206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 st->st_filename = NULL;
209 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if ((st->st_stack = PyList_New(0)) == NULL)
212 goto fail;
213 if ((st->st_blocks = PyDict_New()) == NULL)
214 goto fail;
215 st->st_cur = NULL;
216 st->st_private = NULL;
217 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 PySymtable_Free(st);
220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221}
222
223struct symtable *
224PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
225{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000226 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 asdl_seq *seq;
228 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 if (st == NULL)
231 return st;
232 st->st_filename = filename;
233 st->st_future = future;
234 /* Make the initial symbol information gathering pass */
235 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000236 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 PySymtable_Free(st);
238 return NULL;
239 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 st->st_top = st->st_cur;
242 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
243 switch (mod->kind) {
244 case Module_kind:
245 seq = mod->v.Module.body;
246 for (i = 0; i < asdl_seq_LEN(seq); i++)
247 if (!symtable_visit_stmt(st,
248 (stmt_ty)asdl_seq_GET(seq, i)))
249 goto error;
250 break;
251 case Expression_kind:
252 if (!symtable_visit_expr(st, mod->v.Expression.body))
253 goto error;
254 break;
255 case Interactive_kind:
256 seq = mod->v.Interactive.body;
257 for (i = 0; i < asdl_seq_LEN(seq); i++)
258 if (!symtable_visit_stmt(st,
259 (stmt_ty)asdl_seq_GET(seq, i)))
260 goto error;
261 break;
262 case Suite_kind:
263 PyErr_SetString(PyExc_RuntimeError,
264 "this compiler does not handle Suites");
265 goto error;
266 }
267 if (!symtable_exit_block(st, (void *)mod)) {
268 PySymtable_Free(st);
269 return NULL;
270 }
271 /* Make the second symbol analysis pass */
272 if (symtable_analyze(st))
273 return st;
274 PySymtable_Free(st);
275 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000276 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 (void) symtable_exit_block(st, (void *)mod);
278 PySymtable_Free(st);
279 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280}
281
282void
283PySymtable_Free(struct symtable *st)
284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 Py_XDECREF(st->st_blocks);
286 Py_XDECREF(st->st_stack);
287 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288}
289
290PySTEntryObject *
291PySymtable_Lookup(struct symtable *st, void *key)
292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 k = PyLong_FromVoidPtr(key);
296 if (k == NULL)
297 return NULL;
298 v = PyDict_GetItem(st->st_blocks, k);
299 if (v) {
300 assert(PySTEntry_Check(v));
301 Py_INCREF(v);
302 }
303 else {
304 PyErr_SetString(PyExc_KeyError,
305 "unknown symbol table entry");
306 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 Py_DECREF(k);
309 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310}
311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313PyST_GetScope(PySTEntryObject *ste, PyObject *name)
314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
316 if (!v)
317 return 0;
318 assert(PyLong_Check(v));
319 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320}
321
322
323/* Analyze raw symbol information to determine scope of each name.
324
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000325 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000331 explicit global is declared with the global statement. An implicit
332 global is a free variable for which the compiler has found no binding
333 in an enclosing function scope. The implicit global is either a global
334 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
335 to handle these names to implement slightly odd semantics. In such a
336 block, the name is treated as global until it is assigned to; then it
337 is treated as a local.
338
339 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000340 The first pass collects raw facts from the AST via the symtable_visit_*
341 functions: the name is a parameter here, the name is used but not defined
342 here, etc. The second pass analyzes these facts during a pass over the
343 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344
345 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000347 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000348 Names which are explicitly declared nonlocal must exist in this set of
349 visible names - if they do not, a syntax error is raised. After doing
350 the local analysis, it analyzes each of its child blocks using an
351 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352
Nick Coghlan650f0d02007-04-15 12:05:43 +0000353 The children update the free variable set. If a local variable is added to
354 the free variable set by the child, the variable is marked as a cell. The
355 function object being defined must provide runtime storage for the variable
356 that may outlive the function's frame. Cell variables are removed from the
357 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000358
Nick Coghlan650f0d02007-04-15 12:05:43 +0000359 During analysis, the names are:
360 symbols: dict mapping from symbol names to flag values (including offset scope values)
361 scopes: dict mapping from symbol names to scope values (no offset)
362 local: set of all symbol names local to the current scope
363 bound: set of all symbol names local to a containing function scope
364 free: set of all symbol names referenced but not bound in child scopes
365 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366*/
367
368#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 PyObject *o = PyLong_FromLong(I); \
370 if (!o) \
371 return 0; \
372 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
373 Py_DECREF(o); \
374 return 0; \
375 } \
376 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377}
378
379/* Decide on scope of name, given flags.
380
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000381 The namespace dictionaries may be modified to record information
382 about the new name. For example, a new global will add an entry to
383 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000384*/
385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000387analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 PyObject *bound, PyObject *local, PyObject *free,
389 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (flags & DEF_GLOBAL) {
392 if (flags & DEF_PARAM) {
393 PyErr_Format(PyExc_SyntaxError,
394 "name '%U' is parameter and global",
395 name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000396 PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
397 ste->ste_lineno, ste->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398
399 return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000400 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 if (flags & DEF_NONLOCAL) {
402 PyErr_Format(PyExc_SyntaxError,
403 "name '%U' is nonlocal and global",
404 name);
405 return 0;
406 }
407 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
408 if (PySet_Add(global, name) < 0)
409 return 0;
410 if (bound && (PySet_Discard(bound, name) < 0))
411 return 0;
412 return 1;
413 }
414 if (flags & DEF_NONLOCAL) {
415 if (flags & DEF_PARAM) {
416 PyErr_Format(PyExc_SyntaxError,
417 "name '%U' is parameter and nonlocal",
418 name);
419 return 0;
420 }
421 if (!bound) {
422 PyErr_Format(PyExc_SyntaxError,
423 "nonlocal declaration not allowed at module level");
424 return 0;
425 }
426 if (!PySet_Contains(bound, name)) {
427 PyErr_Format(PyExc_SyntaxError,
428 "no binding for nonlocal '%U' found",
429 name);
430
431 return 0;
432 }
433 SET_SCOPE(scopes, name, FREE);
434 ste->ste_free = 1;
435 return PySet_Add(free, name) >= 0;
436 }
437 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000438 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (PySet_Add(local, name) < 0)
440 return 0;
441 if (PySet_Discard(global, name) < 0)
442 return 0;
443 return 1;
444 }
445 /* If an enclosing block has a binding for this name, it
446 is a free variable rather than a global variable.
447 Note that having a non-NULL bound implies that the block
448 is nested.
449 */
450 if (bound && PySet_Contains(bound, name)) {
451 SET_SCOPE(scopes, name, FREE);
452 ste->ste_free = 1;
453 return PySet_Add(free, name) >= 0;
454 }
455 /* If a parent has a global statement, then call it global
456 explicit? It could also be global implicit.
457 */
458 if (global && PySet_Contains(global, name)) {
459 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
460 return 1;
461 }
462 if (ste->ste_nested)
463 ste->ste_free = 1;
464 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
465 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466}
467
468#undef SET_SCOPE
469
470/* If a name is defined in free and also in locals, then this block
471 provides the binding for the free variable. The name should be
472 marked CELL in this block and removed from the free list.
473
474 Note that the current block's free variables are included in free.
475 That's safe because no name can be free and local in the same scope.
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000476
Martin v. Löwis2673a572007-10-29 19:54:24 +0000477 The 'restricted' argument may be set to a string to restrict the analysis
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000478 to the one variable whose name equals that string (e.g. "__class__").
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479*/
480
481static int
Martin v. Löwis2673a572007-10-29 19:54:24 +0000482analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 PyObject *name, *v, *v_cell;
485 int success = 0;
486 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 v_cell = PyLong_FromLong(CELL);
489 if (!v_cell)
490 return 0;
491 while (PyDict_Next(scopes, &pos, &name, &v)) {
492 long scope;
493 assert(PyLong_Check(v));
494 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000495 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 continue;
497 if (!PySet_Contains(free, name))
498 continue;
499 if (restricted != NULL &&
500 PyUnicode_CompareWithASCIIString(name, restricted))
501 continue;
502 /* Replace LOCAL with CELL for this name, and remove
503 from free. It is safe to replace the value of name
504 in the dict, because it will not cause a resize.
505 */
506 if (PyDict_SetItem(scopes, name, v_cell) < 0)
507 goto error;
508 if (PySet_Discard(free, name) < 0)
509 goto error;
510 }
511 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 Py_DECREF(v_cell);
514 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515}
516
517/* Check for illegal statements in unoptimized namespaces */
518static int
519check_unoptimized(const PySTEntryObject* ste) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 const char* trailer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
523 || !(ste->ste_free || ste->ste_child_free))
524 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 trailer = (ste->ste_child_free ?
527 "contains a nested function with free variables" :
528 "is a nested function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 switch (ste->ste_unoptimized) {
531 case OPT_TOPLEVEL: /* import * at top-level is fine */
532 return 1;
533 case OPT_IMPORT_STAR:
534 PyErr_Format(PyExc_SyntaxError,
535 "import * is not allowed in function '%U' because it %s",
536 ste->ste_name, trailer);
537 break;
538 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000540 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
541 ste->ste_opt_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543}
544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545/* Enter the final scope information into the ste_symbols dict.
546 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 * All arguments are dicts. Modifies symbols, others are read-only.
548*/
549static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 PyObject *name = NULL, *itr = NULL;
554 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
555 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* Update scope information for all symbols in this scope */
558 while (PyDict_Next(symbols, &pos, &name, &v)) {
559 long scope, flags;
560 assert(PyLong_Check(v));
561 flags = PyLong_AS_LONG(v);
562 v_scope = PyDict_GetItem(scopes, name);
563 assert(v_scope && PyLong_Check(v_scope));
564 scope = PyLong_AS_LONG(v_scope);
565 flags |= (scope << SCOPE_OFFSET);
566 v_new = PyLong_FromLong(flags);
567 if (!v_new)
568 return 0;
569 if (PyDict_SetItem(symbols, name, v_new) < 0) {
570 Py_DECREF(v_new);
571 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 Py_DECREF(v_new);
574 }
575
576 /* Record not yet resolved free variables from children (if any) */
577 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
578 if (!v_free)
579 return 0;
580
581 itr = PyObject_GetIter(free);
582 if (!itr)
583 goto error;
584
585 while ((name = PyIter_Next(itr))) {
586 v = PyDict_GetItem(symbols, name);
587
588 /* Handle symbol that already exists in this scope */
589 if (v) {
590 /* Handle a free variable in a method of
591 the class that has the same name as a local
592 or global in the class scope.
593 */
594 if (classflag &&
595 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
596 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
597 v_new = PyLong_FromLong(flags);
598 if (!v_new) {
599 goto error;
600 }
601 if (PyDict_SetItem(symbols, name, v_new) < 0) {
602 Py_DECREF(v_new);
603 goto error;
604 }
605 Py_DECREF(v_new);
606 }
607 /* It's a cell, or already free in this scope */
608 Py_DECREF(name);
609 continue;
610 }
611 /* Handle global symbol */
612 if (!PySet_Contains(bound, name)) {
613 Py_DECREF(name);
614 continue; /* it's a global */
615 }
616 /* Propagate new free symbol up the lexical stack */
617 if (PyDict_SetItem(symbols, name, v_free) < 0) {
618 goto error;
619 }
620 Py_DECREF(name);
621 }
622 Py_DECREF(itr);
623 Py_DECREF(v_free);
624 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000625error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 Py_XDECREF(v_free);
627 Py_XDECREF(itr);
628 Py_XDECREF(name);
629 return 0;
630}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631
632/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000633
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 Arguments:
635 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000636 bound -- set of variables bound in enclosing scopes (input). bound
637 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 free -- set of free variables in enclosed scopes (output)
639 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000640
641 The implementation uses two mutually recursive functions,
642 analyze_block() and analyze_child_block(). analyze_block() is
643 responsible for analyzing the individual names defined in a block.
644 analyze_child_block() prepares temporary namespace dictionaries
645 used to evaluated nested blocks.
646
647 The two functions exist because a child block should see the name
648 bindings of its enclosing blocks, but those bindings should not
649 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650*/
651
652static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
654 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000655
656static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
658 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
661 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
662 PyObject *temp;
663 int i, success = 0;
664 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 local = PySet_New(NULL); /* collect new names bound in block */
667 if (!local)
668 goto error;
669 scopes = PyDict_New(); /* collect scopes defined for each name */
670 if (!scopes)
671 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 /* Allocate new global and bound variable dictionaries. These
674 dictionaries hold the names visible in nested blocks. For
675 ClassBlocks, the bound and global names are initialized
676 before analyzing names, because class bindings aren't
677 visible in methods. For other blocks, they are initialized
678 after names are analyzed.
679 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 /* TODO(jhylton): Package these dicts in a struct so that we
682 can write reasonable helper functions?
683 */
684 newglobal = PySet_New(NULL);
685 if (!newglobal)
686 goto error;
687 newfree = PySet_New(NULL);
688 if (!newfree)
689 goto error;
690 newbound = PySet_New(NULL);
691 if (!newbound)
692 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 /* Class namespace has no effect on names visible in
695 nested functions, so populate the global and bound
696 sets to be passed to child blocks before analyzing
697 this one.
698 */
699 if (ste->ste_type == ClassBlock) {
700 /* Pass down known globals */
701 temp = PyNumber_InPlaceOr(newglobal, global);
702 if (!temp)
703 goto error;
704 Py_DECREF(temp);
705 /* Pass down previously bound symbols */
706 if (bound) {
707 temp = PyNumber_InPlaceOr(newbound, bound);
708 if (!temp)
709 goto error;
710 Py_DECREF(temp);
711 }
712 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
715 long flags = PyLong_AS_LONG(v);
716 if (!analyze_name(ste, scopes, name, flags,
717 bound, local, free, global))
718 goto error;
719 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 /* Populate global and bound sets to be passed to children. */
722 if (ste->ste_type != ClassBlock) {
723 /* Add function locals to bound set */
724 if (ste->ste_type == FunctionBlock) {
725 temp = PyNumber_InPlaceOr(newbound, local);
726 if (!temp)
727 goto error;
728 Py_DECREF(temp);
729 }
730 /* Pass down previously bound symbols */
731 if (bound) {
732 temp = PyNumber_InPlaceOr(newbound, bound);
733 if (!temp)
734 goto error;
735 Py_DECREF(temp);
736 }
737 /* Pass down known globals */
738 temp = PyNumber_InPlaceOr(newglobal, global);
739 if (!temp)
740 goto error;
741 Py_DECREF(temp);
742 }
743 else {
744 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000745 if (!GET_IDENTIFIER(__class__))
746 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 assert(PySet_Contains(local, __class__) == 1);
748 if (PySet_Add(newbound, __class__) < 0)
749 goto error;
750 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300752 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 newbound, newglobal now contain the names visible in
755 nested blocks. The free variables in the children will
756 be collected in allfree.
757 */
758 allfree = PySet_New(NULL);
759 if (!allfree)
760 goto error;
761 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
762 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
763 PySTEntryObject* entry;
764 assert(c && PySTEntry_Check(c));
765 entry = (PySTEntryObject*)c;
766 if (!analyze_child_block(entry, newbound, newfree, newglobal,
767 allfree))
768 goto error;
769 /* Check if any children have free variables */
770 if (entry->ste_free || entry->ste_child_free)
771 ste->ste_child_free = 1;
772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 temp = PyNumber_InPlaceOr(newfree, allfree);
775 if (!temp)
776 goto error;
777 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 /* Check if any local variables must be converted to cell variables */
780 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
781 NULL))
782 goto error;
783 else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000784 "__class__"))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 goto error;
786 /* Records the results of the analysis in the symbol table entry */
787 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
788 ste->ste_type == ClassBlock))
789 goto error;
790 if (!check_unoptimized(ste))
791 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 temp = PyNumber_InPlaceOr(free, newfree);
794 if (!temp)
795 goto error;
796 Py_DECREF(temp);
797 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 Py_XDECREF(scopes);
800 Py_XDECREF(local);
801 Py_XDECREF(newbound);
802 Py_XDECREF(newglobal);
803 Py_XDECREF(newfree);
804 Py_XDECREF(allfree);
805 if (!success)
806 assert(PyErr_Occurred());
807 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808}
809
810static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
812 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
815 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 These dictionary are used by all blocks enclosed by the
820 current block. The analyze_block() call modifies these
821 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 */
824 temp_bound = PySet_New(bound);
825 if (!temp_bound)
826 goto error;
827 temp_free = PySet_New(free);
828 if (!temp_free)
829 goto error;
830 temp_global = PySet_New(global);
831 if (!temp_global)
832 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
835 goto error;
836 temp = PyNumber_InPlaceOr(child_free, temp_free);
837 if (!temp)
838 goto error;
839 Py_DECREF(temp);
840 Py_DECREF(temp_bound);
841 Py_DECREF(temp_free);
842 Py_DECREF(temp_global);
843 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000844 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 Py_XDECREF(temp_bound);
846 Py_XDECREF(temp_free);
847 Py_XDECREF(temp_global);
848 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000849}
850
851static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852symtable_analyze(struct symtable *st)
853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 PyObject *free, *global;
855 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 free = PySet_New(NULL);
858 if (!free)
859 return 0;
860 global = PySet_New(NULL);
861 if (!global) {
862 Py_DECREF(free);
863 return 0;
864 }
865 r = analyze_block(st->st_top, NULL, free, global);
866 Py_DECREF(free);
867 Py_DECREF(global);
868 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869}
870
871
872static int
Neal Norwitz5d0ad502005-12-19 04:27:42 +0000873symtable_warn(struct symtable *st, char *msg, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
876 lineno, NULL, NULL) < 0) {
877 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
878 PyErr_SetString(PyExc_SyntaxError, msg);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000879 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
880 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 }
882 return 0;
883 }
884 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885}
886
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000887/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 This reference is released when the block is exited, via the DECREF
889 in symtable_exit_block().
890*/
891
892static int
893symtable_exit_block(struct symtable *st, void *ast)
894{
Benjamin Peterson609da582011-06-29 22:52:39 -0500895 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896
Benjamin Peterson609da582011-06-29 22:52:39 -0500897 st->st_cur = NULL;
898 size = PyList_GET_SIZE(st->st_stack);
899 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500900 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500902 if (--size)
903 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 }
905 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906}
907
908static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000910 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911{
Benjamin Peterson609da582011-06-29 22:52:39 -0500912 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913
Benjamin Peterson609da582011-06-29 22:52:39 -0500914 ste = ste_new(st, name, block, ast, lineno, col_offset);
915 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500917 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
918 Py_DECREF(ste);
919 return 0;
920 }
921 prev = st->st_cur;
922 /* The entry is owned by the stack. Borrow it for st_cur. */
923 Py_DECREF(ste);
924 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000925 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 st->st_global = st->st_cur->ste_symbols;
927 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500928 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 return 0;
930 }
931 }
932 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933}
934
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000935static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936symtable_lookup(struct symtable *st, PyObject *name)
937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 PyObject *o;
939 PyObject *mangled = _Py_Mangle(st->st_private, name);
940 if (!mangled)
941 return 0;
942 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
943 Py_DECREF(mangled);
944 if (!o)
945 return 0;
946 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947}
948
949static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 PyObject *o;
953 PyObject *dict;
954 long val;
955 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956
Jeremy Hylton81e95022007-02-27 06:50:52 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 if (!mangled)
959 return 0;
960 dict = st->st_cur->ste_symbols;
961 if ((o = PyDict_GetItem(dict, mangled))) {
962 val = PyLong_AS_LONG(o);
963 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
964 /* Is it better to use 'mangled' or 'name' here? */
965 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000966 PyErr_SyntaxLocationEx(st->st_filename,
967 st->st_cur->ste_lineno,
968 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 goto error;
970 }
971 val |= flag;
972 } else
973 val = flag;
974 o = PyLong_FromLong(val);
975 if (o == NULL)
976 goto error;
977 if (PyDict_SetItem(dict, mangled, o) < 0) {
978 Py_DECREF(o);
979 goto error;
980 }
981 Py_DECREF(o);
982
983 if (flag & DEF_PARAM) {
984 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
985 goto error;
986 } else if (flag & DEF_GLOBAL) {
987 /* XXX need to update DEF_GLOBAL for other flags too;
988 perhaps only DEF_FREE_GLOBAL */
989 val = flag;
990 if ((o = PyDict_GetItem(st->st_global, mangled))) {
991 val |= PyLong_AS_LONG(o);
992 }
993 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 goto error;
996 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
997 Py_DECREF(o);
998 goto error;
999 }
1000 Py_DECREF(o);
1001 }
1002 Py_DECREF(mangled);
1003 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001004
1005error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 Py_DECREF(mangled);
1007 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008}
1009
1010/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1011 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 function.
1013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1015 useful if the first node in the sequence requires special treatment.
1016*/
1017
1018#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 if (!symtable_visit_ ## TYPE((ST), (V))) \
1020 return 0;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001021
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 int i; \
1024 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1025 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1026 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1027 if (!symtable_visit_ ## TYPE((ST), elt)) \
1028 return 0; \
1029 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001031
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 int i; \
1034 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1035 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1036 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1037 if (!symtable_visit_ ## TYPE((ST), elt)) \
1038 return 0; \
1039 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001041
Guido van Rossum4f72a782006-10-27 23:31:49 +00001042#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 int i = 0; \
1044 asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1045 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1046 expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1047 if (!elt) continue; /* can be NULL */ \
1048 if (!symtable_visit_expr((ST), elt)) \
1049 return 0; \
1050 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001051}
1052
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053static int
Guido van Rossumc2e20742006-02-27 22:32:47 +00001054symtable_new_tmpname(struct symtable *st)
1055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 char tmpname[256];
1057 identifier tmp;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1060 ++st->st_cur->ste_tmpname);
1061 tmp = PyUnicode_InternFromString(tmpname);
1062 if (!tmp)
1063 return 0;
1064 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1065 return 0;
1066 Py_DECREF(tmp);
1067 return 1;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001068}
1069
Guido van Rossum4f72a782006-10-27 23:31:49 +00001070
Guido van Rossumc2e20742006-02-27 22:32:47 +00001071static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072symtable_visit_stmt(struct symtable *st, stmt_ty s)
1073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 switch (s->kind) {
1075 case FunctionDef_kind:
1076 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1077 return 0;
1078 if (s->v.FunctionDef.args->defaults)
1079 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1080 if (s->v.FunctionDef.args->kw_defaults)
1081 VISIT_KWONLYDEFAULTS(st,
1082 s->v.FunctionDef.args->kw_defaults);
1083 if (!symtable_visit_annotations(st, s))
1084 return 0;
1085 if (s->v.FunctionDef.decorator_list)
1086 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1087 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001088 FunctionBlock, (void *)s, s->lineno,
1089 s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001091 VISIT(st, arguments, s->v.FunctionDef.args);
1092 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 if (!symtable_exit_block(st, s))
1094 return 0;
1095 break;
1096 case ClassDef_kind: {
1097 PyObject *tmp;
1098 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1099 return 0;
1100 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1101 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1102 if (s->v.ClassDef.starargs)
1103 VISIT(st, expr, s->v.ClassDef.starargs);
1104 if (s->v.ClassDef.kwargs)
1105 VISIT(st, expr, s->v.ClassDef.kwargs);
1106 if (s->v.ClassDef.decorator_list)
1107 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1108 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001109 (void *)s, s->lineno, s->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 return 0;
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001111 if (!GET_IDENTIFIER(__class__) ||
1112 !symtable_add_def(st, __class__, DEF_LOCAL) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 !GET_IDENTIFIER(__locals__) ||
1114 !symtable_add_def(st, __locals__, DEF_PARAM)) {
1115 symtable_exit_block(st, s);
1116 return 0;
1117 }
1118 tmp = st->st_private;
1119 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001120 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 st->st_private = tmp;
1122 if (!symtable_exit_block(st, s))
1123 return 0;
1124 break;
1125 }
1126 case Return_kind:
1127 if (s->v.Return.value) {
1128 VISIT(st, expr, s->v.Return.value);
1129 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 }
1131 break;
1132 case Delete_kind:
1133 VISIT_SEQ(st, expr, s->v.Delete.targets);
1134 break;
1135 case Assign_kind:
1136 VISIT_SEQ(st, expr, s->v.Assign.targets);
1137 VISIT(st, expr, s->v.Assign.value);
1138 break;
1139 case AugAssign_kind:
1140 VISIT(st, expr, s->v.AugAssign.target);
1141 VISIT(st, expr, s->v.AugAssign.value);
1142 break;
1143 case For_kind:
1144 VISIT(st, expr, s->v.For.target);
1145 VISIT(st, expr, s->v.For.iter);
1146 VISIT_SEQ(st, stmt, s->v.For.body);
1147 if (s->v.For.orelse)
1148 VISIT_SEQ(st, stmt, s->v.For.orelse);
1149 break;
1150 case While_kind:
1151 VISIT(st, expr, s->v.While.test);
1152 VISIT_SEQ(st, stmt, s->v.While.body);
1153 if (s->v.While.orelse)
1154 VISIT_SEQ(st, stmt, s->v.While.orelse);
1155 break;
1156 case If_kind:
1157 /* XXX if 0: and lookup_yield() hacks */
1158 VISIT(st, expr, s->v.If.test);
1159 VISIT_SEQ(st, stmt, s->v.If.body);
1160 if (s->v.If.orelse)
1161 VISIT_SEQ(st, stmt, s->v.If.orelse);
1162 break;
1163 case Raise_kind:
1164 if (s->v.Raise.exc) {
1165 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001166 if (s->v.Raise.cause) {
1167 VISIT(st, expr, s->v.Raise.cause);
1168 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 }
1170 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001171 case Try_kind:
1172 VISIT_SEQ(st, stmt, s->v.Try.body);
1173 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1174 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1175 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 break;
1177 case Assert_kind:
1178 VISIT(st, expr, s->v.Assert.test);
1179 if (s->v.Assert.msg)
1180 VISIT(st, expr, s->v.Assert.msg);
1181 break;
1182 case Import_kind:
1183 VISIT_SEQ(st, alias, s->v.Import.names);
1184 /* XXX Don't have the lineno available inside
1185 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001186 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001188 st->st_cur->ste_opt_col_offset = s->col_offset;
1189 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 break;
1191 case ImportFrom_kind:
1192 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1193 /* XXX Don't have the lineno available inside
1194 visit_alias */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001195 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 st->st_cur->ste_opt_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001197 st->st_cur->ste_opt_col_offset = s->col_offset;
1198 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 break;
1200 case Global_kind: {
1201 int i;
1202 asdl_seq *seq = s->v.Global.names;
1203 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1204 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 long cur = symtable_lookup(st, name);
1206 if (cur < 0)
1207 return 0;
1208 if (cur & (DEF_LOCAL | USE)) {
1209 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001210 char *c_name = _PyUnicode_AsString(name);
1211 if (!c_name)
1212 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 if (cur & DEF_LOCAL)
1214 PyOS_snprintf(buf, sizeof(buf),
1215 GLOBAL_AFTER_ASSIGN,
1216 c_name);
1217 else
1218 PyOS_snprintf(buf, sizeof(buf),
1219 GLOBAL_AFTER_USE,
1220 c_name);
1221 if (!symtable_warn(st, buf, s->lineno))
1222 return 0;
1223 }
1224 if (!symtable_add_def(st, name, DEF_GLOBAL))
1225 return 0;
1226 }
1227 break;
1228 }
1229 case Nonlocal_kind: {
1230 int i;
1231 asdl_seq *seq = s->v.Nonlocal.names;
1232 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1233 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 long cur = symtable_lookup(st, name);
1235 if (cur < 0)
1236 return 0;
1237 if (cur & (DEF_LOCAL | USE)) {
1238 char buf[256];
Benjamin Petersone132f522012-10-31 19:01:42 -04001239 char *c_name = _PyUnicode_AsString(name);
1240 if (!c_name)
1241 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 if (cur & DEF_LOCAL)
1243 PyOS_snprintf(buf, sizeof(buf),
1244 NONLOCAL_AFTER_ASSIGN,
1245 c_name);
1246 else
1247 PyOS_snprintf(buf, sizeof(buf),
1248 NONLOCAL_AFTER_USE,
1249 c_name);
1250 if (!symtable_warn(st, buf, s->lineno))
1251 return 0;
1252 }
1253 if (!symtable_add_def(st, name, DEF_NONLOCAL))
1254 return 0;
1255 }
1256 break;
1257 }
1258 case Expr_kind:
1259 VISIT(st, expr, s->v.Expr.value);
1260 break;
1261 case Pass_kind:
1262 case Break_kind:
1263 case Continue_kind:
1264 /* nothing to do here */
1265 break;
1266 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001267 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 VISIT_SEQ(st, stmt, s->v.With.body);
1269 break;
1270 }
1271 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272}
1273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275symtable_visit_expr(struct symtable *st, expr_ty e)
1276{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 switch (e->kind) {
1278 case BoolOp_kind:
1279 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1280 break;
1281 case BinOp_kind:
1282 VISIT(st, expr, e->v.BinOp.left);
1283 VISIT(st, expr, e->v.BinOp.right);
1284 break;
1285 case UnaryOp_kind:
1286 VISIT(st, expr, e->v.UnaryOp.operand);
1287 break;
1288 case Lambda_kind: {
1289 if (!GET_IDENTIFIER(lambda))
1290 return 0;
1291 if (e->v.Lambda.args->defaults)
1292 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001293 if (e->v.Lambda.args->kw_defaults)
1294 VISIT_KWONLYDEFAULTS(st,
1295 e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001297 FunctionBlock, (void *)e, e->lineno,
1298 e->col_offset))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 return 0;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001300 VISIT(st, arguments, e->v.Lambda.args);
1301 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 if (!symtable_exit_block(st, (void *)e))
1303 return 0;
1304 break;
1305 }
1306 case IfExp_kind:
1307 VISIT(st, expr, e->v.IfExp.test);
1308 VISIT(st, expr, e->v.IfExp.body);
1309 VISIT(st, expr, e->v.IfExp.orelse);
1310 break;
1311 case Dict_kind:
1312 VISIT_SEQ(st, expr, e->v.Dict.keys);
1313 VISIT_SEQ(st, expr, e->v.Dict.values);
1314 break;
1315 case Set_kind:
1316 VISIT_SEQ(st, expr, e->v.Set.elts);
1317 break;
1318 case GeneratorExp_kind:
1319 if (!symtable_visit_genexp(st, e))
1320 return 0;
1321 break;
1322 case ListComp_kind:
1323 if (!symtable_visit_listcomp(st, e))
1324 return 0;
1325 break;
1326 case SetComp_kind:
1327 if (!symtable_visit_setcomp(st, e))
1328 return 0;
1329 break;
1330 case DictComp_kind:
1331 if (!symtable_visit_dictcomp(st, e))
1332 return 0;
1333 break;
1334 case Yield_kind:
Benjamin Peterson527c6222012-01-14 08:58:23 -05001335 case YieldFrom_kind: {
1336 expr_ty value;
1337 value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value;
1338 if (value)
1339 VISIT(st, expr, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 break;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001342 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 case Compare_kind:
1344 VISIT(st, expr, e->v.Compare.left);
1345 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1346 break;
1347 case Call_kind:
1348 VISIT(st, expr, e->v.Call.func);
1349 VISIT_SEQ(st, expr, e->v.Call.args);
1350 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1351 if (e->v.Call.starargs)
1352 VISIT(st, expr, e->v.Call.starargs);
1353 if (e->v.Call.kwargs)
1354 VISIT(st, expr, e->v.Call.kwargs);
1355 break;
1356 case Num_kind:
1357 case Str_kind:
1358 case Bytes_kind:
1359 case Ellipsis_kind:
1360 /* Nothing to do here. */
1361 break;
1362 /* The following exprs can be assignment targets. */
1363 case Attribute_kind:
1364 VISIT(st, expr, e->v.Attribute.value);
1365 break;
1366 case Subscript_kind:
1367 VISIT(st, expr, e->v.Subscript.value);
1368 VISIT(st, slice, e->v.Subscript.slice);
1369 break;
1370 case Starred_kind:
1371 VISIT(st, expr, e->v.Starred.value);
1372 break;
1373 case Name_kind:
1374 if (!symtable_add_def(st, e->v.Name.id,
1375 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1376 return 0;
1377 /* Special-case super: it counts as a use of __class__ */
1378 if (e->v.Name.ctx == Load &&
1379 st->st_cur->ste_type == FunctionBlock &&
1380 !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001381 if (!GET_IDENTIFIER(__class__) ||
1382 !symtable_add_def(st, __class__, USE))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 return 0;
1384 }
1385 break;
1386 /* child nodes of List and Tuple will have expr_context set */
1387 case List_kind:
1388 VISIT_SEQ(st, expr, e->v.List.elts);
1389 break;
1390 case Tuple_kind:
1391 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1392 break;
1393 }
1394 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395}
1396
1397static int
1398symtable_implicit_arg(struct symtable *st, int pos)
1399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1401 if (id == NULL)
1402 return 0;
1403 if (!symtable_add_def(st, id, DEF_PARAM)) {
1404 Py_DECREF(id);
1405 return 0;
1406 }
1407 Py_DECREF(id);
1408 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409}
1410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001412symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 if (!args)
1417 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 for (i = 0; i < asdl_seq_LEN(args); i++) {
1420 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1421 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1422 return 0;
1423 }
1424
1425 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426}
1427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001429symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (!args)
1434 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 for (i = 0; i < asdl_seq_LEN(args); i++) {
1437 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1438 if (arg->annotation)
1439 VISIT(st, expr, arg->annotation);
1440 }
1441
1442 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001443}
1444
Neal Norwitzc1505362006-12-28 06:47:50 +00001445static int
1446symtable_visit_annotations(struct symtable *st, stmt_ty s)
1447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 arguments_ty a = s->v.FunctionDef.args;
1449
1450 if (a->args && !symtable_visit_argannotations(st, a->args))
1451 return 0;
1452 if (a->varargannotation)
1453 VISIT(st, expr, a->varargannotation);
1454 if (a->kwargannotation)
1455 VISIT(st, expr, a->kwargannotation);
1456 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1457 return 0;
1458 if (s->v.FunctionDef.returns)
1459 VISIT(st, expr, s->v.FunctionDef.returns);
1460 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461}
1462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464symtable_visit_arguments(struct symtable *st, arguments_ty a)
1465{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 /* skip default arguments inside function block
1467 XXX should ast be different?
1468 */
1469 if (a->args && !symtable_visit_params(st, a->args))
1470 return 0;
1471 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1472 return 0;
1473 if (a->vararg) {
1474 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1475 return 0;
1476 st->st_cur->ste_varargs = 1;
1477 }
1478 if (a->kwarg) {
1479 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1480 return 0;
1481 st->st_cur->ste_varkeywords = 1;
1482 }
1483 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484}
1485
1486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1489{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 if (eh->v.ExceptHandler.type)
1491 VISIT(st, expr, eh->v.ExceptHandler.type);
1492 if (eh->v.ExceptHandler.name)
1493 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1494 return 0;
1495 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1496 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497}
1498
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001499static int
1500symtable_visit_withitem(struct symtable *st, withitem_ty item)
1501{
1502 VISIT(st, expr, item->context_expr);
1503 if (item->optional_vars) {
1504 VISIT(st, expr, item->optional_vars);
1505 }
1506 return 1;
1507}
1508
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511symtable_visit_alias(struct symtable *st, alias_ty a)
1512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001513 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001514 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 dotted package name (e.g. spam.eggs)
1516 */
1517 PyObject *store_name;
1518 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001519 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1520 PyUnicode_GET_LENGTH(name), 1);
1521 if (dot != -1) {
1522 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (!store_name)
1524 return 0;
1525 }
1526 else {
1527 store_name = name;
1528 Py_INCREF(store_name);
1529 }
1530 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1531 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1532 Py_DECREF(store_name);
1533 return r;
1534 }
1535 else {
1536 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001537 int lineno = st->st_cur->ste_lineno;
1538 int col_offset = st->st_cur->ste_col_offset;
1539 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1540 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1541 Py_DECREF(store_name);
1542 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 }
1544 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1545 Py_DECREF(store_name);
1546 return 1;
1547 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548}
1549
1550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 VISIT(st, expr, lc->target);
1555 VISIT(st, expr, lc->iter);
1556 VISIT_SEQ(st, expr, lc->ifs);
1557 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558}
1559
1560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562symtable_visit_keyword(struct symtable *st, keyword_ty k)
1563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 VISIT(st, expr, k->value);
1565 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566}
1567
1568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570symtable_visit_slice(struct symtable *st, slice_ty s)
1571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 switch (s->kind) {
1573 case Slice_kind:
1574 if (s->v.Slice.lower)
1575 VISIT(st, expr, s->v.Slice.lower)
1576 if (s->v.Slice.upper)
1577 VISIT(st, expr, s->v.Slice.upper)
1578 if (s->v.Slice.step)
1579 VISIT(st, expr, s->v.Slice.step)
1580 break;
1581 case ExtSlice_kind:
1582 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1583 break;
1584 case Index_kind:
1585 VISIT(st, expr, s->v.Index.value)
1586 break;
1587 }
1588 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589}
1590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001592symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001593 identifier scope_name, asdl_seq *generators,
1594 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 int is_generator = (e->kind == GeneratorExp_kind);
1597 int needs_tmp = !is_generator;
1598 comprehension_ty outermost = ((comprehension_ty)
1599 asdl_seq_GET(generators, 0));
1600 /* Outermost iterator is evaluated in current scope */
1601 VISIT(st, expr, outermost->iter);
1602 /* Create comprehension scope for the rest */
1603 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001604 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1605 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 return 0;
1607 }
1608 st->st_cur->ste_generator = is_generator;
1609 /* Outermost iter is received as an argument */
1610 if (!symtable_implicit_arg(st, 0)) {
1611 symtable_exit_block(st, (void *)e);
1612 return 0;
1613 }
1614 /* Allocate temporary name if needed */
1615 if (needs_tmp && !symtable_new_tmpname(st)) {
1616 symtable_exit_block(st, (void *)e);
1617 return 0;
1618 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001619 VISIT(st, expr, outermost->target);
1620 VISIT_SEQ(st, expr, outermost->ifs);
1621 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001623 VISIT(st, expr, value);
1624 VISIT(st, expr, elt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001629symtable_visit_genexp(struct symtable *st, expr_ty e)
1630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1632 e->v.GeneratorExp.generators,
1633 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001634}
1635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001637symtable_visit_listcomp(struct symtable *st, expr_ty e)
1638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1640 e->v.ListComp.generators,
1641 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001642}
1643
1644static int
1645symtable_visit_setcomp(struct symtable *st, expr_ty e)
1646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1648 e->v.SetComp.generators,
1649 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001650}
1651
1652static int
1653symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1656 e->v.DictComp.generators,
1657 e->v.DictComp.key,
1658 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001659}