ยปCore Development>Code coverage>Python/symtable.c

Python code coverage for Python/symtable.c

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