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