Line data Source code
1 : #include "Python.h"
2 : #include "Python-ast.h"
3 : #include "code.h"
4 : #include "symtable.h"
5 : #include "structmember.h"
6 :
7 : /* error strings used for warnings */
8 : #define GLOBAL_AFTER_ASSIGN \
9 : "name '%.400s' is assigned to before global declaration"
10 :
11 : #define NONLOCAL_AFTER_ASSIGN \
12 : "name '%.400s' is assigned to before nonlocal declaration"
13 :
14 : #define GLOBAL_AFTER_USE \
15 : "name '%.400s' is used prior to global declaration"
16 :
17 : #define NONLOCAL_AFTER_USE \
18 : "name '%.400s' is used prior to nonlocal declaration"
19 :
20 : #define IMPORT_STAR_WARNING "import * only allowed at module level"
21 :
22 : static PySTEntryObject *
23 30 : ste_new(struct symtable *st, identifier name, _Py_block_ty block,
24 : void *key, int lineno, int col_offset)
25 : {
26 30 : PySTEntryObject *ste = NULL;
27 : PyObject *k;
28 :
29 30 : k = PyLong_FromVoidPtr(key);
30 30 : if (k == NULL)
31 0 : goto fail;
32 30 : ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
33 30 : if (ste == NULL)
34 0 : goto fail;
35 30 : ste->ste_table = st;
36 30 : ste->ste_id = k;
37 :
38 30 : ste->ste_name = name;
39 30 : Py_INCREF(name);
40 :
41 30 : ste->ste_symbols = NULL;
42 30 : ste->ste_varnames = NULL;
43 30 : ste->ste_children = NULL;
44 :
45 30 : ste->ste_symbols = PyDict_New();
46 30 : if (ste->ste_symbols == NULL)
47 0 : goto fail;
48 :
49 30 : ste->ste_varnames = PyList_New(0);
50 30 : if (ste->ste_varnames == NULL)
51 0 : goto fail;
52 :
53 30 : ste->ste_children = PyList_New(0);
54 30 : if (ste->ste_children == NULL)
55 0 : goto fail;
56 :
57 30 : ste->ste_type = block;
58 30 : ste->ste_unoptimized = 0;
59 30 : ste->ste_nested = 0;
60 30 : ste->ste_free = 0;
61 30 : ste->ste_varargs = 0;
62 30 : ste->ste_varkeywords = 0;
63 30 : ste->ste_opt_lineno = 0;
64 30 : ste->ste_opt_col_offset = 0;
65 30 : ste->ste_tmpname = 0;
66 30 : ste->ste_lineno = lineno;
67 30 : ste->ste_col_offset = col_offset;
68 :
69 57 : if (st->st_cur != NULL &&
70 54 : (st->st_cur->ste_nested ||
71 27 : st->st_cur->ste_type == FunctionBlock))
72 0 : ste->ste_nested = 1;
73 30 : ste->ste_child_free = 0;
74 30 : ste->ste_generator = 0;
75 30 : ste->ste_returns_value = 0;
76 :
77 30 : if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
78 0 : goto fail;
79 :
80 30 : return ste;
81 : fail:
82 0 : Py_XDECREF(ste);
83 0 : return NULL;
84 : }
85 :
86 : static PyObject *
87 0 : ste_repr(PySTEntryObject *ste)
88 : {
89 0 : return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
90 : ste->ste_name,
91 : PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
92 : }
93 :
94 : static void
95 30 : ste_dealloc(PySTEntryObject *ste)
96 : {
97 30 : ste->ste_table = NULL;
98 30 : Py_XDECREF(ste->ste_id);
99 30 : Py_XDECREF(ste->ste_name);
100 30 : Py_XDECREF(ste->ste_symbols);
101 30 : Py_XDECREF(ste->ste_varnames);
102 30 : Py_XDECREF(ste->ste_children);
103 30 : PyObject_Del(ste);
104 30 : }
105 :
106 : #define OFF(x) offsetof(PySTEntryObject, x)
107 :
108 : static PyMemberDef ste_memberlist[] = {
109 : {"id", T_OBJECT, OFF(ste_id), READONLY},
110 : {"name", T_OBJECT, OFF(ste_name), READONLY},
111 : {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
112 : {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
113 : {"children", T_OBJECT, OFF(ste_children), READONLY},
114 : {"optimized",T_INT, OFF(ste_unoptimized), READONLY},
115 : {"nested", T_INT, OFF(ste_nested), READONLY},
116 : {"type", T_INT, OFF(ste_type), READONLY},
117 : {"lineno", T_INT, OFF(ste_lineno), READONLY},
118 : {NULL}
119 : };
120 :
121 : PyTypeObject PySTEntry_Type = {
122 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
123 : "symtable entry",
124 : sizeof(PySTEntryObject),
125 : 0,
126 : (destructor)ste_dealloc, /* tp_dealloc */
127 : 0, /* tp_print */
128 : 0, /* tp_getattr */
129 : 0, /* tp_setattr */
130 : 0, /* tp_reserved */
131 : (reprfunc)ste_repr, /* tp_repr */
132 : 0, /* tp_as_number */
133 : 0, /* tp_as_sequence */
134 : 0, /* tp_as_mapping */
135 : 0, /* tp_hash */
136 : 0, /* tp_call */
137 : 0, /* tp_str */
138 : PyObject_GenericGetAttr, /* tp_getattro */
139 : 0, /* tp_setattro */
140 : 0, /* tp_as_buffer */
141 : Py_TPFLAGS_DEFAULT, /* tp_flags */
142 : 0, /* tp_doc */
143 : 0, /* tp_traverse */
144 : 0, /* tp_clear */
145 : 0, /* tp_richcompare */
146 : 0, /* tp_weaklistoffset */
147 : 0, /* tp_iter */
148 : 0, /* tp_iternext */
149 : 0, /* tp_methods */
150 : ste_memberlist, /* tp_members */
151 : 0, /* tp_getset */
152 : 0, /* tp_base */
153 : 0, /* tp_dict */
154 : 0, /* tp_descr_get */
155 : 0, /* tp_descr_set */
156 : 0, /* tp_dictoffset */
157 : 0, /* tp_init */
158 : 0, /* tp_alloc */
159 : 0, /* tp_new */
160 : };
161 :
162 : static int symtable_analyze(struct symtable *st);
163 : static int symtable_warn(struct symtable *st, char *msg, int lineno);
164 : static int symtable_enter_block(struct symtable *st, identifier name,
165 : _Py_block_ty block, void *ast, int lineno,
166 : int col_offset);
167 : static int symtable_exit_block(struct symtable *st, void *ast);
168 : static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
169 : static int symtable_visit_expr(struct symtable *st, expr_ty s);
170 : static int symtable_visit_genexp(struct symtable *st, expr_ty s);
171 : static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
172 : static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
173 : static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
174 : static int symtable_visit_arguments(struct symtable *st, arguments_ty);
175 : static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
176 : static int symtable_visit_alias(struct symtable *st, alias_ty);
177 : static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
178 : static int symtable_visit_keyword(struct symtable *st, keyword_ty);
179 : static int symtable_visit_slice(struct symtable *st, slice_ty);
180 : static int symtable_visit_params(struct symtable *st, asdl_seq *args);
181 : static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
182 : static int symtable_implicit_arg(struct symtable *st, int pos);
183 : static int symtable_visit_annotations(struct symtable *st, stmt_ty s);
184 : static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
185 :
186 :
187 : static identifier top = NULL, lambda = NULL, genexpr = NULL,
188 : listcomp = NULL, setcomp = NULL, dictcomp = NULL,
189 : __class__ = NULL, __locals__ = NULL;
190 :
191 : #define GET_IDENTIFIER(VAR) \
192 : ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
193 :
194 : #define DUPLICATE_ARGUMENT \
195 : "duplicate argument '%U' in function definition"
196 :
197 : static struct symtable *
198 3 : symtable_new(void)
199 : {
200 : struct symtable *st;
201 :
202 3 : st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
203 3 : if (st == NULL)
204 0 : return NULL;
205 :
206 3 : st->st_filename = NULL;
207 3 : st->st_blocks = NULL;
208 :
209 3 : if ((st->st_stack = PyList_New(0)) == NULL)
210 0 : goto fail;
211 3 : if ((st->st_blocks = PyDict_New()) == NULL)
212 0 : goto fail;
213 3 : st->st_cur = NULL;
214 3 : st->st_private = NULL;
215 3 : return st;
216 : fail:
217 0 : PySymtable_Free(st);
218 0 : return NULL;
219 : }
220 :
221 : struct symtable *
222 3 : PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
223 : {
224 3 : struct symtable *st = symtable_new();
225 : asdl_seq *seq;
226 : int i;
227 :
228 3 : if (st == NULL)
229 0 : return st;
230 3 : st->st_filename = filename;
231 3 : st->st_future = future;
232 : /* Make the initial symbol information gathering pass */
233 6 : if (!GET_IDENTIFIER(top) ||
234 3 : !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
235 0 : PySymtable_Free(st);
236 0 : return NULL;
237 : }
238 :
239 3 : st->st_top = st->st_cur;
240 3 : st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
241 3 : switch (mod->kind) {
242 : case Module_kind:
243 3 : seq = mod->v.Module.body;
244 25 : for (i = 0; i < asdl_seq_LEN(seq); i++)
245 22 : if (!symtable_visit_stmt(st,
246 22 : (stmt_ty)asdl_seq_GET(seq, i)))
247 0 : goto error;
248 3 : break;
249 : case Expression_kind:
250 0 : if (!symtable_visit_expr(st, mod->v.Expression.body))
251 0 : goto error;
252 0 : break;
253 : case Interactive_kind:
254 0 : seq = mod->v.Interactive.body;
255 0 : for (i = 0; i < asdl_seq_LEN(seq); i++)
256 0 : if (!symtable_visit_stmt(st,
257 0 : (stmt_ty)asdl_seq_GET(seq, i)))
258 0 : goto error;
259 0 : break;
260 : case Suite_kind:
261 0 : PyErr_SetString(PyExc_RuntimeError,
262 : "this compiler does not handle Suites");
263 0 : goto error;
264 : }
265 3 : if (!symtable_exit_block(st, (void *)mod)) {
266 0 : PySymtable_Free(st);
267 0 : return NULL;
268 : }
269 : /* Make the second symbol analysis pass */
270 3 : if (symtable_analyze(st))
271 3 : return st;
272 0 : PySymtable_Free(st);
273 0 : return NULL;
274 : error:
275 0 : (void) symtable_exit_block(st, (void *)mod);
276 0 : PySymtable_Free(st);
277 0 : return NULL;
278 : }
279 :
280 : void
281 3 : PySymtable_Free(struct symtable *st)
282 : {
283 3 : Py_XDECREF(st->st_blocks);
284 3 : Py_XDECREF(st->st_stack);
285 3 : PyMem_Free((void *)st);
286 3 : }
287 :
288 : PySTEntryObject *
289 30 : PySymtable_Lookup(struct symtable *st, void *key)
290 : {
291 : PyObject *k, *v;
292 :
293 30 : k = PyLong_FromVoidPtr(key);
294 30 : if (k == NULL)
295 0 : return NULL;
296 30 : v = PyDict_GetItem(st->st_blocks, k);
297 30 : if (v) {
298 : assert(PySTEntry_Check(v));
299 30 : Py_INCREF(v);
300 : }
301 : else {
302 0 : PyErr_SetString(PyExc_KeyError,
303 : "unknown symbol table entry");
304 : }
305 :
306 30 : Py_DECREF(k);
307 30 : return (PySTEntryObject *)v;
308 : }
309 :
310 : int
311 376 : PyST_GetScope(PySTEntryObject *ste, PyObject *name)
312 : {
313 376 : PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
314 376 : if (!v)
315 11 : return 0;
316 : assert(PyLong_Check(v));
317 365 : return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
318 : }
319 :
320 :
321 : /* Analyze raw symbol information to determine scope of each name.
322 :
323 : The next several functions are helpers for symtable_analyze(),
324 : which determines whether a name is local, global, or free. In addition,
325 : it determines which local variables are cell variables; they provide
326 : bindings that are used for free variables in enclosed blocks.
327 :
328 : There are also two kinds of global variables, implicit and explicit. An
329 : explicit global is declared with the global statement. An implicit
330 : global is a free variable for which the compiler has found no binding
331 : in an enclosing function scope. The implicit global is either a global
332 : or a builtin. Python's module and class blocks use the xxx_NAME opcodes
333 : to handle these names to implement slightly odd semantics. In such a
334 : block, the name is treated as global until it is assigned to; then it
335 : is treated as a local.
336 :
337 : The symbol table requires two passes to determine the scope of each name.
338 : The first pass collects raw facts from the AST via the symtable_visit_*
339 : functions: the name is a parameter here, the name is used but not defined
340 : here, etc. The second pass analyzes these facts during a pass over the
341 : PySTEntryObjects created during pass 1.
342 :
343 : When a function is entered during the second pass, the parent passes
344 : the set of all name bindings visible to its children. These bindings
345 : are used to determine if non-local variables are free or implicit globals.
346 : Names which are explicitly declared nonlocal must exist in this set of
347 : visible names - if they do not, a syntax error is raised. After doing
348 : the local analysis, it analyzes each of its child blocks using an
349 : updated set of name bindings.
350 :
351 : The children update the free variable set. If a local variable is added to
352 : the free variable set by the child, the variable is marked as a cell. The
353 : function object being defined must provide runtime storage for the variable
354 : that may outlive the function's frame. Cell variables are removed from the
355 : free set before the analyze function returns to its parent.
356 :
357 : During analysis, the names are:
358 : symbols: dict mapping from symbol names to flag values (including offset scope values)
359 : scopes: dict mapping from symbol names to scope values (no offset)
360 : local: set of all symbol names local to the current scope
361 : bound: set of all symbol names local to a containing function scope
362 : free: set of all symbol names referenced but not bound in child scopes
363 : global: set of all symbol names explicitly declared as global
364 : */
365 :
366 : #define SET_SCOPE(DICT, NAME, I) { \
367 : PyObject *o = PyLong_FromLong(I); \
368 : if (!o) \
369 : return 0; \
370 : if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
371 : Py_DECREF(o); \
372 : return 0; \
373 : } \
374 : Py_DECREF(o); \
375 : }
376 :
377 : /* Decide on scope of name, given flags.
378 :
379 : The namespace dictionaries may be modified to record information
380 : about the new name. For example, a new global will add an entry to
381 : global. A name that was global can be changed to local.
382 : */
383 :
384 : static int
385 200 : analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
386 : PyObject *bound, PyObject *local, PyObject *free,
387 : PyObject *global)
388 : {
389 200 : if (flags & DEF_GLOBAL) {
390 0 : if (flags & DEF_PARAM) {
391 0 : PyErr_Format(PyExc_SyntaxError,
392 : "name '%U' is parameter and global",
393 : name);
394 0 : PyErr_SyntaxLocationEx(ste->ste_table->st_filename,
395 : ste->ste_lineno, ste->ste_col_offset);
396 :
397 0 : return 0;
398 : }
399 0 : if (flags & DEF_NONLOCAL) {
400 0 : PyErr_Format(PyExc_SyntaxError,
401 : "name '%U' is nonlocal and global",
402 : name);
403 0 : return 0;
404 : }
405 0 : SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
406 0 : if (PySet_Add(global, name) < 0)
407 0 : return 0;
408 0 : if (bound && (PySet_Discard(bound, name) < 0))
409 0 : return 0;
410 0 : return 1;
411 : }
412 200 : if (flags & DEF_NONLOCAL) {
413 0 : if (flags & DEF_PARAM) {
414 0 : PyErr_Format(PyExc_SyntaxError,
415 : "name '%U' is parameter and nonlocal",
416 : name);
417 0 : return 0;
418 : }
419 0 : if (!bound) {
420 0 : PyErr_Format(PyExc_SyntaxError,
421 : "nonlocal declaration not allowed at module level");
422 0 : return 0;
423 : }
424 0 : if (!PySet_Contains(bound, name)) {
425 0 : PyErr_Format(PyExc_SyntaxError,
426 : "no binding for nonlocal '%U' found",
427 : name);
428 :
429 0 : return 0;
430 : }
431 0 : SET_SCOPE(scopes, name, FREE);
432 0 : ste->ste_free = 1;
433 0 : return PySet_Add(free, name) >= 0;
434 : }
435 200 : if (flags & DEF_BOUND) {
436 146 : SET_SCOPE(scopes, name, LOCAL);
437 146 : if (PySet_Add(local, name) < 0)
438 0 : return 0;
439 146 : if (PySet_Discard(global, name) < 0)
440 0 : return 0;
441 146 : return 1;
442 : }
443 : /* If an enclosing block has a binding for this name, it
444 : is a free variable rather than a global variable.
445 : Note that having a non-NULL bound implies that the block
446 : is nested.
447 : */
448 54 : if (bound && PySet_Contains(bound, name)) {
449 0 : SET_SCOPE(scopes, name, FREE);
450 0 : ste->ste_free = 1;
451 0 : return PySet_Add(free, name) >= 0;
452 : }
453 : /* If a parent has a global statement, then call it global
454 : explicit? It could also be global implicit.
455 : */
456 54 : if (global && PySet_Contains(global, name)) {
457 0 : SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
458 0 : return 1;
459 : }
460 54 : if (ste->ste_nested)
461 0 : ste->ste_free = 1;
462 54 : SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
463 54 : return 1;
464 : }
465 :
466 : #undef SET_SCOPE
467 :
468 : /* If a name is defined in free and also in locals, then this block
469 : provides the binding for the free variable. The name should be
470 : marked CELL in this block and removed from the free list.
471 :
472 : Note that the current block's free variables are included in free.
473 : That's safe because no name can be free and local in the same scope.
474 :
475 : The 'restricted' argument may be set to a string to restrict the analysis
476 : to the one variable whose name equals that string (e.g. "__class__").
477 : */
478 :
479 : static int
480 27 : analyze_cells(PyObject *scopes, PyObject *free, const char *restricted)
481 : {
482 : PyObject *name, *v, *v_cell;
483 27 : int success = 0;
484 27 : Py_ssize_t pos = 0;
485 :
486 27 : v_cell = PyLong_FromLong(CELL);
487 27 : if (!v_cell)
488 0 : return 0;
489 222 : while (PyDict_Next(scopes, &pos, &name, &v)) {
490 : long scope;
491 : assert(PyLong_Check(v));
492 168 : scope = PyLong_AS_LONG(v);
493 168 : if (scope != LOCAL)
494 52 : continue;
495 116 : if (!PySet_Contains(free, name))
496 116 : continue;
497 0 : if (restricted != NULL &&
498 0 : PyUnicode_CompareWithASCIIString(name, restricted))
499 0 : continue;
500 : /* Replace LOCAL with CELL for this name, and remove
501 : from free. It is safe to replace the value of name
502 : in the dict, because it will not cause a resize.
503 : */
504 0 : if (PyDict_SetItem(scopes, name, v_cell) < 0)
505 0 : goto error;
506 0 : if (PySet_Discard(free, name) < 0)
507 0 : goto error;
508 : }
509 27 : success = 1;
510 : error:
511 27 : Py_DECREF(v_cell);
512 27 : return success;
513 : }
514 :
515 : /* Check for illegal statements in unoptimized namespaces */
516 : static int
517 30 : check_unoptimized(const PySTEntryObject* ste) {
518 : const char* trailer;
519 :
520 30 : if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized
521 0 : || !(ste->ste_free || ste->ste_child_free))
522 30 : return 1;
523 :
524 0 : trailer = (ste->ste_child_free ?
525 0 : "contains a nested function with free variables" :
526 : "is a nested function");
527 :
528 0 : switch (ste->ste_unoptimized) {
529 : case OPT_TOPLEVEL: /* import * at top-level is fine */
530 0 : return 1;
531 : case OPT_IMPORT_STAR:
532 0 : PyErr_Format(PyExc_SyntaxError,
533 : "import * is not allowed in function '%U' because it %s",
534 : ste->ste_name, trailer);
535 0 : break;
536 : }
537 :
538 0 : PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno,
539 : ste->ste_opt_col_offset);
540 0 : return 0;
541 : }
542 :
543 : /* Enter the final scope information into the ste_symbols dict.
544 : *
545 : * All arguments are dicts. Modifies symbols, others are read-only.
546 : */
547 : static int
548 30 : update_symbols(PyObject *symbols, PyObject *scopes,
549 : PyObject *bound, PyObject *free, int classflag)
550 : {
551 30 : PyObject *name = NULL, *itr = NULL;
552 30 : PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
553 30 : Py_ssize_t pos = 0;
554 :
555 : /* Update scope information for all symbols in this scope */
556 260 : while (PyDict_Next(symbols, &pos, &name, &v)) {
557 : long scope, flags;
558 : assert(PyLong_Check(v));
559 200 : flags = PyLong_AS_LONG(v);
560 200 : v_scope = PyDict_GetItem(scopes, name);
561 : assert(v_scope && PyLong_Check(v_scope));
562 200 : scope = PyLong_AS_LONG(v_scope);
563 200 : flags |= (scope << SCOPE_OFFSET);
564 200 : v_new = PyLong_FromLong(flags);
565 200 : if (!v_new)
566 0 : return 0;
567 200 : if (PyDict_SetItem(symbols, name, v_new) < 0) {
568 0 : Py_DECREF(v_new);
569 0 : return 0;
570 : }
571 200 : Py_DECREF(v_new);
572 : }
573 :
574 : /* Record not yet resolved free variables from children (if any) */
575 30 : v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
576 30 : if (!v_free)
577 0 : return 0;
578 :
579 30 : itr = PyObject_GetIter(free);
580 30 : if (!itr)
581 0 : goto error;
582 :
583 60 : while ((name = PyIter_Next(itr))) {
584 0 : v = PyDict_GetItem(symbols, name);
585 :
586 : /* Handle symbol that already exists in this scope */
587 0 : if (v) {
588 : /* Handle a free variable in a method of
589 : the class that has the same name as a local
590 : or global in the class scope.
591 : */
592 0 : if (classflag &&
593 0 : PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
594 0 : long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
595 0 : v_new = PyLong_FromLong(flags);
596 0 : if (!v_new) {
597 0 : goto error;
598 : }
599 0 : if (PyDict_SetItem(symbols, name, v_new) < 0) {
600 0 : Py_DECREF(v_new);
601 0 : goto error;
602 : }
603 0 : Py_DECREF(v_new);
604 : }
605 : /* It's a cell, or already free in this scope */
606 0 : Py_DECREF(name);
607 0 : continue;
608 : }
609 : /* Handle global symbol */
610 0 : if (!PySet_Contains(bound, name)) {
611 0 : Py_DECREF(name);
612 0 : continue; /* it's a global */
613 : }
614 : /* Propagate new free symbol up the lexical stack */
615 0 : if (PyDict_SetItem(symbols, name, v_free) < 0) {
616 0 : goto error;
617 : }
618 0 : Py_DECREF(name);
619 : }
620 30 : Py_DECREF(itr);
621 30 : Py_DECREF(v_free);
622 30 : return 1;
623 : error:
624 0 : Py_XDECREF(v_free);
625 0 : Py_XDECREF(itr);
626 0 : Py_XDECREF(name);
627 0 : return 0;
628 : }
629 :
630 : /* Make final symbol table decisions for block of ste.
631 :
632 : Arguments:
633 : ste -- current symtable entry (input/output)
634 : bound -- set of variables bound in enclosing scopes (input). bound
635 : is NULL for module blocks.
636 : free -- set of free variables in enclosed scopes (output)
637 : globals -- set of declared global variables in enclosing scopes (input)
638 :
639 : The implementation uses two mutually recursive functions,
640 : analyze_block() and analyze_child_block(). analyze_block() is
641 : responsible for analyzing the individual names defined in a block.
642 : analyze_child_block() prepares temporary namespace dictionaries
643 : used to evaluated nested blocks.
644 :
645 : The two functions exist because a child block should see the name
646 : bindings of its enclosing blocks, but those bindings should not
647 : propagate back to a parent block.
648 : */
649 :
650 : static int
651 : analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
652 : PyObject *global, PyObject* child_free);
653 :
654 : static int
655 30 : analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
656 : PyObject *global)
657 : {
658 30 : PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
659 30 : PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
660 : PyObject *temp;
661 30 : int i, success = 0;
662 30 : Py_ssize_t pos = 0;
663 :
664 30 : local = PySet_New(NULL); /* collect new names bound in block */
665 30 : if (!local)
666 0 : goto error;
667 30 : scopes = PyDict_New(); /* collect scopes defined for each name */
668 30 : if (!scopes)
669 0 : goto error;
670 :
671 : /* Allocate new global and bound variable dictionaries. These
672 : dictionaries hold the names visible in nested blocks. For
673 : ClassBlocks, the bound and global names are initialized
674 : before analyzing names, because class bindings aren't
675 : visible in methods. For other blocks, they are initialized
676 : after names are analyzed.
677 : */
678 :
679 : /* TODO(jhylton): Package these dicts in a struct so that we
680 : can write reasonable helper functions?
681 : */
682 30 : newglobal = PySet_New(NULL);
683 30 : if (!newglobal)
684 0 : goto error;
685 30 : newfree = PySet_New(NULL);
686 30 : if (!newfree)
687 0 : goto error;
688 30 : newbound = PySet_New(NULL);
689 30 : if (!newbound)
690 0 : goto error;
691 :
692 : /* Class namespace has no effect on names visible in
693 : nested functions, so populate the global and bound
694 : sets to be passed to child blocks before analyzing
695 : this one.
696 : */
697 30 : if (ste->ste_type == ClassBlock) {
698 : /* Pass down known globals */
699 3 : temp = PyNumber_InPlaceOr(newglobal, global);
700 3 : if (!temp)
701 0 : goto error;
702 3 : Py_DECREF(temp);
703 : /* Pass down previously bound symbols */
704 3 : if (bound) {
705 3 : temp = PyNumber_InPlaceOr(newbound, bound);
706 3 : if (!temp)
707 0 : goto error;
708 3 : Py_DECREF(temp);
709 : }
710 : }
711 :
712 260 : while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
713 200 : long flags = PyLong_AS_LONG(v);
714 200 : if (!analyze_name(ste, scopes, name, flags,
715 : bound, local, free, global))
716 0 : goto error;
717 : }
718 :
719 : /* Populate global and bound sets to be passed to children. */
720 30 : if (ste->ste_type != ClassBlock) {
721 : /* Add function locals to bound set */
722 27 : if (ste->ste_type == FunctionBlock) {
723 24 : temp = PyNumber_InPlaceOr(newbound, local);
724 24 : if (!temp)
725 0 : goto error;
726 24 : Py_DECREF(temp);
727 : }
728 : /* Pass down previously bound symbols */
729 27 : if (bound) {
730 24 : temp = PyNumber_InPlaceOr(newbound, bound);
731 24 : if (!temp)
732 0 : goto error;
733 24 : Py_DECREF(temp);
734 : }
735 : /* Pass down known globals */
736 27 : temp = PyNumber_InPlaceOr(newglobal, global);
737 27 : if (!temp)
738 0 : goto error;
739 27 : Py_DECREF(temp);
740 : }
741 : else {
742 : /* Special-case __class__ */
743 3 : if (!GET_IDENTIFIER(__class__))
744 0 : goto error;
745 : assert(PySet_Contains(local, __class__) == 1);
746 3 : if (PySet_Add(newbound, __class__) < 0)
747 0 : goto error;
748 : }
749 :
750 : /* Recursively call analyze_child_block() on each child block.
751 :
752 : newbound, newglobal now contain the names visible in
753 : nested blocks. The free variables in the children will
754 : be collected in allfree.
755 : */
756 30 : allfree = PySet_New(NULL);
757 30 : if (!allfree)
758 0 : goto error;
759 57 : for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
760 27 : PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
761 : PySTEntryObject* entry;
762 : assert(c && PySTEntry_Check(c));
763 27 : entry = (PySTEntryObject*)c;
764 27 : if (!analyze_child_block(entry, newbound, newfree, newglobal,
765 : allfree))
766 0 : goto error;
767 : /* Check if any children have free variables */
768 27 : if (entry->ste_free || entry->ste_child_free)
769 0 : ste->ste_child_free = 1;
770 : }
771 :
772 30 : temp = PyNumber_InPlaceOr(newfree, allfree);
773 30 : if (!temp)
774 0 : goto error;
775 30 : Py_DECREF(temp);
776 :
777 : /* Check if any local variables must be converted to cell variables */
778 30 : if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree,
779 : NULL))
780 : goto error;
781 30 : else if (ste->ste_type == ClassBlock && !analyze_cells(scopes, newfree,
782 : "__class__"))
783 0 : goto error;
784 : /* Records the results of the analysis in the symbol table entry */
785 30 : if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
786 30 : ste->ste_type == ClassBlock))
787 0 : goto error;
788 30 : if (!check_unoptimized(ste))
789 0 : goto error;
790 :
791 30 : temp = PyNumber_InPlaceOr(free, newfree);
792 30 : if (!temp)
793 0 : goto error;
794 30 : Py_DECREF(temp);
795 30 : success = 1;
796 : error:
797 30 : Py_XDECREF(scopes);
798 30 : Py_XDECREF(local);
799 30 : Py_XDECREF(newbound);
800 30 : Py_XDECREF(newglobal);
801 30 : Py_XDECREF(newfree);
802 30 : Py_XDECREF(allfree);
803 : if (!success)
804 : assert(PyErr_Occurred());
805 30 : return success;
806 : }
807 :
808 : static int
809 27 : analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
810 : PyObject *global, PyObject* child_free)
811 : {
812 27 : PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
813 : PyObject *temp;
814 :
815 : /* Copy the bound and global dictionaries.
816 :
817 : These dictionary are used by all blocks enclosed by the
818 : current block. The analyze_block() call modifies these
819 : dictionaries.
820 :
821 : */
822 27 : temp_bound = PySet_New(bound);
823 27 : if (!temp_bound)
824 0 : goto error;
825 27 : temp_free = PySet_New(free);
826 27 : if (!temp_free)
827 0 : goto error;
828 27 : temp_global = PySet_New(global);
829 27 : if (!temp_global)
830 0 : goto error;
831 :
832 27 : if (!analyze_block(entry, temp_bound, temp_free, temp_global))
833 0 : goto error;
834 27 : temp = PyNumber_InPlaceOr(child_free, temp_free);
835 27 : if (!temp)
836 0 : goto error;
837 27 : Py_DECREF(temp);
838 27 : Py_DECREF(temp_bound);
839 27 : Py_DECREF(temp_free);
840 27 : Py_DECREF(temp_global);
841 27 : return 1;
842 : error:
843 0 : Py_XDECREF(temp_bound);
844 0 : Py_XDECREF(temp_free);
845 0 : Py_XDECREF(temp_global);
846 0 : return 0;
847 : }
848 :
849 : static int
850 3 : symtable_analyze(struct symtable *st)
851 : {
852 : PyObject *free, *global;
853 : int r;
854 :
855 3 : free = PySet_New(NULL);
856 3 : if (!free)
857 0 : return 0;
858 3 : global = PySet_New(NULL);
859 3 : if (!global) {
860 0 : Py_DECREF(free);
861 0 : return 0;
862 : }
863 3 : r = analyze_block(st->st_top, NULL, free, global);
864 3 : Py_DECREF(free);
865 3 : Py_DECREF(global);
866 3 : return r;
867 : }
868 :
869 :
870 : static int
871 0 : symtable_warn(struct symtable *st, char *msg, int lineno)
872 : {
873 0 : if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
874 : lineno, NULL, NULL) < 0) {
875 0 : if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
876 0 : PyErr_SetString(PyExc_SyntaxError, msg);
877 0 : PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno,
878 0 : st->st_cur->ste_col_offset);
879 : }
880 0 : return 0;
881 : }
882 0 : return 1;
883 : }
884 :
885 : /* symtable_enter_block() gets a reference via ste_new.
886 : This reference is released when the block is exited, via the DECREF
887 : in symtable_exit_block().
888 : */
889 :
890 : static int
891 30 : symtable_exit_block(struct symtable *st, void *ast)
892 : {
893 : Py_ssize_t size;
894 :
895 30 : st->st_cur = NULL;
896 30 : size = PyList_GET_SIZE(st->st_stack);
897 30 : if (size) {
898 30 : if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
899 0 : return 0;
900 30 : if (--size)
901 27 : st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
902 : }
903 30 : return 1;
904 : }
905 :
906 : static int
907 30 : symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
908 : void *ast, int lineno, int col_offset)
909 : {
910 30 : PySTEntryObject *prev = NULL, *ste;
911 :
912 30 : ste = ste_new(st, name, block, ast, lineno, col_offset);
913 30 : if (ste == NULL)
914 0 : return 0;
915 30 : if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
916 0 : Py_DECREF(ste);
917 0 : return 0;
918 : }
919 30 : prev = st->st_cur;
920 : /* The entry is owned by the stack. Borrow it for st_cur. */
921 30 : Py_DECREF(ste);
922 30 : st->st_cur = ste;
923 30 : if (block == ModuleBlock)
924 3 : st->st_global = st->st_cur->ste_symbols;
925 30 : if (prev) {
926 27 : if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
927 0 : return 0;
928 : }
929 : }
930 30 : return 1;
931 : }
932 :
933 : static long
934 0 : symtable_lookup(struct symtable *st, PyObject *name)
935 : {
936 : PyObject *o;
937 0 : PyObject *mangled = _Py_Mangle(st->st_private, name);
938 0 : if (!mangled)
939 0 : return 0;
940 0 : o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
941 0 : Py_DECREF(mangled);
942 0 : if (!o)
943 0 : return 0;
944 0 : return PyLong_AsLong(o);
945 : }
946 :
947 : static int
948 419 : symtable_add_def(struct symtable *st, PyObject *name, int flag)
949 : {
950 : PyObject *o;
951 : PyObject *dict;
952 : long val;
953 419 : PyObject *mangled = _Py_Mangle(st->st_private, name);
954 :
955 :
956 419 : if (!mangled)
957 0 : return 0;
958 419 : dict = st->st_cur->ste_symbols;
959 419 : if ((o = PyDict_GetItem(dict, mangled))) {
960 219 : val = PyLong_AS_LONG(o);
961 219 : if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
962 : /* Is it better to use 'mangled' or 'name' here? */
963 0 : PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
964 0 : PyErr_SyntaxLocationEx(st->st_filename,
965 0 : st->st_cur->ste_lineno,
966 0 : st->st_cur->ste_col_offset);
967 0 : goto error;
968 : }
969 219 : val |= flag;
970 : } else
971 200 : val = flag;
972 419 : o = PyLong_FromLong(val);
973 419 : if (o == NULL)
974 0 : goto error;
975 419 : if (PyDict_SetItem(dict, mangled, o) < 0) {
976 0 : Py_DECREF(o);
977 0 : goto error;
978 : }
979 419 : Py_DECREF(o);
980 :
981 419 : if (flag & DEF_PARAM) {
982 57 : if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
983 0 : goto error;
984 362 : } else if (flag & DEF_GLOBAL) {
985 : /* XXX need to update DEF_GLOBAL for other flags too;
986 : perhaps only DEF_FREE_GLOBAL */
987 0 : val = flag;
988 0 : if ((o = PyDict_GetItem(st->st_global, mangled))) {
989 0 : val |= PyLong_AS_LONG(o);
990 : }
991 0 : o = PyLong_FromLong(val);
992 0 : if (o == NULL)
993 0 : goto error;
994 0 : if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
995 0 : Py_DECREF(o);
996 0 : goto error;
997 : }
998 0 : Py_DECREF(o);
999 : }
1000 419 : Py_DECREF(mangled);
1001 419 : return 1;
1002 :
1003 : error:
1004 0 : Py_DECREF(mangled);
1005 0 : return 0;
1006 : }
1007 :
1008 : /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1009 : They use the ASDL name to synthesize the name of the C type and the visit
1010 : function.
1011 :
1012 : VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1013 : useful if the first node in the sequence requires special treatment.
1014 : */
1015 :
1016 : #define VISIT(ST, TYPE, V) \
1017 : if (!symtable_visit_ ## TYPE((ST), (V))) \
1018 : return 0;
1019 :
1020 : #define VISIT_SEQ(ST, TYPE, SEQ) { \
1021 : int i; \
1022 : asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1023 : for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1024 : TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1025 : if (!symtable_visit_ ## TYPE((ST), elt)) \
1026 : return 0; \
1027 : } \
1028 : }
1029 :
1030 : #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
1031 : int i; \
1032 : asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1033 : for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1034 : TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1035 : if (!symtable_visit_ ## TYPE((ST), elt)) \
1036 : return 0; \
1037 : } \
1038 : }
1039 :
1040 : #define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \
1041 : int i = 0; \
1042 : asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \
1043 : for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1044 : expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \
1045 : if (!elt) continue; /* can be NULL */ \
1046 : if (!symtable_visit_expr((ST), elt)) \
1047 : return 0; \
1048 : } \
1049 : }
1050 :
1051 : static int
1052 0 : symtable_new_tmpname(struct symtable *st)
1053 : {
1054 : char tmpname[256];
1055 : identifier tmp;
1056 :
1057 0 : PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1058 0 : ++st->st_cur->ste_tmpname);
1059 0 : tmp = PyUnicode_InternFromString(tmpname);
1060 0 : if (!tmp)
1061 0 : return 0;
1062 0 : if (!symtable_add_def(st, tmp, DEF_LOCAL))
1063 0 : return 0;
1064 0 : Py_DECREF(tmp);
1065 0 : return 1;
1066 : }
1067 :
1068 :
1069 : static int
1070 186 : symtable_visit_stmt(struct symtable *st, stmt_ty s)
1071 : {
1072 186 : switch (s->kind) {
1073 : case FunctionDef_kind:
1074 24 : if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
1075 0 : return 0;
1076 24 : if (s->v.FunctionDef.args->defaults)
1077 2 : VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1078 24 : if (s->v.FunctionDef.args->kw_defaults)
1079 0 : VISIT_KWONLYDEFAULTS(st,
1080 : s->v.FunctionDef.args->kw_defaults);
1081 24 : if (!symtable_visit_annotations(st, s))
1082 0 : return 0;
1083 24 : if (s->v.FunctionDef.decorator_list)
1084 2 : VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1085 24 : if (!symtable_enter_block(st, s->v.FunctionDef.name,
1086 : FunctionBlock, (void *)s, s->lineno,
1087 : s->col_offset))
1088 0 : return 0;
1089 24 : VISIT(st, arguments, s->v.FunctionDef.args);
1090 24 : VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
1091 24 : if (!symtable_exit_block(st, s))
1092 0 : return 0;
1093 24 : break;
1094 : case ClassDef_kind: {
1095 : PyObject *tmp;
1096 3 : if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
1097 0 : return 0;
1098 3 : VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1099 3 : VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
1100 3 : if (s->v.ClassDef.starargs)
1101 0 : VISIT(st, expr, s->v.ClassDef.starargs);
1102 3 : if (s->v.ClassDef.kwargs)
1103 0 : VISIT(st, expr, s->v.ClassDef.kwargs);
1104 3 : if (s->v.ClassDef.decorator_list)
1105 0 : VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1106 3 : if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
1107 : (void *)s, s->lineno, s->col_offset))
1108 0 : return 0;
1109 9 : if (!GET_IDENTIFIER(__class__) ||
1110 6 : !symtable_add_def(st, __class__, DEF_LOCAL) ||
1111 7 : !GET_IDENTIFIER(__locals__) ||
1112 3 : !symtable_add_def(st, __locals__, DEF_PARAM)) {
1113 0 : symtable_exit_block(st, s);
1114 0 : return 0;
1115 : }
1116 3 : tmp = st->st_private;
1117 3 : st->st_private = s->v.ClassDef.name;
1118 3 : VISIT_SEQ(st, stmt, s->v.ClassDef.body);
1119 3 : st->st_private = tmp;
1120 3 : if (!symtable_exit_block(st, s))
1121 0 : return 0;
1122 3 : break;
1123 : }
1124 : case Return_kind:
1125 26 : if (s->v.Return.value) {
1126 26 : VISIT(st, expr, s->v.Return.value);
1127 26 : st->st_cur->ste_returns_value = 1;
1128 : }
1129 26 : break;
1130 : case Delete_kind:
1131 0 : VISIT_SEQ(st, expr, s->v.Delete.targets);
1132 0 : break;
1133 : case Assign_kind:
1134 61 : VISIT_SEQ(st, expr, s->v.Assign.targets);
1135 61 : VISIT(st, expr, s->v.Assign.value);
1136 61 : break;
1137 : case AugAssign_kind:
1138 1 : VISIT(st, expr, s->v.AugAssign.target);
1139 1 : VISIT(st, expr, s->v.AugAssign.value);
1140 1 : break;
1141 : case For_kind:
1142 2 : VISIT(st, expr, s->v.For.target);
1143 2 : VISIT(st, expr, s->v.For.iter);
1144 2 : VISIT_SEQ(st, stmt, s->v.For.body);
1145 2 : if (s->v.For.orelse)
1146 0 : VISIT_SEQ(st, stmt, s->v.For.orelse);
1147 2 : break;
1148 : case While_kind:
1149 1 : VISIT(st, expr, s->v.While.test);
1150 1 : VISIT_SEQ(st, stmt, s->v.While.body);
1151 1 : if (s->v.While.orelse)
1152 0 : VISIT_SEQ(st, stmt, s->v.While.orelse);
1153 1 : break;
1154 : case If_kind:
1155 : /* XXX if 0: and lookup_yield() hacks */
1156 14 : VISIT(st, expr, s->v.If.test);
1157 14 : VISIT_SEQ(st, stmt, s->v.If.body);
1158 14 : if (s->v.If.orelse)
1159 2 : VISIT_SEQ(st, stmt, s->v.If.orelse);
1160 14 : break;
1161 : case Raise_kind:
1162 4 : if (s->v.Raise.exc) {
1163 4 : VISIT(st, expr, s->v.Raise.exc);
1164 4 : if (s->v.Raise.cause) {
1165 0 : VISIT(st, expr, s->v.Raise.cause);
1166 : }
1167 : }
1168 4 : break;
1169 : case Try_kind:
1170 5 : VISIT_SEQ(st, stmt, s->v.Try.body);
1171 5 : VISIT_SEQ(st, stmt, s->v.Try.orelse);
1172 5 : VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1173 5 : VISIT_SEQ(st, stmt, s->v.Try.finalbody);
1174 5 : break;
1175 : case Assert_kind:
1176 0 : VISIT(st, expr, s->v.Assert.test);
1177 0 : if (s->v.Assert.msg)
1178 0 : VISIT(st, expr, s->v.Assert.msg);
1179 0 : break;
1180 : case Import_kind:
1181 4 : VISIT_SEQ(st, alias, s->v.Import.names);
1182 : /* XXX Don't have the lineno available inside
1183 : visit_alias */
1184 4 : if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
1185 1 : st->st_cur->ste_opt_lineno = s->lineno;
1186 1 : st->st_cur->ste_opt_col_offset = s->col_offset;
1187 : }
1188 4 : break;
1189 : case ImportFrom_kind:
1190 13 : VISIT_SEQ(st, alias, s->v.ImportFrom.names);
1191 : /* XXX Don't have the lineno available inside
1192 : visit_alias */
1193 13 : if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) {
1194 2 : st->st_cur->ste_opt_lineno = s->lineno;
1195 2 : st->st_cur->ste_opt_col_offset = s->col_offset;
1196 : }
1197 13 : break;
1198 : case Global_kind: {
1199 : int i;
1200 0 : asdl_seq *seq = s->v.Global.names;
1201 0 : for (i = 0; i < asdl_seq_LEN(seq); i++) {
1202 0 : identifier name = (identifier)asdl_seq_GET(seq, i);
1203 0 : char *c_name = _PyUnicode_AsString(name);
1204 0 : long cur = symtable_lookup(st, name);
1205 0 : if (cur < 0)
1206 0 : return 0;
1207 0 : if (cur & (DEF_LOCAL | USE)) {
1208 : char buf[256];
1209 0 : if (cur & DEF_LOCAL)
1210 0 : PyOS_snprintf(buf, sizeof(buf),
1211 : GLOBAL_AFTER_ASSIGN,
1212 : c_name);
1213 : else
1214 0 : PyOS_snprintf(buf, sizeof(buf),
1215 : GLOBAL_AFTER_USE,
1216 : c_name);
1217 0 : if (!symtable_warn(st, buf, s->lineno))
1218 0 : return 0;
1219 : }
1220 0 : if (!symtable_add_def(st, name, DEF_GLOBAL))
1221 0 : return 0;
1222 : }
1223 0 : break;
1224 : }
1225 : case Nonlocal_kind: {
1226 : int i;
1227 0 : asdl_seq *seq = s->v.Nonlocal.names;
1228 0 : for (i = 0; i < asdl_seq_LEN(seq); i++) {
1229 0 : identifier name = (identifier)asdl_seq_GET(seq, i);
1230 0 : char *c_name = _PyUnicode_AsString(name);
1231 0 : long cur = symtable_lookup(st, name);
1232 0 : if (cur < 0)
1233 0 : return 0;
1234 0 : if (cur & (DEF_LOCAL | USE)) {
1235 : char buf[256];
1236 0 : if (cur & DEF_LOCAL)
1237 0 : PyOS_snprintf(buf, sizeof(buf),
1238 : NONLOCAL_AFTER_ASSIGN,
1239 : c_name);
1240 : else
1241 0 : PyOS_snprintf(buf, sizeof(buf),
1242 : NONLOCAL_AFTER_USE,
1243 : c_name);
1244 0 : if (!symtable_warn(st, buf, s->lineno))
1245 0 : return 0;
1246 : }
1247 0 : if (!symtable_add_def(st, name, DEF_NONLOCAL))
1248 0 : return 0;
1249 : }
1250 0 : break;
1251 : }
1252 : case Expr_kind:
1253 27 : VISIT(st, expr, s->v.Expr.value);
1254 27 : break;
1255 : case Pass_kind:
1256 : case Break_kind:
1257 : case Continue_kind:
1258 : /* nothing to do here */
1259 1 : break;
1260 : case With_kind:
1261 0 : VISIT_SEQ(st, withitem, s->v.With.items);
1262 0 : VISIT_SEQ(st, stmt, s->v.With.body);
1263 0 : break;
1264 : }
1265 186 : return 1;
1266 : }
1267 :
1268 : static int
1269 686 : symtable_visit_expr(struct symtable *st, expr_ty e)
1270 : {
1271 686 : switch (e->kind) {
1272 : case BoolOp_kind:
1273 5 : VISIT_SEQ(st, expr, e->v.BoolOp.values);
1274 5 : break;
1275 : case BinOp_kind:
1276 20 : VISIT(st, expr, e->v.BinOp.left);
1277 20 : VISIT(st, expr, e->v.BinOp.right);
1278 20 : break;
1279 : case UnaryOp_kind:
1280 1 : VISIT(st, expr, e->v.UnaryOp.operand);
1281 1 : break;
1282 : case Lambda_kind: {
1283 0 : if (!GET_IDENTIFIER(lambda))
1284 0 : return 0;
1285 0 : if (e->v.Lambda.args->defaults)
1286 0 : VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1287 0 : if (e->v.Lambda.args->kw_defaults)
1288 0 : VISIT_KWONLYDEFAULTS(st,
1289 : e->v.Lambda.args->kw_defaults);
1290 0 : if (!symtable_enter_block(st, lambda,
1291 : FunctionBlock, (void *)e, e->lineno,
1292 : e->col_offset))
1293 0 : return 0;
1294 0 : VISIT(st, arguments, e->v.Lambda.args);
1295 0 : VISIT(st, expr, e->v.Lambda.body);
1296 0 : if (!symtable_exit_block(st, (void *)e))
1297 0 : return 0;
1298 0 : break;
1299 : }
1300 : case IfExp_kind:
1301 0 : VISIT(st, expr, e->v.IfExp.test);
1302 0 : VISIT(st, expr, e->v.IfExp.body);
1303 0 : VISIT(st, expr, e->v.IfExp.orelse);
1304 0 : break;
1305 : case Dict_kind:
1306 1 : VISIT_SEQ(st, expr, e->v.Dict.keys);
1307 1 : VISIT_SEQ(st, expr, e->v.Dict.values);
1308 1 : break;
1309 : case Set_kind:
1310 0 : VISIT_SEQ(st, expr, e->v.Set.elts);
1311 0 : break;
1312 : case GeneratorExp_kind:
1313 0 : if (!symtable_visit_genexp(st, e))
1314 0 : return 0;
1315 0 : break;
1316 : case ListComp_kind:
1317 0 : if (!symtable_visit_listcomp(st, e))
1318 0 : return 0;
1319 0 : break;
1320 : case SetComp_kind:
1321 0 : if (!symtable_visit_setcomp(st, e))
1322 0 : return 0;
1323 0 : break;
1324 : case DictComp_kind:
1325 0 : if (!symtable_visit_dictcomp(st, e))
1326 0 : return 0;
1327 0 : break;
1328 : case Yield_kind:
1329 : case YieldFrom_kind: {
1330 : expr_ty value;
1331 0 : value = (e->kind == YieldFrom_kind) ? e->v.YieldFrom.value : e->v.Yield.value;
1332 0 : if (value)
1333 0 : VISIT(st, expr, value);
1334 0 : st->st_cur->ste_generator = 1;
1335 0 : break;
1336 : }
1337 : case Compare_kind:
1338 20 : VISIT(st, expr, e->v.Compare.left);
1339 20 : VISIT_SEQ(st, expr, e->v.Compare.comparators);
1340 20 : break;
1341 : case Call_kind:
1342 81 : VISIT(st, expr, e->v.Call.func);
1343 81 : VISIT_SEQ(st, expr, e->v.Call.args);
1344 81 : VISIT_SEQ(st, keyword, e->v.Call.keywords);
1345 81 : if (e->v.Call.starargs)
1346 0 : VISIT(st, expr, e->v.Call.starargs);
1347 81 : if (e->v.Call.kwargs)
1348 0 : VISIT(st, expr, e->v.Call.kwargs);
1349 81 : break;
1350 : case Num_kind:
1351 : case Str_kind:
1352 : case Bytes_kind:
1353 : case Ellipsis_kind:
1354 : /* Nothing to do here. */
1355 110 : break;
1356 : /* The following exprs can be assignment targets. */
1357 : case Attribute_kind:
1358 108 : VISIT(st, expr, e->v.Attribute.value);
1359 108 : break;
1360 : case Subscript_kind:
1361 21 : VISIT(st, expr, e->v.Subscript.value);
1362 21 : VISIT(st, slice, e->v.Subscript.slice);
1363 21 : break;
1364 : case Starred_kind:
1365 0 : VISIT(st, expr, e->v.Starred.value);
1366 0 : break;
1367 : case Name_kind:
1368 302 : if (!symtable_add_def(st, e->v.Name.id,
1369 302 : e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1370 0 : return 0;
1371 : /* Special-case super: it counts as a use of __class__ */
1372 570 : if (e->v.Name.ctx == Load &&
1373 493 : st->st_cur->ste_type == FunctionBlock &&
1374 225 : !PyUnicode_CompareWithASCIIString(e->v.Name.id, "super")) {
1375 0 : if (!GET_IDENTIFIER(__class__) ||
1376 0 : !symtable_add_def(st, __class__, USE))
1377 0 : return 0;
1378 : }
1379 302 : break;
1380 : /* child nodes of List and Tuple will have expr_context set */
1381 : case List_kind:
1382 2 : VISIT_SEQ(st, expr, e->v.List.elts);
1383 2 : break;
1384 : case Tuple_kind:
1385 15 : VISIT_SEQ(st, expr, e->v.Tuple.elts);
1386 15 : break;
1387 : }
1388 686 : return 1;
1389 : }
1390 :
1391 : static int
1392 0 : symtable_implicit_arg(struct symtable *st, int pos)
1393 : {
1394 0 : PyObject *id = PyUnicode_FromFormat(".%d", pos);
1395 0 : if (id == NULL)
1396 0 : return 0;
1397 0 : if (!symtable_add_def(st, id, DEF_PARAM)) {
1398 0 : Py_DECREF(id);
1399 0 : return 0;
1400 : }
1401 0 : Py_DECREF(id);
1402 0 : return 1;
1403 : }
1404 :
1405 : static int
1406 24 : symtable_visit_params(struct symtable *st, asdl_seq *args)
1407 : {
1408 : int i;
1409 :
1410 24 : if (!args)
1411 0 : return -1;
1412 :
1413 75 : for (i = 0; i < asdl_seq_LEN(args); i++) {
1414 51 : arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1415 51 : if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1416 0 : return 0;
1417 : }
1418 :
1419 24 : return 1;
1420 : }
1421 :
1422 : static int
1423 24 : symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
1424 : {
1425 : int i;
1426 :
1427 24 : if (!args)
1428 0 : return -1;
1429 :
1430 75 : for (i = 0; i < asdl_seq_LEN(args); i++) {
1431 51 : arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1432 51 : if (arg->annotation)
1433 0 : VISIT(st, expr, arg->annotation);
1434 : }
1435 :
1436 24 : return 1;
1437 : }
1438 :
1439 : static int
1440 24 : symtable_visit_annotations(struct symtable *st, stmt_ty s)
1441 : {
1442 24 : arguments_ty a = s->v.FunctionDef.args;
1443 :
1444 24 : if (a->args && !symtable_visit_argannotations(st, a->args))
1445 0 : return 0;
1446 24 : if (a->varargannotation)
1447 0 : VISIT(st, expr, a->varargannotation);
1448 24 : if (a->kwargannotation)
1449 0 : VISIT(st, expr, a->kwargannotation);
1450 24 : if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1451 0 : return 0;
1452 24 : if (s->v.FunctionDef.returns)
1453 0 : VISIT(st, expr, s->v.FunctionDef.returns);
1454 24 : return 1;
1455 : }
1456 :
1457 : static int
1458 24 : symtable_visit_arguments(struct symtable *st, arguments_ty a)
1459 : {
1460 : /* skip default arguments inside function block
1461 : XXX should ast be different?
1462 : */
1463 24 : if (a->args && !symtable_visit_params(st, a->args))
1464 0 : return 0;
1465 24 : if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1466 0 : return 0;
1467 24 : if (a->vararg) {
1468 1 : if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1469 0 : return 0;
1470 1 : st->st_cur->ste_varargs = 1;
1471 : }
1472 24 : if (a->kwarg) {
1473 2 : if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1474 0 : return 0;
1475 2 : st->st_cur->ste_varkeywords = 1;
1476 : }
1477 24 : return 1;
1478 : }
1479 :
1480 :
1481 : static int
1482 5 : symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1483 : {
1484 5 : if (eh->v.ExceptHandler.type)
1485 3 : VISIT(st, expr, eh->v.ExceptHandler.type);
1486 5 : if (eh->v.ExceptHandler.name)
1487 3 : if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1488 0 : return 0;
1489 5 : VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1490 5 : return 1;
1491 : }
1492 :
1493 : static int
1494 0 : symtable_visit_withitem(struct symtable *st, withitem_ty item)
1495 : {
1496 0 : VISIT(st, expr, item->context_expr);
1497 0 : if (item->optional_vars) {
1498 0 : VISIT(st, expr, item->optional_vars);
1499 : }
1500 0 : return 1;
1501 : }
1502 :
1503 :
1504 : static int
1505 27 : symtable_visit_alias(struct symtable *st, alias_ty a)
1506 : {
1507 : /* Compute store_name, the name actually bound by the import
1508 : operation. It is different than a->name when a->name is a
1509 : dotted package name (e.g. spam.eggs)
1510 : */
1511 : PyObject *store_name;
1512 27 : PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1513 27 : Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1514 : PyUnicode_GET_LENGTH(name), 1);
1515 27 : if (dot != -1) {
1516 0 : store_name = PyUnicode_Substring(name, 0, dot);
1517 0 : if (!store_name)
1518 0 : return 0;
1519 : }
1520 : else {
1521 27 : store_name = name;
1522 27 : Py_INCREF(store_name);
1523 : }
1524 27 : if (PyUnicode_CompareWithASCIIString(name, "*")) {
1525 27 : int r = symtable_add_def(st, store_name, DEF_IMPORT);
1526 27 : Py_DECREF(store_name);
1527 27 : return r;
1528 : }
1529 : else {
1530 0 : if (st->st_cur->ste_type != ModuleBlock) {
1531 0 : int lineno = st->st_cur->ste_lineno;
1532 0 : int col_offset = st->st_cur->ste_col_offset;
1533 0 : PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1534 0 : PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset);
1535 0 : Py_DECREF(store_name);
1536 0 : return 0;
1537 : }
1538 0 : st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1539 0 : Py_DECREF(store_name);
1540 0 : return 1;
1541 : }
1542 : }
1543 :
1544 :
1545 : static int
1546 0 : symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1547 : {
1548 0 : VISIT(st, expr, lc->target);
1549 0 : VISIT(st, expr, lc->iter);
1550 0 : VISIT_SEQ(st, expr, lc->ifs);
1551 0 : return 1;
1552 : }
1553 :
1554 :
1555 : static int
1556 9 : symtable_visit_keyword(struct symtable *st, keyword_ty k)
1557 : {
1558 9 : VISIT(st, expr, k->value);
1559 9 : return 1;
1560 : }
1561 :
1562 :
1563 : static int
1564 21 : symtable_visit_slice(struct symtable *st, slice_ty s)
1565 : {
1566 21 : switch (s->kind) {
1567 : case Slice_kind:
1568 4 : if (s->v.Slice.lower)
1569 2 : VISIT(st, expr, s->v.Slice.lower)
1570 4 : if (s->v.Slice.upper)
1571 4 : VISIT(st, expr, s->v.Slice.upper)
1572 4 : if (s->v.Slice.step)
1573 0 : VISIT(st, expr, s->v.Slice.step)
1574 4 : break;
1575 : case ExtSlice_kind:
1576 0 : VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1577 0 : break;
1578 : case Index_kind:
1579 17 : VISIT(st, expr, s->v.Index.value)
1580 17 : break;
1581 : }
1582 21 : return 1;
1583 : }
1584 :
1585 : static int
1586 0 : symtable_handle_comprehension(struct symtable *st, expr_ty e,
1587 : identifier scope_name, asdl_seq *generators,
1588 : expr_ty elt, expr_ty value)
1589 : {
1590 0 : int is_generator = (e->kind == GeneratorExp_kind);
1591 0 : int needs_tmp = !is_generator;
1592 0 : comprehension_ty outermost = ((comprehension_ty)
1593 : asdl_seq_GET(generators, 0));
1594 : /* Outermost iterator is evaluated in current scope */
1595 0 : VISIT(st, expr, outermost->iter);
1596 : /* Create comprehension scope for the rest */
1597 0 : if (!scope_name ||
1598 0 : !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1599 : e->lineno, e->col_offset)) {
1600 0 : return 0;
1601 : }
1602 0 : st->st_cur->ste_generator = is_generator;
1603 : /* Outermost iter is received as an argument */
1604 0 : if (!symtable_implicit_arg(st, 0)) {
1605 0 : symtable_exit_block(st, (void *)e);
1606 0 : return 0;
1607 : }
1608 : /* Allocate temporary name if needed */
1609 0 : if (needs_tmp && !symtable_new_tmpname(st)) {
1610 0 : symtable_exit_block(st, (void *)e);
1611 0 : return 0;
1612 : }
1613 0 : VISIT(st, expr, outermost->target);
1614 0 : VISIT_SEQ(st, expr, outermost->ifs);
1615 0 : VISIT_SEQ_TAIL(st, comprehension, generators, 1);
1616 0 : if (value)
1617 0 : VISIT(st, expr, value);
1618 0 : VISIT(st, expr, elt);
1619 0 : return symtable_exit_block(st, (void *)e);
1620 : }
1621 :
1622 : static int
1623 0 : symtable_visit_genexp(struct symtable *st, expr_ty e)
1624 : {
1625 0 : return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1626 : e->v.GeneratorExp.generators,
1627 : e->v.GeneratorExp.elt, NULL);
1628 : }
1629 :
1630 : static int
1631 0 : symtable_visit_listcomp(struct symtable *st, expr_ty e)
1632 : {
1633 0 : return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1634 : e->v.ListComp.generators,
1635 : e->v.ListComp.elt, NULL);
1636 : }
1637 :
1638 : static int
1639 0 : symtable_visit_setcomp(struct symtable *st, expr_ty e)
1640 : {
1641 0 : return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1642 : e->v.SetComp.generators,
1643 : e->v.SetComp.elt, NULL);
1644 : }
1645 :
1646 : static int
1647 0 : symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1648 : {
1649 0 : return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1650 : e->v.DictComp.generators,
1651 : e->v.DictComp.key,
1652 : e->v.DictComp.value);
1653 : }
|